blob: 3476ba6d40dc067cee6372138640cbc0c8fb2669 [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 Dunbardbc93372008-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 }
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000729 virtual llvm::Function *EnumerationMutationFunction() {
730 return ObjCTypes.EnumerationMutationFn;
731 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000732
733 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000734 const Stmt &S) {
735 CGF.ErrorUnsupported(&S, "try or synchronized statement");
736 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000737 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000738 const ObjCAtThrowStmt &S) {
739 CGF.ErrorUnsupported(&S, "throw statement");
740 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000741 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000742 llvm::Value *AddrWeakObj) {
743 assert(0 && "FIXME: Implement EmitObjCWeakRead for non-fragile ABI");
744 return 0;
745 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000746 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000747 llvm::Value *src, llvm::Value *dst) {
748 assert(0 && "FIXME: Implement EmitObjCWeakAssign for non-fragile ABI");
749 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000750 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000751 llvm::Value *src, llvm::Value *dest) {
752 assert(0 && "FIXME: Implement EmitObjCGlobalAssign for non-fragile ABI");
753 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000754 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000755 llvm::Value *src, llvm::Value *dest) {
756 assert(0 && "FIXME: Implement EmitObjCIvarAssign for non-fragile ABI");
757 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000758 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000759 llvm::Value *src, llvm::Value *dest) {
760 assert(0 && "FIXME: Implement EmitObjCStrongCastAssign for non-fragile ABI");
761 }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000762 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
763 QualType ObjectTy,
764 llvm::Value *BaseValue,
765 const ObjCIvarDecl *Ivar,
766 const FieldDecl *Field,
767 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000768 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
769 ObjCInterfaceDecl *Interface,
770 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000771};
772
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000773} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000774
775/* *** Helper Functions *** */
776
777/// getConstantGEP() - Help routine to construct simple GEPs.
778static llvm::Constant *getConstantGEP(llvm::Constant *C,
779 unsigned idx0,
780 unsigned idx1) {
781 llvm::Value *Idxs[] = {
782 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
783 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
784 };
785 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
786}
787
788/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000789
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000790CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
791 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000792{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000793 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000794 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000795}
796
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000797/// GetClass - Return a reference to the class for the given interface
798/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000799llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000800 const ObjCInterfaceDecl *ID) {
801 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000802}
803
804/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000805llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000806 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000807}
808
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000809/// Generate a constant CFString object.
810/*
811 struct __builtin_CFString {
812 const int *isa; // point to __CFConstantStringClassReference
813 int flags;
814 const char *str;
815 long length;
816 };
817*/
818
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000819llvm::Constant *CGObjCCommonMac::GenerateConstantString(
820 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000821 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000822}
823
824/// Generates a message send where the super is the receiver. This is
825/// a message send to self with special delivery semantics indicating
826/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000827CodeGen::RValue
828CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000829 QualType ResultType,
830 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000831 const ObjCInterfaceDecl *Class,
832 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000833 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000834 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000835 // Create and init a super structure; this is a (receiver, class)
836 // pair we will pass to objc_msgSendSuper.
837 llvm::Value *ObjCSuper =
838 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
839 llvm::Value *ReceiverAsObject =
840 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
841 CGF.Builder.CreateStore(ReceiverAsObject,
842 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000843
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000844 // If this is a class message the metaclass is passed as the target.
845 llvm::Value *Target;
846 if (IsClassMessage) {
847 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
848 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
849 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
850 Target = Super;
851 } else {
852 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
853 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000854 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
855 // and ObjCTypes types.
856 const llvm::Type *ClassTy =
857 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000858 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000859 CGF.Builder.CreateStore(Target,
860 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
861
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000862 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000863 ObjCSuper, ObjCTypes.SuperPtrCTy,
864 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000865}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000866
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000867/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000868CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000869 QualType ResultType,
870 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000871 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000872 bool IsClassMessage,
873 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000874 llvm::Value *Arg0 =
875 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000876 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000877 Arg0, CGF.getContext().getObjCIdType(),
878 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000879}
880
881CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000882 QualType ResultType,
883 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000884 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000885 QualType Arg0Ty,
886 bool IsSuper,
887 const CallArgList &CallArgs) {
888 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000889 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
890 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
891 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000892 CGF.getContext().getObjCSelType()));
893 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000894
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000895 CodeGenTypes &Types = CGM.getTypes();
896 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
897 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000898
899 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000900 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000901 Fn = ObjCTypes.getSendStretFn(IsSuper);
902 } else if (ResultType->isFloatingType()) {
903 // FIXME: Sadly, this is wrong. This actually depends on the
904 // architecture. This happens to be right for x86-32 though.
905 Fn = ObjCTypes.getSendFpretFn(IsSuper);
906 } else {
907 Fn = ObjCTypes.getSendFn(IsSuper);
908 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000909 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000910 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000911}
912
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000913llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000914 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000915 // FIXME: I don't understand why gcc generates this, or where it is
916 // resolved. Investigate. Its also wasteful to look this up over and
917 // over.
918 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
919
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000920 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
921 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000922}
923
Fariborz Jahanianda320092009-01-29 19:24:30 +0000924void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000925 // FIXME: We shouldn't need this, the protocol decl should contain
926 // enough information to tell us whether this was a declaration or a
927 // definition.
928 DefinedProtocols.insert(PD->getIdentifier());
929
930 // If we have generated a forward reference to this protocol, emit
931 // it now. Otherwise do nothing, the protocol objects are lazily
932 // emitted.
933 if (Protocols.count(PD->getIdentifier()))
934 GetOrEmitProtocol(PD);
935}
936
Fariborz Jahanianda320092009-01-29 19:24:30 +0000937llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000938 if (DefinedProtocols.count(PD->getIdentifier()))
939 return GetOrEmitProtocol(PD);
940 return GetOrEmitProtocolRef(PD);
941}
942
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000943/*
944 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
945 struct _objc_protocol {
946 struct _objc_protocol_extension *isa;
947 char *protocol_name;
948 struct _objc_protocol_list *protocol_list;
949 struct _objc__method_prototype_list *instance_methods;
950 struct _objc__method_prototype_list *class_methods
951 };
952
953 See EmitProtocolExtension().
954*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000955llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
956 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
957
958 // Early exit if a defining object has already been generated.
959 if (Entry && Entry->hasInitializer())
960 return Entry;
961
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000962 // FIXME: I don't understand why gcc generates this, or where it is
963 // resolved. Investigate. Its also wasteful to look this up over and
964 // over.
965 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
966
Chris Lattner8ec03f52008-11-24 03:54:41 +0000967 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000968
969 // Construct method lists.
970 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
971 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
972 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
973 e = PD->instmeth_end(); i != e; ++i) {
974 ObjCMethodDecl *MD = *i;
975 llvm::Constant *C = GetMethodDescriptionConstant(MD);
976 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
977 OptInstanceMethods.push_back(C);
978 } else {
979 InstanceMethods.push_back(C);
980 }
981 }
982
983 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
984 e = PD->classmeth_end(); i != e; ++i) {
985 ObjCMethodDecl *MD = *i;
986 llvm::Constant *C = GetMethodDescriptionConstant(MD);
987 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
988 OptClassMethods.push_back(C);
989 } else {
990 ClassMethods.push_back(C);
991 }
992 }
993
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000994 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000995 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000996 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000997 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000998 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000999 PD->protocol_begin(),
1000 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001001 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001002 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1003 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001004 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1005 InstanceMethods);
1006 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001007 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1008 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001009 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1010 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001011 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1012 Values);
1013
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001014 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001015 // Already created, fix the linkage and update the initializer.
1016 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001017 Entry->setInitializer(Init);
1018 } else {
1019 Entry =
1020 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1021 llvm::GlobalValue::InternalLinkage,
1022 Init,
1023 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1024 &CGM.getModule());
1025 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1026 UsedGlobals.push_back(Entry);
1027 // FIXME: Is this necessary? Why only for protocol?
1028 Entry->setAlignment(4);
1029 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001030
1031 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032}
1033
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001034llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001035 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1036
1037 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001038 // We use the initializer as a marker of whether this is a forward
1039 // reference or not. At module finalization we add the empty
1040 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001041 Entry =
1042 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001043 llvm::GlobalValue::ExternalLinkage,
1044 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001045 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001046 &CGM.getModule());
1047 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1048 UsedGlobals.push_back(Entry);
1049 // FIXME: Is this necessary? Why only for protocol?
1050 Entry->setAlignment(4);
1051 }
1052
1053 return Entry;
1054}
1055
1056/*
1057 struct _objc_protocol_extension {
1058 uint32_t size;
1059 struct objc_method_description_list *optional_instance_methods;
1060 struct objc_method_description_list *optional_class_methods;
1061 struct objc_property_list *instance_properties;
1062 };
1063*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064llvm::Constant *
1065CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1066 const ConstantVector &OptInstanceMethods,
1067 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001068 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001069 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001070 std::vector<llvm::Constant*> Values(4);
1071 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001072 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001073 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1074 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001075 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1076 OptInstanceMethods);
1077 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001078 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1079 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001080 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1081 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001082 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1083 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001084 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001086 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001087 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1088 Values[3]->isNullValue())
1089 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1090
1091 llvm::Constant *Init =
1092 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1093 llvm::GlobalVariable *GV =
1094 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1095 llvm::GlobalValue::InternalLinkage,
1096 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001097 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001098 &CGM.getModule());
1099 // No special section, but goes in llvm.used
1100 UsedGlobals.push_back(GV);
1101
1102 return GV;
1103}
1104
1105/*
1106 struct objc_protocol_list {
1107 struct objc_protocol_list *next;
1108 long count;
1109 Protocol *list[];
1110 };
1111*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001112llvm::Constant *
1113CGObjCMac::EmitProtocolList(const std::string &Name,
1114 ObjCProtocolDecl::protocol_iterator begin,
1115 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001116 std::vector<llvm::Constant*> ProtocolRefs;
1117
Daniel Dunbardbc93372008-08-21 21:57:41 +00001118 for (; begin != end; ++begin)
1119 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001120
1121 // Just return null for empty protocol lists
1122 if (ProtocolRefs.empty())
1123 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1124
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001125 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001126 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1127
1128 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001129 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1131 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1132 Values[2] =
1133 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1134 ProtocolRefs.size()),
1135 ProtocolRefs);
1136
1137 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1138 llvm::GlobalVariable *GV =
1139 new llvm::GlobalVariable(Init->getType(), false,
1140 llvm::GlobalValue::InternalLinkage,
1141 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001142 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001143 &CGM.getModule());
1144 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1145 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1146}
1147
1148/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001149 struct _objc_property {
1150 const char * const name;
1151 const char * const attributes;
1152 };
1153
1154 struct _objc_property_list {
1155 uint32_t entsize; // sizeof (struct _objc_property)
1156 uint32_t prop_count;
1157 struct _objc_property[prop_count];
1158 };
1159*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001160llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1161 const Decl *Container,
1162 const ObjCContainerDecl *OCD,
1163 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001164 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001165 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1166 E = OCD->prop_end(); I != E; ++I) {
1167 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001168 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001169 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001170 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1171 Prop));
1172 }
1173
1174 // Return null for empty list.
1175 if (Properties.empty())
1176 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1177
1178 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001179 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001180 std::vector<llvm::Constant*> Values(3);
1181 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1182 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1183 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1184 Properties.size());
1185 Values[2] = llvm::ConstantArray::get(AT, Properties);
1186 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1187
1188 llvm::GlobalVariable *GV =
1189 new llvm::GlobalVariable(Init->getType(), false,
1190 llvm::GlobalValue::InternalLinkage,
1191 Init,
1192 Name,
1193 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001194 if (ObjCABI == 2)
1195 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001196 // No special section on property lists?
1197 UsedGlobals.push_back(GV);
1198 return llvm::ConstantExpr::getBitCast(GV,
1199 ObjCTypes.PropertyListPtrTy);
1200
1201}
1202
1203/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001204 struct objc_method_description_list {
1205 int count;
1206 struct objc_method_description list[];
1207 };
1208*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001209llvm::Constant *
1210CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1211 std::vector<llvm::Constant*> Desc(2);
1212 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1213 ObjCTypes.SelectorPtrTy);
1214 Desc[1] = GetMethodVarType(MD);
1215 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1216 Desc);
1217}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001218
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001219llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1220 const char *Section,
1221 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001222 // Return null for empty list.
1223 if (Methods.empty())
1224 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1225
1226 std::vector<llvm::Constant*> Values(2);
1227 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1228 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1229 Methods.size());
1230 Values[1] = llvm::ConstantArray::get(AT, Methods);
1231 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1232
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001233 llvm::GlobalVariable *GV =
1234 new llvm::GlobalVariable(Init->getType(), false,
1235 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001236 Init, Name, &CGM.getModule());
1237 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001238 UsedGlobals.push_back(GV);
1239 return llvm::ConstantExpr::getBitCast(GV,
1240 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001241}
1242
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001243/*
1244 struct _objc_category {
1245 char *category_name;
1246 char *class_name;
1247 struct _objc_method_list *instance_methods;
1248 struct _objc_method_list *class_methods;
1249 struct _objc_protocol_list *protocols;
1250 uint32_t size; // <rdar://4585769>
1251 struct _objc_property_list *instance_properties;
1252 };
1253 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001254void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001255 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001256
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001257 // FIXME: This is poor design, the OCD should have a pointer to the
1258 // category decl. Additionally, note that Category can be null for
1259 // the @implementation w/o an @interface case. Sema should just
1260 // create one for us as it does for @implementation so everyone else
1261 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001262 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001263 const ObjCCategoryDecl *Category =
1264 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001265 std::string ExtName(Interface->getNameAsString() + "_" +
1266 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001267
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001268 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1269 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1270 e = OCD->instmeth_end(); i != e; ++i) {
1271 // Instance methods should always be defined.
1272 InstanceMethods.push_back(GetMethodConstant(*i));
1273 }
1274 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1275 e = OCD->classmeth_end(); i != e; ++i) {
1276 // Class methods should always be defined.
1277 ClassMethods.push_back(GetMethodConstant(*i));
1278 }
1279
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001280 std::vector<llvm::Constant*> Values(7);
1281 Values[0] = GetClassName(OCD->getIdentifier());
1282 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001283 Values[2] =
1284 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1285 ExtName,
1286 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001287 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001288 Values[3] =
1289 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1290 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001291 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001292 if (Category) {
1293 Values[4] =
1294 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1295 Category->protocol_begin(),
1296 Category->protocol_end());
1297 } else {
1298 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1299 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001300 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001301
1302 // If there is no category @interface then there can be no properties.
1303 if (Category) {
1304 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001305 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001306 } else {
1307 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1308 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001309
1310 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1311 Values);
1312
1313 llvm::GlobalVariable *GV =
1314 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1315 llvm::GlobalValue::InternalLinkage,
1316 Init,
1317 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1318 &CGM.getModule());
1319 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1320 UsedGlobals.push_back(GV);
1321 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001322}
1323
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001324// FIXME: Get from somewhere?
1325enum ClassFlags {
1326 eClassFlags_Factory = 0x00001,
1327 eClassFlags_Meta = 0x00002,
1328 // <rdr://5142207>
1329 eClassFlags_HasCXXStructors = 0x02000,
1330 eClassFlags_Hidden = 0x20000,
1331 eClassFlags_ABI2_Hidden = 0x00010,
1332 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1333};
1334
1335// <rdr://5142207&4705298&4843145>
1336static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1337 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1338 // FIXME: Support -fvisibility
1339 switch (attr->getVisibility()) {
1340 default:
1341 assert(0 && "Unknown visibility");
1342 return false;
1343 case VisibilityAttr::DefaultVisibility:
1344 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1345 return false;
1346 case VisibilityAttr::HiddenVisibility:
1347 return true;
1348 }
1349 } else {
1350 return false; // FIXME: Support -fvisibility
1351 }
1352}
1353
1354/*
1355 struct _objc_class {
1356 Class isa;
1357 Class super_class;
1358 const char *name;
1359 long version;
1360 long info;
1361 long instance_size;
1362 struct _objc_ivar_list *ivars;
1363 struct _objc_method_list *methods;
1364 struct _objc_cache *cache;
1365 struct _objc_protocol_list *protocols;
1366 // Objective-C 1.0 extensions (<rdr://4585769>)
1367 const char *ivar_layout;
1368 struct _objc_class_ext *ext;
1369 };
1370
1371 See EmitClassExtension();
1372 */
1373void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001374 DefinedSymbols.insert(ID->getIdentifier());
1375
Chris Lattner8ec03f52008-11-24 03:54:41 +00001376 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001377 // FIXME: Gross
1378 ObjCInterfaceDecl *Interface =
1379 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001380 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001381 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001382 Interface->protocol_begin(),
1383 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001384 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001385 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001386 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001387 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001388
1389 // FIXME: Set CXX-structors flag.
1390 if (IsClassHidden(ID->getClassInterface()))
1391 Flags |= eClassFlags_Hidden;
1392
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001393 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1394 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1395 e = ID->instmeth_end(); i != e; ++i) {
1396 // Instance methods should always be defined.
1397 InstanceMethods.push_back(GetMethodConstant(*i));
1398 }
1399 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1400 e = ID->classmeth_end(); i != e; ++i) {
1401 // Class methods should always be defined.
1402 ClassMethods.push_back(GetMethodConstant(*i));
1403 }
1404
1405 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1406 e = ID->propimpl_end(); i != e; ++i) {
1407 ObjCPropertyImplDecl *PID = *i;
1408
1409 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1410 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1411
1412 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1413 if (llvm::Constant *C = GetMethodConstant(MD))
1414 InstanceMethods.push_back(C);
1415 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1416 if (llvm::Constant *C = GetMethodConstant(MD))
1417 InstanceMethods.push_back(C);
1418 }
1419 }
1420
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001421 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001422 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001423 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001424 // Record a reference to the super class.
1425 LazySymbols.insert(Super->getIdentifier());
1426
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001427 Values[ 1] =
1428 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1429 ObjCTypes.ClassPtrTy);
1430 } else {
1431 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1432 }
1433 Values[ 2] = GetClassName(ID->getIdentifier());
1434 // Version is always 0.
1435 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1436 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1437 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001438 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001439 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001440 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001441 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001442 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001443 // cache is always NULL.
1444 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1445 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001446 // FIXME: Set ivar_layout
1447 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001448 Values[11] = EmitClassExtension(ID);
1449 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1450 Values);
1451
1452 llvm::GlobalVariable *GV =
1453 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1454 llvm::GlobalValue::InternalLinkage,
1455 Init,
1456 std::string("\01L_OBJC_CLASS_")+ClassName,
1457 &CGM.getModule());
1458 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1459 UsedGlobals.push_back(GV);
1460 // FIXME: Why?
1461 GV->setAlignment(32);
1462 DefinedClasses.push_back(GV);
1463}
1464
1465llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1466 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001467 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001468 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001469 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001470 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001471
1472 if (IsClassHidden(ID->getClassInterface()))
1473 Flags |= eClassFlags_Hidden;
1474
1475 std::vector<llvm::Constant*> Values(12);
1476 // The isa for the metaclass is the root of the hierarchy.
1477 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1478 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1479 Root = Super;
1480 Values[ 0] =
1481 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1482 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001483 // The super class for the metaclass is emitted as the name of the
1484 // super class. The runtime fixes this up to point to the
1485 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001486 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1487 Values[ 1] =
1488 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1489 ObjCTypes.ClassPtrTy);
1490 } else {
1491 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1492 }
1493 Values[ 2] = GetClassName(ID->getIdentifier());
1494 // Version is always 0.
1495 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1496 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1497 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001498 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001499 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001500 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001501 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001502 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001503 // cache is always NULL.
1504 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1505 Values[ 9] = Protocols;
1506 // ivar_layout for metaclass is always NULL.
1507 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1508 // The class extension is always unused for metaclasses.
1509 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1510 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1511 Values);
1512
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001513 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001514 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001515
1516 // Check for a forward reference.
1517 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1518 if (GV) {
1519 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1520 "Forward metaclass reference has incorrect type.");
1521 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1522 GV->setInitializer(Init);
1523 } else {
1524 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1525 llvm::GlobalValue::InternalLinkage,
1526 Init, Name,
1527 &CGM.getModule());
1528 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1530 UsedGlobals.push_back(GV);
1531 // FIXME: Why?
1532 GV->setAlignment(32);
1533
1534 return GV;
1535}
1536
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001537llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001538 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001539
1540 // FIXME: Should we look these up somewhere other than the
1541 // module. Its a bit silly since we only generate these while
1542 // processing an implementation, so exactly one pointer would work
1543 // if know when we entered/exitted an implementation block.
1544
1545 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001546 // Previously, metaclass with internal linkage may have been defined.
1547 // pass 'true' as 2nd argument so it is returned.
1548 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001549 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1550 "Forward metaclass reference has incorrect type.");
1551 return GV;
1552 } else {
1553 // Generate as an external reference to keep a consistent
1554 // module. This will be patched up when we emit the metaclass.
1555 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1556 llvm::GlobalValue::ExternalLinkage,
1557 0,
1558 Name,
1559 &CGM.getModule());
1560 }
1561}
1562
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001563/*
1564 struct objc_class_ext {
1565 uint32_t size;
1566 const char *weak_ivar_layout;
1567 struct _objc_property_list *properties;
1568 };
1569*/
1570llvm::Constant *
1571CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1572 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001573 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001574
1575 std::vector<llvm::Constant*> Values(3);
1576 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001577 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001578 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001579 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001580 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001581
1582 // Return null if no extension bits are used.
1583 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1584 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1585
1586 llvm::Constant *Init =
1587 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1588 llvm::GlobalVariable *GV =
1589 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1590 llvm::GlobalValue::InternalLinkage,
1591 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001592 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001593 &CGM.getModule());
1594 // No special section, but goes in llvm.used
1595 UsedGlobals.push_back(GV);
1596
1597 return GV;
1598}
1599
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001600/// countInheritedIvars - count number of ivars in class and its super class(s)
1601///
1602static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1603 int count = 0;
1604 if (!OI)
1605 return 0;
1606 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1607 if (SuperClass)
1608 count += countInheritedIvars(SuperClass);
1609 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1610 E = OI->ivar_end(); I != E; ++I)
1611 ++count;
1612 return count;
1613}
1614
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001615/// getInterfaceDeclForIvar - Get the interface declaration node where
1616/// this ivar is declared in.
1617/// FIXME. Ideally, this info should be in the ivar node. But currently
1618/// it is not and prevailing wisdom is that ASTs should not have more
1619/// info than is absolutely needed, even though this info reflects the
1620/// source language.
1621///
1622static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1623 const ObjCInterfaceDecl *OI,
1624 const ObjCIvarDecl *IVD) {
1625 if (!OI)
1626 return 0;
1627 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1628 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1629 E = OI->ivar_end(); I != E; ++I)
1630 if ((*I)->getIdentifier() == IVD->getIdentifier())
1631 return OI;
1632 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1633}
1634
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001635/*
1636 struct objc_ivar {
1637 char *ivar_name;
1638 char *ivar_type;
1639 int ivar_offset;
1640 };
1641
1642 struct objc_ivar_list {
1643 int ivar_count;
1644 struct objc_ivar list[count];
1645 };
1646 */
1647llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001648 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 std::vector<llvm::Constant*> Ivars, Ivar(3);
1650
1651 // When emitting the root class GCC emits ivar entries for the
1652 // actual class structure. It is not clear if we need to follow this
1653 // behavior; for now lets try and get away with not doing it. If so,
1654 // the cleanest solution would be to make up an ObjCInterfaceDecl
1655 // for the class.
1656 if (ForClass)
1657 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001658
1659 ObjCInterfaceDecl *OID =
1660 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1661 const llvm::Type *InterfaceTy =
1662 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001663 const llvm::StructLayout *Layout =
1664 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001665
1666 RecordDecl::field_iterator ifield, pfield;
1667 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001668 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1669 FieldDecl *Field = *ifield;
1670 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1671 getLLVMFieldNo(Field));
1672 if (Field->getIdentifier())
1673 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1674 else
1675 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001676 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001677 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001678 Ivar[1] = GetMethodVarType(TypeStr);
1679 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001680 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001681 }
1682
1683 // Return null for empty list.
1684 if (Ivars.empty())
1685 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1686
1687 std::vector<llvm::Constant*> Values(2);
1688 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1689 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1690 Ivars.size());
1691 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1692 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1693
1694 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1695 "\01L_OBJC_INSTANCE_VARIABLES_");
1696 llvm::GlobalVariable *GV =
1697 new llvm::GlobalVariable(Init->getType(), false,
1698 llvm::GlobalValue::InternalLinkage,
1699 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001700 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001701 &CGM.getModule());
1702 if (ForClass) {
1703 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1704 // FIXME: Why is this only here?
1705 GV->setAlignment(32);
1706 } else {
1707 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1708 }
1709 UsedGlobals.push_back(GV);
1710 return llvm::ConstantExpr::getBitCast(GV,
1711 ObjCTypes.IvarListPtrTy);
1712}
1713
1714/*
1715 struct objc_method {
1716 SEL method_name;
1717 char *method_types;
1718 void *method;
1719 };
1720
1721 struct objc_method_list {
1722 struct objc_method_list *obsolete;
1723 int count;
1724 struct objc_method methods_list[count];
1725 };
1726*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001727
1728/// GetMethodConstant - Return a struct objc_method constant for the
1729/// given method if it has been defined. The result is null if the
1730/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001731llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001732 // FIXME: Use DenseMap::lookup
1733 llvm::Function *Fn = MethodDefinitions[MD];
1734 if (!Fn)
1735 return 0;
1736
1737 std::vector<llvm::Constant*> Method(3);
1738 Method[0] =
1739 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1740 ObjCTypes.SelectorPtrTy);
1741 Method[1] = GetMethodVarType(MD);
1742 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1743 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1744}
1745
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001746llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1747 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001748 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001749 // Return null for empty list.
1750 if (Methods.empty())
1751 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1752
1753 std::vector<llvm::Constant*> Values(3);
1754 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1755 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1756 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1757 Methods.size());
1758 Values[2] = llvm::ConstantArray::get(AT, Methods);
1759 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1760
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001761 llvm::GlobalVariable *GV =
1762 new llvm::GlobalVariable(Init->getType(), false,
1763 llvm::GlobalValue::InternalLinkage,
1764 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001765 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001766 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001767 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001768 UsedGlobals.push_back(GV);
1769 return llvm::ConstantExpr::getBitCast(GV,
1770 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001771}
1772
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001773llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001774 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001775 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001776 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001777
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001778 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001779 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001780 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001781 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001782 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001783 llvm::GlobalValue::InternalLinkage,
1784 Name,
1785 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001786 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001787
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001788 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001789}
1790
1791llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001792 // Abuse this interface function as a place to finalize.
1793 FinishModule();
1794
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001795 return NULL;
1796}
1797
Daniel Dunbar49f66022008-09-24 03:38:44 +00001798llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1799 return ObjCTypes.GetPropertyFn;
1800}
1801
1802llvm::Function *CGObjCMac::GetPropertySetFunction() {
1803 return ObjCTypes.SetPropertyFn;
1804}
1805
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001806llvm::Function *CGObjCMac::EnumerationMutationFunction()
1807{
1808 return ObjCTypes.EnumerationMutationFn;
1809}
1810
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001811/*
1812
1813Objective-C setjmp-longjmp (sjlj) Exception Handling
1814--
1815
1816The basic framework for a @try-catch-finally is as follows:
1817{
1818 objc_exception_data d;
1819 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001820 bool _call_try_exit = true;
1821
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001822 objc_exception_try_enter(&d);
1823 if (!setjmp(d.jmp_buf)) {
1824 ... try body ...
1825 } else {
1826 // exception path
1827 id _caught = objc_exception_extract(&d);
1828
1829 // enter new try scope for handlers
1830 if (!setjmp(d.jmp_buf)) {
1831 ... match exception and execute catch blocks ...
1832
1833 // fell off end, rethrow.
1834 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001835 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001836 } else {
1837 // exception in catch block
1838 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001839 _call_try_exit = false;
1840 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001841 }
1842 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001843 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001844
1845finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001846 if (_call_try_exit)
1847 objc_exception_try_exit(&d);
1848
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001849 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001850 ... dispatch to finally destination ...
1851
1852finally_rethrow:
1853 objc_exception_throw(_rethrow);
1854
1855finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001856}
1857
1858This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001859uses _rethrow to determine if objc_exception_try_exit should be called
1860and if the object should be rethrown. This breaks in the face of
1861throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001862
1863We specialize this framework for a few particular circumstances:
1864
1865 - If there are no catch blocks, then we avoid emitting the second
1866 exception handling context.
1867
1868 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1869 e)) we avoid emitting the code to rethrow an uncaught exception.
1870
1871 - FIXME: If there is no @finally block we can do a few more
1872 simplifications.
1873
1874Rethrows and Jumps-Through-Finally
1875--
1876
1877Support for implicit rethrows and jumping through the finally block is
1878handled by storing the current exception-handling context in
1879ObjCEHStack.
1880
Daniel Dunbar898d5082008-09-30 01:06:03 +00001881In order to implement proper @finally semantics, we support one basic
1882mechanism for jumping through the finally block to an arbitrary
1883destination. Constructs which generate exits from a @try or @catch
1884block use this mechanism to implement the proper semantics by chaining
1885jumps, as necessary.
1886
1887This mechanism works like the one used for indirect goto: we
1888arbitrarily assign an ID to each destination and store the ID for the
1889destination in a variable prior to entering the finally block. At the
1890end of the finally block we simply create a switch to the proper
1891destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001892
1893Code gen for @synchronized(expr) stmt;
1894Effectively generating code for:
1895objc_sync_enter(expr);
1896@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001897*/
1898
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001899void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1900 const Stmt &S) {
1901 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001902 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001903 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001904 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001905 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1906 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1907 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001908
1909 // Push an EH context entry, used for handling rethrows and jumps
1910 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001911 CGF.PushCleanupBlock(FinallyBlock);
1912
Anders Carlsson273558f2009-02-07 21:37:21 +00001913 CGF.ObjCEHValueStack.push_back(0);
1914
Daniel Dunbar898d5082008-09-30 01:06:03 +00001915 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001916 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1917 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001918 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1919 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001920 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1921 "_call_try_exit");
1922 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1923
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001924 if (!isTry) {
1925 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001926 llvm::Value *Arg = CGF.EmitScalarExpr(
1927 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1928 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1929 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001930 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001931
1932 // Enter a new try block and call setjmp.
1933 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1934 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1935 "jmpbufarray");
1936 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1937 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1938 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001939
Daniel Dunbar55e87422008-11-11 02:29:29 +00001940 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1941 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001942 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001943 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001944
1945 // Emit the @try block.
1946 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001947 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1948 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001949 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001950
1951 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001952 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001953
1954 // Retrieve the exception object. We may emit multiple blocks but
1955 // nothing can cross this so the value is already in SSA form.
1956 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1957 ExceptionData,
1958 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001959 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001960 if (!isTry)
1961 {
1962 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001963 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001964 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001965 }
1966 else if (const ObjCAtCatchStmt* CatchStmt =
1967 cast<ObjCAtTryStmt>(S).getCatchStmts())
1968 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001969 // Enter a new exception try block (in case a @catch block throws
1970 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001971 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001972
Anders Carlsson80f25672008-09-09 17:59:25 +00001973 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1974 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001975 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001976
Daniel Dunbar55e87422008-11-11 02:29:29 +00001977 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1978 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001979 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001980
1981 CGF.EmitBlock(CatchBlock);
1982
Daniel Dunbar55e40722008-09-27 07:03:52 +00001983 // Handle catch list. As a special case we check if everything is
1984 // matched and avoid generating code for falling off the end if
1985 // so.
1986 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001987 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001988 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001989
Anders Carlssondde0a942008-09-11 09:15:33 +00001990 const DeclStmt *CatchParam =
1991 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001992 const VarDecl *VD = 0;
1993 const PointerType *PT = 0;
1994
Anders Carlsson80f25672008-09-09 17:59:25 +00001995 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001996 if (!CatchParam) {
1997 AllMatched = true;
1998 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001999 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00002000 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002001
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002002 // catch(id e) always matches.
2003 // FIXME: For the time being we also match id<X>; this should
2004 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002005 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002006 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002007 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002008 }
2009
Daniel Dunbar55e40722008-09-27 07:03:52 +00002010 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002011 if (CatchParam) {
2012 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002013 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002014 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002015 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002016
Anders Carlssondde0a942008-09-11 09:15:33 +00002017 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002018 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002019 break;
2020 }
2021
Daniel Dunbar129271a2008-09-27 07:36:24 +00002022 assert(PT && "Unexpected non-pointer type in @catch");
2023 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002024 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002025 assert(ObjCType && "Catch parameter must have Objective-C type!");
2026
2027 // Check if the @catch block matches the exception object.
2028 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2029
Anders Carlsson80f25672008-09-09 17:59:25 +00002030 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2031 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002032
Daniel Dunbar55e87422008-11-11 02:29:29 +00002033 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002034
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002035 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002036 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002037
2038 // Emit the @catch block.
2039 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002040 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002041 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002042
2043 llvm::Value *Tmp =
2044 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2045 "tmp");
2046 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002047
2048 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002049 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002050
2051 CGF.EmitBlock(NextCatchBlock);
2052 }
2053
Daniel Dunbar55e40722008-09-27 07:03:52 +00002054 if (!AllMatched) {
2055 // None of the handlers caught the exception, so store it to be
2056 // rethrown at the end of the @finally block.
2057 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002058 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002059 }
2060
2061 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002062 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002063 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2064 ExceptionData),
2065 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002066 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002067 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002068 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002069 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002070 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002071 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002072 }
2073
Daniel Dunbar898d5082008-09-30 01:06:03 +00002074 // Pop the exception-handling stack entry. It is important to do
2075 // this now, because the code in the @finally block is not in this
2076 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002077 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2078
Anders Carlsson273558f2009-02-07 21:37:21 +00002079 CGF.ObjCEHValueStack.pop_back();
2080
Anders Carlsson80f25672008-09-09 17:59:25 +00002081 // Emit the @finally block.
2082 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002083 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2084
2085 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2086
2087 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002088 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002089
2090 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002091 if (isTry) {
2092 if (const ObjCAtFinallyStmt* FinallyStmt =
2093 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2094 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2095 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002096 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002097 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002098 // For @synchronized, call objc_sync_enter(sync.expr)
2099 llvm::Value *Arg = CGF.EmitScalarExpr(
2100 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2101 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2102 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2103 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002104
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002105 // Emit the switch block
2106 if (Info.SwitchBlock)
2107 CGF.EmitBlock(Info.SwitchBlock);
2108 if (Info.EndBlock)
2109 CGF.EmitBlock(Info.EndBlock);
2110
Daniel Dunbar898d5082008-09-30 01:06:03 +00002111 CGF.EmitBlock(FinallyRethrow);
2112 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2113 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002114 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002115
2116 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002117}
2118
2119void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002120 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002121 llvm::Value *ExceptionAsObject;
2122
2123 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2124 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2125 ExceptionAsObject =
2126 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2127 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002128 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002129 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002130 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002131 }
2132
2133 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002134 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002135
2136 // Clear the insertion point to indicate we are in unreachable code.
2137 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002138}
2139
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002140/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002141/// object: objc_read_weak (id *src)
2142///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002143llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002144 llvm::Value *AddrWeakObj)
2145{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002146 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002147 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002148 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002149 return read_weak;
2150}
2151
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002152/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2153/// objc_assign_weak (id src, id *dst)
2154///
2155void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2156 llvm::Value *src, llvm::Value *dst)
2157{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002158 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2159 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002160 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2161 src, dst, "weakassign");
2162 return;
2163}
2164
Fariborz Jahanian58626502008-11-19 00:59:10 +00002165/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2166/// objc_assign_global (id src, id *dst)
2167///
2168void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2169 llvm::Value *src, llvm::Value *dst)
2170{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002171 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2172 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002173 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2174 src, dst, "globalassign");
2175 return;
2176}
2177
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002178/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2179/// objc_assign_ivar (id src, id *dst)
2180///
2181void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2182 llvm::Value *src, llvm::Value *dst)
2183{
2184 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2185 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2186 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2187 src, dst, "assignivar");
2188 return;
2189}
2190
Fariborz Jahanian58626502008-11-19 00:59:10 +00002191/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2192/// objc_assign_strongCast (id src, id *dst)
2193///
2194void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2195 llvm::Value *src, llvm::Value *dst)
2196{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002197 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2198 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002199 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2200 src, dst, "weakassign");
2201 return;
2202}
2203
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002204/// EmitObjCValueForIvar - Code Gen for ivar reference.
2205///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002206LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2207 QualType ObjectTy,
2208 llvm::Value *BaseValue,
2209 const ObjCIvarDecl *Ivar,
2210 const FieldDecl *Field,
2211 unsigned CVRQualifiers) {
2212 if (Ivar->isBitField())
2213 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2214 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002215 // TODO: Add a special case for isa (index 0)
2216 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2217 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002218 LValue LV = LValue::MakeAddr(V,
2219 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2220 LValue::SetObjCIvar(LV, true);
2221 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002222}
2223
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002224llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2225 ObjCInterfaceDecl *Interface,
2226 const ObjCIvarDecl *Ivar) {
2227 const llvm::Type *InterfaceLTy =
2228 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2229 const llvm::StructLayout *Layout =
2230 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2231 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2232 uint64_t Offset =
2233 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2234
2235 return llvm::ConstantInt::get(
2236 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2237 Offset);
2238}
2239
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002240/* *** Private Interface *** */
2241
2242/// EmitImageInfo - Emit the image info marker used to encode some module
2243/// level information.
2244///
2245/// See: <rdr://4810609&4810587&4810587>
2246/// struct IMAGE_INFO {
2247/// unsigned version;
2248/// unsigned flags;
2249/// };
2250enum ImageInfoFlags {
2251 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2252 eImageInfo_GarbageCollected = (1 << 1),
2253 eImageInfo_GCOnly = (1 << 2)
2254};
2255
2256void CGObjCMac::EmitImageInfo() {
2257 unsigned version = 0; // Version is unused?
2258 unsigned flags = 0;
2259
2260 // FIXME: Fix and continue?
2261 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2262 flags |= eImageInfo_GarbageCollected;
2263 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2264 flags |= eImageInfo_GCOnly;
2265
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002266 // Emitted as int[2];
2267 llvm::Constant *values[2] = {
2268 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2269 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2270 };
2271 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002272 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002273 new llvm::GlobalVariable(AT, true,
2274 llvm::GlobalValue::InternalLinkage,
2275 llvm::ConstantArray::get(AT, values, 2),
2276 "\01L_OBJC_IMAGE_INFO",
2277 &CGM.getModule());
2278
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002279 if (ObjCABI == 1) {
2280 GV->setSection("__OBJC, __image_info,regular");
2281 } else {
2282 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2283 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002284
2285 UsedGlobals.push_back(GV);
2286}
2287
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002288
2289// struct objc_module {
2290// unsigned long version;
2291// unsigned long size;
2292// const char *name;
2293// Symtab symtab;
2294// };
2295
2296// FIXME: Get from somewhere
2297static const int ModuleVersion = 7;
2298
2299void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002300 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002301
2302 std::vector<llvm::Constant*> Values(4);
2303 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2304 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002305 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002306 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002307 Values[3] = EmitModuleSymbols();
2308
2309 llvm::GlobalVariable *GV =
2310 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2311 llvm::GlobalValue::InternalLinkage,
2312 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2313 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002314 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002315 &CGM.getModule());
2316 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2317 UsedGlobals.push_back(GV);
2318}
2319
2320llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002321 unsigned NumClasses = DefinedClasses.size();
2322 unsigned NumCategories = DefinedCategories.size();
2323
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002324 // Return null if no symbols were defined.
2325 if (!NumClasses && !NumCategories)
2326 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2327
2328 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2330 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2331 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2332 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2333
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002334 // The runtime expects exactly the list of defined classes followed
2335 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002337 for (unsigned i=0; i<NumClasses; i++)
2338 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2339 ObjCTypes.Int8PtrTy);
2340 for (unsigned i=0; i<NumCategories; i++)
2341 Symbols[NumClasses + i] =
2342 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2343 ObjCTypes.Int8PtrTy);
2344
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002346 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002347 NumClasses + NumCategories),
2348 Symbols);
2349
2350 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2351
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002352 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002353 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002354 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002356 "\01L_OBJC_SYMBOLS",
2357 &CGM.getModule());
2358 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2359 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002360 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2361}
2362
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002363llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002364 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002365 LazySymbols.insert(ID->getIdentifier());
2366
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002367 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2368
2369 if (!Entry) {
2370 llvm::Constant *Casted =
2371 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2372 ObjCTypes.ClassPtrTy);
2373 Entry =
2374 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2375 llvm::GlobalValue::InternalLinkage,
2376 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2377 &CGM.getModule());
2378 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2379 UsedGlobals.push_back(Entry);
2380 }
2381
2382 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002383}
2384
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002385llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002386 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2387
2388 if (!Entry) {
2389 llvm::Constant *Casted =
2390 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2391 ObjCTypes.SelectorPtrTy);
2392 Entry =
2393 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2394 llvm::GlobalValue::InternalLinkage,
2395 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2396 &CGM.getModule());
2397 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2398 UsedGlobals.push_back(Entry);
2399 }
2400
2401 return Builder.CreateLoad(Entry, false, "tmp");
2402}
2403
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002404llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002405 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002406
2407 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002408 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002409 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002410 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002411 llvm::GlobalValue::InternalLinkage,
2412 C, "\01L_OBJC_CLASS_NAME_",
2413 &CGM.getModule());
2414 Entry->setSection("__TEXT,__cstring,cstring_literals");
2415 UsedGlobals.push_back(Entry);
2416 }
2417
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002418 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002419}
2420
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002421llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002422 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2423
2424 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002425 // FIXME: Avoid std::string copying.
2426 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002427 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002428 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002429 llvm::GlobalValue::InternalLinkage,
2430 C, "\01L_OBJC_METH_VAR_NAME_",
2431 &CGM.getModule());
2432 Entry->setSection("__TEXT,__cstring,cstring_literals");
2433 UsedGlobals.push_back(Entry);
2434 }
2435
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002436 return getConstantGEP(Entry, 0, 0);
2437}
2438
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002439// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002440llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2442}
2443
2444// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002445llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2447}
2448
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002449llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002451
2452 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002454 Entry =
2455 new llvm::GlobalVariable(C->getType(), false,
2456 llvm::GlobalValue::InternalLinkage,
2457 C, "\01L_OBJC_METH_VAR_TYPE_",
2458 &CGM.getModule());
2459 Entry->setSection("__TEXT,__cstring,cstring_literals");
2460 UsedGlobals.push_back(Entry);
2461 }
2462
2463 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002464}
2465
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002466// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002467llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002468 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002469 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2470 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471 return GetMethodVarType(TypeStr);
2472}
2473
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002474// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002475llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002476 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2477
2478 if (!Entry) {
2479 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2480 Entry =
2481 new llvm::GlobalVariable(C->getType(), false,
2482 llvm::GlobalValue::InternalLinkage,
2483 C, "\01L_OBJC_PROP_NAME_ATTR_",
2484 &CGM.getModule());
2485 Entry->setSection("__TEXT,__cstring,cstring_literals");
2486 UsedGlobals.push_back(Entry);
2487 }
2488
2489 return getConstantGEP(Entry, 0, 0);
2490}
2491
2492// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002493// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002494llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002495 const Decl *Container) {
2496 std::string TypeStr;
2497 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002498 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2499}
2500
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002501void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2502 const ObjCContainerDecl *CD,
2503 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002504 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002505 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002506 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002507 assert (CD && "Missing container decl in GetNameForMethod");
2508 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002509 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2510 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002511 NameOut += ' ';
2512 NameOut += D->getSelector().getAsString();
2513 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002514}
2515
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002516/// GetFirstIvarInRecord - This routine returns the record for the
2517/// implementation of the fiven class OID. It also returns field
2518/// corresponding to the first ivar in the class in FIV. It also
2519/// returns the one before the first ivar.
2520///
2521const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2522 const ObjCInterfaceDecl *OID,
2523 RecordDecl::field_iterator &FIV,
2524 RecordDecl::field_iterator &PIV) {
2525 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2526 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2527 RecordDecl::field_iterator ifield = RD->field_begin();
2528 RecordDecl::field_iterator pfield = RD->field_end();
2529 while (countSuperClassIvars-- > 0) {
2530 pfield = ifield;
2531 ++ifield;
2532 }
2533 FIV = ifield;
2534 PIV = pfield;
2535 return RD;
2536}
2537
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002538void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002539 EmitModuleInfo();
2540
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002541 // Emit the dummy bodies for any protocols which were referenced but
2542 // never defined.
2543 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2544 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2545 if (i->second->hasInitializer())
2546 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002547
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002548 std::vector<llvm::Constant*> Values(5);
2549 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2550 Values[1] = GetClassName(i->first);
2551 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2552 Values[3] = Values[4] =
2553 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2554 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2555 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2556 Values));
2557 }
2558
2559 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002560 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002561 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002562 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002563 }
2564
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002565 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002566 llvm::GlobalValue *GV =
2567 new llvm::GlobalVariable(AT, false,
2568 llvm::GlobalValue::AppendingLinkage,
2569 llvm::ConstantArray::get(AT, Used),
2570 "llvm.used",
2571 &CGM.getModule());
2572
2573 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002574
2575 // Add assembler directives to add lazy undefined symbol references
2576 // for classes which are referenced but not defined. This is
2577 // important for correct linker interaction.
2578
2579 // FIXME: Uh, this isn't particularly portable.
2580 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002581
2582 if (!CGM.getModule().getModuleInlineAsm().empty())
2583 s << "\n";
2584
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002585 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2586 e = LazySymbols.end(); i != e; ++i) {
2587 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2588 }
2589 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2590 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002591 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002592 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2593 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002594
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002595 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002596}
2597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002598CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002599 : CGObjCCommonMac(cgm),
2600 ObjCTypes(cgm)
2601{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002602 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002603 ObjCABI = 2;
2604}
2605
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002606/* *** */
2607
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002608ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2609: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002610{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002611 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2612 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002613
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002614 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002615 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002616 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002617 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2618
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002619 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002620 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002621 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002622
2623 // FIXME: It would be nice to unify this with the opaque type, so
2624 // that the IR comes out a bit cleaner.
2625 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2626 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002627
2628 // I'm not sure I like this. The implicit coordination is a bit
2629 // gross. We should solve this in a reasonable fashion because this
2630 // is a pretty common task (match some runtime data structure with
2631 // an LLVM data structure).
2632
2633 // FIXME: This is leaked.
2634 // FIXME: Merge with rewriter code?
2635
2636 // struct _objc_super {
2637 // id self;
2638 // Class cls;
2639 // }
2640 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2641 SourceLocation(),
2642 &Ctx.Idents.get("_objc_super"));
2643 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2644 Ctx.getObjCIdType(), 0, false));
2645 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2646 Ctx.getObjCClassType(), 0, false));
2647 RD->completeDefinition(Ctx);
2648
2649 SuperCTy = Ctx.getTagDeclType(RD);
2650 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2651
2652 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002653 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2654
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002655 // struct _prop_t {
2656 // char *name;
2657 // char *attributes;
2658 // }
2659 PropertyTy = llvm::StructType::get(Int8PtrTy,
2660 Int8PtrTy,
2661 NULL);
2662 CGM.getModule().addTypeName("struct._prop_t",
2663 PropertyTy);
2664
2665 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002666 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002667 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002668 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002669 // }
2670 PropertyListTy = llvm::StructType::get(IntTy,
2671 IntTy,
2672 llvm::ArrayType::get(PropertyTy, 0),
2673 NULL);
2674 CGM.getModule().addTypeName("struct._prop_list_t",
2675 PropertyListTy);
2676 // struct _prop_list_t *
2677 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2678
2679 // struct _objc_method {
2680 // SEL _cmd;
2681 // char *method_type;
2682 // char *_imp;
2683 // }
2684 MethodTy = llvm::StructType::get(SelectorPtrTy,
2685 Int8PtrTy,
2686 Int8PtrTy,
2687 NULL);
2688 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002689
2690 // struct _objc_cache *
2691 CacheTy = llvm::OpaqueType::get();
2692 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2693 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002694
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002695 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002696
2697 QualType IdType = Ctx.getObjCIdType();
2698 QualType SelType = Ctx.getObjCSelType();
2699 llvm::SmallVector<QualType,16> Params;
2700 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002701
2702 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002703 Params.push_back(IdType);
2704 Params.push_back(SelType);
2705 Params.push_back(Ctx.LongTy);
2706 Params.push_back(Ctx.BoolTy);
2707 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2708 false);
2709 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002710
2711 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2712 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002713 Params.push_back(IdType);
2714 Params.push_back(SelType);
2715 Params.push_back(Ctx.LongTy);
2716 Params.push_back(IdType);
2717 Params.push_back(Ctx.BoolTy);
2718 Params.push_back(Ctx.BoolTy);
2719 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2720 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2721
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002722 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002723
2724 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002725 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002726 Params.push_back(IdType);
2727 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2728 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2729 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002730
2731 // gc's API
2732 // id objc_read_weak (id *)
2733 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002734 Params.push_back(Ctx.getPointerType(IdType));
2735 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2736 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2737
2738 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002739 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002740 Params.push_back(IdType);
2741 Params.push_back(Ctx.getPointerType(IdType));
2742
2743 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2744 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2745 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2746 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2747 GcAssignStrongCastFn =
2748 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002749}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002750
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002751ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2752 : ObjCCommonTypesHelper(cgm)
2753{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002754 // struct _objc_method_description {
2755 // SEL name;
2756 // char *types;
2757 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002758 MethodDescriptionTy =
2759 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002760 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002761 NULL);
2762 CGM.getModule().addTypeName("struct._objc_method_description",
2763 MethodDescriptionTy);
2764
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002765 // struct _objc_method_description_list {
2766 // int count;
2767 // struct _objc_method_description[1];
2768 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002769 MethodDescriptionListTy =
2770 llvm::StructType::get(IntTy,
2771 llvm::ArrayType::get(MethodDescriptionTy, 0),
2772 NULL);
2773 CGM.getModule().addTypeName("struct._objc_method_description_list",
2774 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002775
2776 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002777 MethodDescriptionListPtrTy =
2778 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2779
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002780 // Protocol description structures
2781
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002782 // struct _objc_protocol_extension {
2783 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2784 // struct _objc_method_description_list *optional_instance_methods;
2785 // struct _objc_method_description_list *optional_class_methods;
2786 // struct _objc_property_list *instance_properties;
2787 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002788 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002789 llvm::StructType::get(IntTy,
2790 MethodDescriptionListPtrTy,
2791 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002792 PropertyListPtrTy,
2793 NULL);
2794 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2795 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002796
2797 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002798 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2799
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002800 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002801
2802 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2803 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2804
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002805 const llvm::Type *T =
2806 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2807 LongTy,
2808 llvm::ArrayType::get(ProtocolTyHolder, 0),
2809 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002810 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2811
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002812 // struct _objc_protocol {
2813 // struct _objc_protocol_extension *isa;
2814 // char *protocol_name;
2815 // struct _objc_protocol **_objc_protocol_list;
2816 // struct _objc_method_description_list *instance_methods;
2817 // struct _objc_method_description_list *class_methods;
2818 // }
2819 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002820 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002821 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2822 MethodDescriptionListPtrTy,
2823 MethodDescriptionListPtrTy,
2824 NULL);
2825 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2826
2827 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2828 CGM.getModule().addTypeName("struct._objc_protocol_list",
2829 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002830 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002831 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2832
2833 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002834 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002835 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002836
2837 // Class description structures
2838
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002839 // struct _objc_ivar {
2840 // char *ivar_name;
2841 // char *ivar_type;
2842 // int ivar_offset;
2843 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002844 IvarTy = llvm::StructType::get(Int8PtrTy,
2845 Int8PtrTy,
2846 IntTy,
2847 NULL);
2848 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2849
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002850 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002851 IvarListTy = llvm::OpaqueType::get();
2852 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2853 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2854
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002855 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002856 MethodListTy = llvm::OpaqueType::get();
2857 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2858 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2859
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002860 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002861 ClassExtensionTy =
2862 llvm::StructType::get(IntTy,
2863 Int8PtrTy,
2864 PropertyListPtrTy,
2865 NULL);
2866 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2867 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2868
2869 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2870
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002871 // struct _objc_class {
2872 // Class isa;
2873 // Class super_class;
2874 // char *name;
2875 // long version;
2876 // long info;
2877 // long instance_size;
2878 // struct _objc_ivar_list *ivars;
2879 // struct _objc_method_list *methods;
2880 // struct _objc_cache *cache;
2881 // struct _objc_protocol_list *protocols;
2882 // char *ivar_layout;
2883 // struct _objc_class_ext *ext;
2884 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002885 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2886 llvm::PointerType::getUnqual(ClassTyHolder),
2887 Int8PtrTy,
2888 LongTy,
2889 LongTy,
2890 LongTy,
2891 IvarListPtrTy,
2892 MethodListPtrTy,
2893 CachePtrTy,
2894 ProtocolListPtrTy,
2895 Int8PtrTy,
2896 ClassExtensionPtrTy,
2897 NULL);
2898 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2899
2900 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2901 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2902 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2903
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002904 // struct _objc_category {
2905 // char *category_name;
2906 // char *class_name;
2907 // struct _objc_method_list *instance_method;
2908 // struct _objc_method_list *class_method;
2909 // uint32_t size; // sizeof(struct _objc_category)
2910 // struct _objc_property_list *instance_properties;// category's @property
2911 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002912 CategoryTy = llvm::StructType::get(Int8PtrTy,
2913 Int8PtrTy,
2914 MethodListPtrTy,
2915 MethodListPtrTy,
2916 ProtocolListPtrTy,
2917 IntTy,
2918 PropertyListPtrTy,
2919 NULL);
2920 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2921
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922 // Global metadata structures
2923
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002924 // struct _objc_symtab {
2925 // long sel_ref_cnt;
2926 // SEL *refs;
2927 // short cls_def_cnt;
2928 // short cat_def_cnt;
2929 // char *defs[cls_def_cnt + cat_def_cnt];
2930 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002931 SymtabTy = llvm::StructType::get(LongTy,
2932 SelectorPtrTy,
2933 ShortTy,
2934 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002935 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002936 NULL);
2937 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2938 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2939
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002940 // struct _objc_module {
2941 // long version;
2942 // long size; // sizeof(struct _objc_module)
2943 // char *name;
2944 // struct _objc_symtab* symtab;
2945 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002946 ModuleTy =
2947 llvm::StructType::get(LongTy,
2948 LongTy,
2949 Int8PtrTy,
2950 SymtabPtrTy,
2951 NULL);
2952 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002953
Daniel Dunbar49f66022008-09-24 03:38:44 +00002954 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002955
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002956 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002957 std::vector<const llvm::Type*> Params;
2958 Params.push_back(ObjectPtrTy);
2959 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002960 MessageSendFn =
2961 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2962 Params,
2963 true),
2964 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002965
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002966 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002967 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002968 Params.push_back(ObjectPtrTy);
2969 Params.push_back(SelectorPtrTy);
2970 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002971 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2972 Params,
2973 true),
2974 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002975
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002976 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002977 Params.clear();
2978 Params.push_back(ObjectPtrTy);
2979 Params.push_back(SelectorPtrTy);
2980 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002981 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002982 MessageSendFpretFn =
2983 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2984 Params,
2985 true),
2986 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002987
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002988 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002989 Params.clear();
2990 Params.push_back(SuperPtrTy);
2991 Params.push_back(SelectorPtrTy);
2992 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002993 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2994 Params,
2995 true),
2996 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002997
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002998 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2999 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003000 Params.clear();
3001 Params.push_back(Int8PtrTy);
3002 Params.push_back(SuperPtrTy);
3003 Params.push_back(SelectorPtrTy);
3004 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003005 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3006 Params,
3007 true),
3008 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003009
3010 // There is no objc_msgSendSuper_fpret? How can that work?
3011 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003012
Anders Carlsson124526b2008-09-09 10:10:21 +00003013 // FIXME: This is the size of the setjmp buffer and should be
3014 // target specific. 18 is what's used on 32-bit X86.
3015 uint64_t SetJmpBufferSize = 18;
3016
3017 // Exceptions
3018 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003019 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003020
3021 ExceptionDataTy =
3022 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3023 SetJmpBufferSize),
3024 StackPtrTy, NULL);
3025 CGM.getModule().addTypeName("struct._objc_exception_data",
3026 ExceptionDataTy);
3027
3028 Params.clear();
3029 Params.push_back(ObjectPtrTy);
3030 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003031 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3032 Params,
3033 false),
3034 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00003035
3036 Params.clear();
3037 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3038 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003039 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3040 Params,
3041 false),
3042 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003043 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003044 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3045 Params,
3046 false),
3047 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003048 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003049 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3050 Params,
3051 false),
3052 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003053
3054 Params.clear();
3055 Params.push_back(ClassPtrTy);
3056 Params.push_back(ObjectPtrTy);
3057 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003058 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3059 Params,
3060 false),
3061 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003062
3063 // synchronized APIs
3064 // void objc_sync_enter (id)
3065 Params.clear();
3066 Params.push_back(ObjectPtrTy);
3067 SyncEnterFn =
3068 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3069 Params,
3070 false),
3071 "objc_sync_enter");
3072 // void objc_sync_exit (id)
3073 SyncExitFn =
3074 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3075 Params,
3076 false),
3077 "objc_sync_exit");
3078
Anders Carlsson124526b2008-09-09 10:10:21 +00003079
3080 Params.clear();
3081 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3082 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003083 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3084 Params,
3085 false),
3086 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003087
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003088}
3089
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003090ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003091: ObjCCommonTypesHelper(cgm)
3092{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003093 // struct _method_list_t {
3094 // uint32_t entsize; // sizeof(struct _objc_method)
3095 // uint32_t method_count;
3096 // struct _objc_method method_list[method_count];
3097 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003098 MethodListnfABITy = llvm::StructType::get(IntTy,
3099 IntTy,
3100 llvm::ArrayType::get(MethodTy, 0),
3101 NULL);
3102 CGM.getModule().addTypeName("struct.__method_list_t",
3103 MethodListnfABITy);
3104 // struct method_list_t *
3105 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003106
3107 // struct _protocol_t {
3108 // id isa; // NULL
3109 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003110 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003111 // const struct method_list_t * const instance_methods;
3112 // const struct method_list_t * const class_methods;
3113 // const struct method_list_t *optionalInstanceMethods;
3114 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003115 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003116 // const uint32_t size; // sizeof(struct _protocol_t)
3117 // const uint32_t flags; // = 0
3118 // }
3119
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003120 // Holder for struct _protocol_list_t *
3121 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3122
3123 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3124 Int8PtrTy,
3125 llvm::PointerType::getUnqual(
3126 ProtocolListTyHolder),
3127 MethodListnfABIPtrTy,
3128 MethodListnfABIPtrTy,
3129 MethodListnfABIPtrTy,
3130 MethodListnfABIPtrTy,
3131 PropertyListPtrTy,
3132 IntTy,
3133 IntTy,
3134 NULL);
3135 CGM.getModule().addTypeName("struct._protocol_t",
3136 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003137
3138 // struct _protocol_t*
3139 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003140
Fariborz Jahanianda320092009-01-29 19:24:30 +00003141 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003142 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003143 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003144 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003145 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3146 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003147 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003148 NULL);
3149 CGM.getModule().addTypeName("struct._objc_protocol_list",
3150 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003151 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3152 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003153
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003154 // struct _objc_protocol_list*
3155 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003156
3157 // struct _ivar_t {
3158 // unsigned long int *offset; // pointer to ivar offset location
3159 // char *name;
3160 // char *type;
3161 // uint32_t alignment;
3162 // uint32_t size;
3163 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003164 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3165 Int8PtrTy,
3166 Int8PtrTy,
3167 IntTy,
3168 IntTy,
3169 NULL);
3170 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3171
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003172 // struct _ivar_list_t {
3173 // uint32 entsize; // sizeof(struct _ivar_t)
3174 // uint32 count;
3175 // struct _iver_t list[count];
3176 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003177 IvarListnfABITy = llvm::StructType::get(IntTy,
3178 IntTy,
3179 llvm::ArrayType::get(
3180 IvarnfABITy, 0),
3181 NULL);
3182 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3183
3184 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003185
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003186 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003187 // uint32_t const flags;
3188 // uint32_t const instanceStart;
3189 // uint32_t const instanceSize;
3190 // uint32_t const reserved; // only when building for 64bit targets
3191 // const uint8_t * const ivarLayout;
3192 // const char *const name;
3193 // const struct _method_list_t * const baseMethods;
3194 // const struct _objc_protocol_list *const baseProtocols;
3195 // const struct _ivar_list_t *const ivars;
3196 // const uint8_t * const weakIvarLayout;
3197 // const struct _prop_list_t * const properties;
3198 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003199
3200 // FIXME. Add 'reserved' field in 64bit abi mode!
3201 ClassRonfABITy = llvm::StructType::get(IntTy,
3202 IntTy,
3203 IntTy,
3204 Int8PtrTy,
3205 Int8PtrTy,
3206 MethodListnfABIPtrTy,
3207 ProtocolListnfABIPtrTy,
3208 IvarListnfABIPtrTy,
3209 Int8PtrTy,
3210 PropertyListPtrTy,
3211 NULL);
3212 CGM.getModule().addTypeName("struct._class_ro_t",
3213 ClassRonfABITy);
3214
3215 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3216 std::vector<const llvm::Type*> Params;
3217 Params.push_back(ObjectPtrTy);
3218 Params.push_back(SelectorPtrTy);
3219 ImpnfABITy = llvm::PointerType::getUnqual(
3220 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3221
3222 // struct _class_t {
3223 // struct _class_t *isa;
3224 // struct _class_t * const superclass;
3225 // void *cache;
3226 // IMP *vtable;
3227 // struct class_ro_t *ro;
3228 // }
3229
3230 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3231 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3232 llvm::PointerType::getUnqual(ClassTyHolder),
3233 CachePtrTy,
3234 llvm::PointerType::getUnqual(ImpnfABITy),
3235 llvm::PointerType::getUnqual(
3236 ClassRonfABITy),
3237 NULL);
3238 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3239
3240 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3241 ClassnfABITy);
3242
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003243 // LLVM for struct _class_t *
3244 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3245
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003246 // struct _category_t {
3247 // const char * const name;
3248 // struct _class_t *const cls;
3249 // const struct _method_list_t * const instance_methods;
3250 // const struct _method_list_t * const class_methods;
3251 // const struct _protocol_list_t * const protocols;
3252 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003253 // }
3254 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003255 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003256 MethodListnfABIPtrTy,
3257 MethodListnfABIPtrTy,
3258 ProtocolListnfABIPtrTy,
3259 PropertyListPtrTy,
3260 NULL);
3261 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003262
3263 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003264 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3265 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003266
3267 // MessageRefTy - LLVM for:
3268 // struct _message_ref_t {
3269 // IMP messenger;
3270 // SEL name;
3271 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003272
3273 // First the clang type for struct _message_ref_t
3274 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3275 SourceLocation(),
3276 &Ctx.Idents.get("_message_ref_t"));
3277 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3278 Ctx.VoidPtrTy, 0, false));
3279 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3280 Ctx.getObjCSelType(), 0, false));
3281 RD->completeDefinition(Ctx);
3282
3283 MessageRefCTy = Ctx.getTagDeclType(RD);
3284 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3285 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003286
3287 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3288 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3289
3290 // SuperMessageRefTy - LLVM for:
3291 // struct _super_message_ref_t {
3292 // SUPER_IMP messenger;
3293 // SEL name;
3294 // };
3295 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3296 SelectorPtrTy,
3297 NULL);
3298 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3299
3300 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3301 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3302
3303 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3304 Params.clear();
3305 Params.push_back(ObjectPtrTy);
3306 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003307 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3308 Params,
3309 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003310 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003311 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003312 "objc_msgSend_fixup");
3313
3314 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3315 MessageSendFpretFixupFn =
3316 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3317 Params,
3318 true),
3319 "objc_msgSend_fpret_fixup");
3320
3321 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3322 MessageSendStretFixupFn =
3323 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3324 Params,
3325 true),
3326 "objc_msgSend_stret_fixup");
3327
3328 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3329 MessageSendIdFixupFn =
3330 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3331 Params,
3332 true),
3333 "objc_msgSendId_fixup");
3334
3335
3336 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3337 MessageSendIdStretFixupFn =
3338 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3339 Params,
3340 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003341 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003342
3343 // id objc_msgSendSuper2_fixup (struct objc_super *,
3344 // struct _super_message_ref_t*, ...)
3345 Params.clear();
3346 Params.push_back(SuperPtrTy);
3347 Params.push_back(SuperMessageRefPtrTy);
3348 MessageSendSuper2FixupFn =
3349 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3350 Params,
3351 true),
3352 "objc_msgSendSuper2_fixup");
3353
3354
3355 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3356 // struct _super_message_ref_t*, ...)
3357 MessageSendSuper2StretFixupFn =
3358 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3359 Params,
3360 true),
3361 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003362
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003363}
3364
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003365llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3366 FinishNonFragileABIModule();
3367
3368 return NULL;
3369}
3370
3371void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3372 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003373
3374 // Build list of all implemented classe addresses in array
3375 // L_OBJC_LABEL_CLASS_$.
3376 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3377 // list of 'nonlazy' implementations (defined as those with a +load{}
3378 // method!!).
3379 unsigned NumClasses = DefinedClasses.size();
3380 if (NumClasses) {
3381 std::vector<llvm::Constant*> Symbols(NumClasses);
3382 for (unsigned i=0; i<NumClasses; i++)
3383 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3384 ObjCTypes.Int8PtrTy);
3385 llvm::Constant* Init =
3386 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3387 NumClasses),
3388 Symbols);
3389
3390 llvm::GlobalVariable *GV =
3391 new llvm::GlobalVariable(Init->getType(), false,
3392 llvm::GlobalValue::InternalLinkage,
3393 Init,
3394 "\01L_OBJC_LABEL_CLASS_$",
3395 &CGM.getModule());
3396 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3397 UsedGlobals.push_back(GV);
3398 }
3399
3400 // Build list of all implemented category addresses in array
3401 // L_OBJC_LABEL_CATEGORY_$.
3402 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3403 // list of 'nonlazy' category implementations (defined as those with a +load{}
3404 // method!!).
3405 unsigned NumCategory = DefinedCategories.size();
3406 if (NumCategory) {
3407 std::vector<llvm::Constant*> Symbols(NumCategory);
3408 for (unsigned i=0; i<NumCategory; i++)
3409 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3410 ObjCTypes.Int8PtrTy);
3411 llvm::Constant* Init =
3412 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3413 NumCategory),
3414 Symbols);
3415
3416 llvm::GlobalVariable *GV =
3417 new llvm::GlobalVariable(Init->getType(), false,
3418 llvm::GlobalValue::InternalLinkage,
3419 Init,
3420 "\01L_OBJC_LABEL_CATEGORY_$",
3421 &CGM.getModule());
3422 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3423 UsedGlobals.push_back(GV);
3424 }
3425
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003426 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3427 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3428 std::vector<llvm::Constant*> Values(2);
3429 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3430 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3431 llvm::Constant* Init = llvm::ConstantArray::get(
3432 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3433 Values);
3434 llvm::GlobalVariable *IMGV =
3435 new llvm::GlobalVariable(Init->getType(), false,
3436 llvm::GlobalValue::InternalLinkage,
3437 Init,
3438 "\01L_OBJC_IMAGE_INFO",
3439 &CGM.getModule());
3440 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3441 UsedGlobals.push_back(IMGV);
3442
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003443 std::vector<llvm::Constant*> Used;
3444 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3445 e = UsedGlobals.end(); i != e; ++i) {
3446 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3447 }
3448
3449 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3450 llvm::GlobalValue *GV =
3451 new llvm::GlobalVariable(AT, false,
3452 llvm::GlobalValue::AppendingLinkage,
3453 llvm::ConstantArray::get(AT, Used),
3454 "llvm.used",
3455 &CGM.getModule());
3456
3457 GV->setSection("llvm.metadata");
3458
3459}
3460
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003461// Metadata flags
3462enum MetaDataDlags {
3463 CLS = 0x0,
3464 CLS_META = 0x1,
3465 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003466 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003467 CLS_EXCEPTION = 0x20
3468};
3469/// BuildClassRoTInitializer - generate meta-data for:
3470/// struct _class_ro_t {
3471/// uint32_t const flags;
3472/// uint32_t const instanceStart;
3473/// uint32_t const instanceSize;
3474/// uint32_t const reserved; // only when building for 64bit targets
3475/// const uint8_t * const ivarLayout;
3476/// const char *const name;
3477/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003478/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003479/// const struct _ivar_list_t *const ivars;
3480/// const uint8_t * const weakIvarLayout;
3481/// const struct _prop_list_t * const properties;
3482/// }
3483///
3484llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3485 unsigned flags,
3486 unsigned InstanceStart,
3487 unsigned InstanceSize,
3488 const ObjCImplementationDecl *ID) {
3489 std::string ClassName = ID->getNameAsString();
3490 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3491 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3492 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3493 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3494 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003495 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003496 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3497 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003498 // const struct _method_list_t * const baseMethods;
3499 std::vector<llvm::Constant*> Methods;
3500 std::string MethodListName("\01l_OBJC_$_");
3501 if (flags & CLS_META) {
3502 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3503 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3504 e = ID->classmeth_end(); i != e; ++i) {
3505 // Class methods should always be defined.
3506 Methods.push_back(GetMethodConstant(*i));
3507 }
3508 } else {
3509 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3510 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3511 e = ID->instmeth_end(); i != e; ++i) {
3512 // Instance methods should always be defined.
3513 Methods.push_back(GetMethodConstant(*i));
3514 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003515 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3516 e = ID->propimpl_end(); i != e; ++i) {
3517 ObjCPropertyImplDecl *PID = *i;
3518
3519 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3520 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3521
3522 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3523 if (llvm::Constant *C = GetMethodConstant(MD))
3524 Methods.push_back(C);
3525 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3526 if (llvm::Constant *C = GetMethodConstant(MD))
3527 Methods.push_back(C);
3528 }
3529 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003530 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003531 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003532 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003533
3534 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3535 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3536 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3537 + OID->getNameAsString(),
3538 OID->protocol_begin(),
3539 OID->protocol_end());
3540
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003541 if (flags & CLS_META)
3542 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3543 else
3544 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003545 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003546 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003547 if (flags & CLS_META)
3548 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3549 else
3550 Values[ 9] =
3551 EmitPropertyList(
3552 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3553 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003554 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3555 Values);
3556 llvm::GlobalVariable *CLASS_RO_GV =
3557 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3558 llvm::GlobalValue::InternalLinkage,
3559 Init,
3560 (flags & CLS_META) ?
3561 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3562 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3563 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003564 CLASS_RO_GV->setAlignment(
3565 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003566 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003567 UsedGlobals.push_back(CLASS_RO_GV);
3568 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003569
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003570}
3571
3572/// BuildClassMetaData - This routine defines that to-level meta-data
3573/// for the given ClassName for:
3574/// struct _class_t {
3575/// struct _class_t *isa;
3576/// struct _class_t * const superclass;
3577/// void *cache;
3578/// IMP *vtable;
3579/// struct class_ro_t *ro;
3580/// }
3581///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003582llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3583 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003584 llvm::Constant *IsAGV,
3585 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003586 llvm::Constant *ClassRoGV,
3587 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003588 std::vector<llvm::Constant*> Values(5);
3589 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003590 Values[1] = SuperClassGV
3591 ? SuperClassGV
3592 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003593 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3594 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3595 Values[4] = ClassRoGV; // &CLASS_RO_GV
3596 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3597 Values);
3598 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3599 if (GV)
3600 GV->setInitializer(Init);
3601 else
3602 GV =
3603 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3604 llvm::GlobalValue::ExternalLinkage,
3605 Init,
3606 ClassName,
3607 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003608 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003609 GV->setAlignment(
3610 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003611 if (HiddenVisibility)
3612 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003613 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003614 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003615}
3616
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003617void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3618 std::string ClassName = ID->getNameAsString();
3619 if (!ObjCEmptyCacheVar) {
3620 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003621 ObjCTypes.CacheTy,
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_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003626 &CGM.getModule());
3627 UsedGlobals.push_back(ObjCEmptyCacheVar);
3628
3629 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003630 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003631 false,
3632 llvm::GlobalValue::ExternalLinkage,
3633 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003634 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003635 &CGM.getModule());
3636 UsedGlobals.push_back(ObjCEmptyVtableVar);
3637 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003638 assert(ID->getClassInterface() &&
3639 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003640 uint32_t InstanceStart =
3641 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3642 uint32_t InstanceSize = InstanceStart;
3643 uint32_t flags = CLS_META;
3644 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3645 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003646
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003647 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003648
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003649 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3650 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003651 flags |= OBJC2_CLS_HIDDEN;
3652 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003653 // class is root
3654 flags |= CLS_ROOT;
3655 std::string SuperClassName = ObjCClassName + ClassName;
3656 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003657 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003658 SuperClassGV =
3659 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3660 llvm::GlobalValue::ExternalLinkage,
3661 0,
3662 SuperClassName,
3663 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003664 UsedGlobals.push_back(SuperClassGV);
3665 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003666 std::string IsAClassName = ObjCMetaClassName + ClassName;
3667 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003668 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003669 IsAGV =
3670 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3671 llvm::GlobalValue::ExternalLinkage,
3672 0,
3673 IsAClassName,
3674 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003675 UsedGlobals.push_back(IsAGV);
3676 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003677 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003678 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003679 std::string RootClassName =
3680 ID->getClassInterface()->getSuperClass()->getNameAsString();
3681 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3682 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003683 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003684 SuperClassGV =
3685 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3686 llvm::GlobalValue::ExternalLinkage,
3687 0,
3688 SuperClassName,
3689 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003690 UsedGlobals.push_back(SuperClassGV);
3691 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003692 IsAGV = SuperClassGV;
3693 }
3694 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3695 InstanceStart,
3696 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003697 std::string TClassName = ObjCMetaClassName + ClassName;
3698 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003699 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3700 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003701
3702 // Metadata for the class
3703 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003704 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003705 flags |= OBJC2_CLS_HIDDEN;
3706 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003707 flags |= CLS_ROOT;
3708 SuperClassGV = 0;
3709 }
3710 else {
3711 // Has a root. Current class is not a root.
3712 std::string RootClassName =
3713 ID->getClassInterface()->getSuperClass()->getNameAsString();
3714 std::string SuperClassName = ObjCClassName + RootClassName;
3715 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003716 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003717 SuperClassGV =
3718 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3719 llvm::GlobalValue::ExternalLinkage,
3720 0,
3721 SuperClassName,
3722 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003723 UsedGlobals.push_back(SuperClassGV);
3724 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003725 }
3726
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003727 InstanceStart = InstanceSize = 0;
3728 if (ObjCInterfaceDecl *OID =
3729 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3730 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003731 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003732 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003733 const llvm::StructLayout *Layout =
3734 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003735
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003736 RecordDecl::field_iterator firstField, lastField;
3737 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003738
3739 for (RecordDecl::field_iterator e = RD->field_end(),
3740 ifield = firstField; ifield != e; ++ifield)
3741 lastField = ifield;
3742
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003743 if (lastField != RD->field_end()) {
3744 FieldDecl *Field = *lastField;
3745 const llvm::Type *FieldTy =
3746 CGM.getTypes().ConvertTypeForMem(Field->getType());
3747 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3748 InstanceSize = Layout->getElementOffset(
3749 CGM.getTypes().getLLVMFieldNo(Field)) +
3750 Size;
3751 if (firstField == RD->field_end())
3752 InstanceStart = InstanceSize;
3753 else
3754 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3755 getLLVMFieldNo(*firstField));
3756 }
3757 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003758 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003759 InstanceStart,
3760 InstanceSize,
3761 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003762
3763 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003764 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003765 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3766 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003767 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003768}
3769
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003770/// GenerateProtocolRef - This routine is called to generate code for
3771/// a protocol reference expression; as in:
3772/// @code
3773/// @protocol(Proto1);
3774/// @endcode
3775/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3776/// which will hold address of the protocol meta-data.
3777///
3778llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3779 const ObjCProtocolDecl *PD) {
3780
3781 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3782 ObjCTypes.ExternalProtocolPtrTy);
3783
3784 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3785 ProtocolName += PD->getNameAsCString();
3786
3787 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3788 if (PTGV)
3789 return Builder.CreateLoad(PTGV, false, "tmp");
3790 PTGV = new llvm::GlobalVariable(
3791 Init->getType(), false,
3792 llvm::GlobalValue::WeakLinkage,
3793 Init,
3794 ProtocolName,
3795 &CGM.getModule());
3796 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3797 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3798 UsedGlobals.push_back(PTGV);
3799 return Builder.CreateLoad(PTGV, false, "tmp");
3800}
3801
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003802/// GenerateCategory - Build metadata for a category implementation.
3803/// struct _category_t {
3804/// const char * const name;
3805/// struct _class_t *const cls;
3806/// const struct _method_list_t * const instance_methods;
3807/// const struct _method_list_t * const class_methods;
3808/// const struct _protocol_list_t * const protocols;
3809/// const struct _prop_list_t * const properties;
3810/// }
3811///
3812void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3813{
3814 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003815 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3816 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003817 "_$_" + OCD->getNameAsString());
3818 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3819
3820 std::vector<llvm::Constant*> Values(6);
3821 Values[0] = GetClassName(OCD->getIdentifier());
3822 // meta-class entry symbol
3823 llvm::GlobalVariable *ClassGV =
3824 CGM.getModule().getGlobalVariable(ExtClassName);
3825 if (!ClassGV)
3826 ClassGV =
3827 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3828 llvm::GlobalValue::ExternalLinkage,
3829 0,
3830 ExtClassName,
3831 &CGM.getModule());
3832 UsedGlobals.push_back(ClassGV);
3833 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003834 std::vector<llvm::Constant*> Methods;
3835 std::string MethodListName(Prefix);
3836 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3837 "_$_" + OCD->getNameAsString();
3838
3839 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3840 e = OCD->instmeth_end(); i != e; ++i) {
3841 // Instance methods should always be defined.
3842 Methods.push_back(GetMethodConstant(*i));
3843 }
3844
3845 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003846 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003847 Methods);
3848
3849 MethodListName = Prefix;
3850 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3851 OCD->getNameAsString();
3852 Methods.clear();
3853 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3854 e = OCD->classmeth_end(); i != e; ++i) {
3855 // Class methods should always be defined.
3856 Methods.push_back(GetMethodConstant(*i));
3857 }
3858
3859 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003860 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003861 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003862 const ObjCCategoryDecl *Category =
3863 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003864 if (Category) {
3865 std::string ExtName(Interface->getNameAsString() + "_$_" +
3866 OCD->getNameAsString());
3867 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3868 + Interface->getNameAsString() + "_$_"
3869 + Category->getNameAsString(),
3870 Category->protocol_begin(),
3871 Category->protocol_end());
3872 Values[5] =
3873 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3874 OCD, Category, ObjCTypes);
3875 }
3876 else {
3877 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3878 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3879 }
3880
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003881 llvm::Constant *Init =
3882 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3883 Values);
3884 llvm::GlobalVariable *GCATV
3885 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3886 false,
3887 llvm::GlobalValue::InternalLinkage,
3888 Init,
3889 ExtCatName,
3890 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003891 GCATV->setAlignment(
3892 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003893 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003894 UsedGlobals.push_back(GCATV);
3895 DefinedCategories.push_back(GCATV);
3896}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003897
3898/// GetMethodConstant - Return a struct objc_method constant for the
3899/// given method if it has been defined. The result is null if the
3900/// method has not been defined. The return value has type MethodPtrTy.
3901llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3902 const ObjCMethodDecl *MD) {
3903 // FIXME: Use DenseMap::lookup
3904 llvm::Function *Fn = MethodDefinitions[MD];
3905 if (!Fn)
3906 return 0;
3907
3908 std::vector<llvm::Constant*> Method(3);
3909 Method[0] =
3910 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3911 ObjCTypes.SelectorPtrTy);
3912 Method[1] = GetMethodVarType(MD);
3913 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3914 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3915}
3916
3917/// EmitMethodList - Build meta-data for method declarations
3918/// struct _method_list_t {
3919/// uint32_t entsize; // sizeof(struct _objc_method)
3920/// uint32_t method_count;
3921/// struct _objc_method method_list[method_count];
3922/// }
3923///
3924llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3925 const std::string &Name,
3926 const char *Section,
3927 const ConstantVector &Methods) {
3928 // Return null for empty list.
3929 if (Methods.empty())
3930 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3931
3932 std::vector<llvm::Constant*> Values(3);
3933 // sizeof(struct _objc_method)
3934 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3935 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3936 // method_count
3937 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3938 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3939 Methods.size());
3940 Values[2] = llvm::ConstantArray::get(AT, Methods);
3941 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3942
3943 llvm::GlobalVariable *GV =
3944 new llvm::GlobalVariable(Init->getType(), false,
3945 llvm::GlobalValue::InternalLinkage,
3946 Init,
3947 Name,
3948 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003949 GV->setAlignment(
3950 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003951 GV->setSection(Section);
3952 UsedGlobals.push_back(GV);
3953 return llvm::ConstantExpr::getBitCast(GV,
3954 ObjCTypes.MethodListnfABIPtrTy);
3955}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003956
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003957/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3958/// the given ivar.
3959///
3960llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3961 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003962 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003963 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003964 Name += "\01_OBJC_IVAR_$_" +
3965 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003966 + Ivar->getNameAsString();
3967 llvm::GlobalVariable *IvarOffsetGV =
3968 CGM.getModule().getGlobalVariable(Name);
3969 if (!IvarOffsetGV)
3970 IvarOffsetGV =
3971 new llvm::GlobalVariable(ObjCTypes.LongTy,
3972 false,
3973 llvm::GlobalValue::ExternalLinkage,
3974 0,
3975 Name,
3976 &CGM.getModule());
3977 return IvarOffsetGV;
3978}
3979
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003980llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003981 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003982 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003983 unsigned long int Offset) {
3984
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003985 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003986 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003987 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003988 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003989
3990 llvm::GlobalVariable *IvarOffsetGV =
3991 CGM.getModule().getGlobalVariable(ExternalName);
3992 if (IvarOffsetGV) {
3993 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003994 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003995 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003996 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003997 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003998 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003999 return IvarOffsetGV;
4000 }
4001
4002 IvarOffsetGV =
4003 new llvm::GlobalVariable(Init->getType(),
4004 false,
4005 llvm::GlobalValue::ExternalLinkage,
4006 Init,
4007 ExternalName,
4008 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004009 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004010 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004011 // @private and @package have hidden visibility.
4012 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4013 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4014 if (!globalVisibility)
4015 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004016 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004017 if (IsClassHidden(ID))
4018 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004019
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004020 IvarOffsetGV->setSection("__DATA, __objc_const");
4021 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004022 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004023}
4024
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004025/// EmitIvarList - Emit the ivar list for the given
4026/// implementation. If ForClass is true the list of class ivars
4027/// (i.e. metaclass ivars) is emitted, otherwise the list of
4028/// interface ivars will be emitted. The return value has type
4029/// IvarListnfABIPtrTy.
4030/// struct _ivar_t {
4031/// unsigned long int *offset; // pointer to ivar offset location
4032/// char *name;
4033/// char *type;
4034/// uint32_t alignment;
4035/// uint32_t size;
4036/// }
4037/// struct _ivar_list_t {
4038/// uint32 entsize; // sizeof(struct _ivar_t)
4039/// uint32 count;
4040/// struct _iver_t list[count];
4041/// }
4042///
4043llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4044 const ObjCImplementationDecl *ID) {
4045
4046 std::vector<llvm::Constant*> Ivars, Ivar(5);
4047
4048 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4049 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4050
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004051 // FIXME. Consolidate this with similar code in GenerateClass.
4052 const llvm::Type *InterfaceTy =
4053 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4054 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004055 const llvm::StructLayout *Layout =
4056 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004057
4058 RecordDecl::field_iterator i,p;
4059 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004060 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4061
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004062 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004063 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004064 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4065 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004066 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004067 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004068 if (Field->getIdentifier())
4069 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4070 else
4071 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4072 std::string TypeStr;
4073 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4074 Ivar[2] = GetMethodVarType(TypeStr);
4075 const llvm::Type *FieldTy =
4076 CGM.getTypes().ConvertTypeForMem(Field->getType());
4077 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4078 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4079 Field->getType().getTypePtr()) >> 3;
4080 Align = llvm::Log2_32(Align);
4081 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004082 // NOTE. Size of a bitfield does not match gcc's, because of the way
4083 // bitfields are treated special in each. But I am told that 'size'
4084 // for bitfield ivars is ignored by the runtime so it does not matter.
4085 // (even if it matters, some day, there is enough info. to get the bitfield
4086 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004087 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4088 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4089 }
4090 // Return null for empty list.
4091 if (Ivars.empty())
4092 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4093 std::vector<llvm::Constant*> Values(3);
4094 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4095 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4096 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4097 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4098 Ivars.size());
4099 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4100 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4101 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4102 llvm::GlobalVariable *GV =
4103 new llvm::GlobalVariable(Init->getType(), false,
4104 llvm::GlobalValue::InternalLinkage,
4105 Init,
4106 Prefix + OID->getNameAsString(),
4107 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004108 GV->setAlignment(
4109 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004110 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004111
4112 UsedGlobals.push_back(GV);
4113 return llvm::ConstantExpr::getBitCast(GV,
4114 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004115}
4116
4117llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4118 const ObjCProtocolDecl *PD) {
4119 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4120
4121 if (!Entry) {
4122 // We use the initializer as a marker of whether this is a forward
4123 // reference or not. At module finalization we add the empty
4124 // contents for protocols which were referenced but never defined.
4125 Entry =
4126 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4127 llvm::GlobalValue::ExternalLinkage,
4128 0,
4129 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4130 &CGM.getModule());
4131 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4132 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004133 }
4134
4135 return Entry;
4136}
4137
4138/// GetOrEmitProtocol - Generate the protocol meta-data:
4139/// @code
4140/// struct _protocol_t {
4141/// id isa; // NULL
4142/// const char * const protocol_name;
4143/// const struct _protocol_list_t * protocol_list; // super protocols
4144/// const struct method_list_t * const instance_methods;
4145/// const struct method_list_t * const class_methods;
4146/// const struct method_list_t *optionalInstanceMethods;
4147/// const struct method_list_t *optionalClassMethods;
4148/// const struct _prop_list_t * properties;
4149/// const uint32_t size; // sizeof(struct _protocol_t)
4150/// const uint32_t flags; // = 0
4151/// }
4152/// @endcode
4153///
4154
4155llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4156 const ObjCProtocolDecl *PD) {
4157 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4158
4159 // Early exit if a defining object has already been generated.
4160 if (Entry && Entry->hasInitializer())
4161 return Entry;
4162
4163 const char *ProtocolName = PD->getNameAsCString();
4164
4165 // Construct method lists.
4166 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4167 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4168 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4169 e = PD->instmeth_end(); i != e; ++i) {
4170 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004171 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004172 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4173 OptInstanceMethods.push_back(C);
4174 } else {
4175 InstanceMethods.push_back(C);
4176 }
4177 }
4178
4179 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4180 e = PD->classmeth_end(); i != e; ++i) {
4181 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004182 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004183 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4184 OptClassMethods.push_back(C);
4185 } else {
4186 ClassMethods.push_back(C);
4187 }
4188 }
4189
4190 std::vector<llvm::Constant*> Values(10);
4191 // isa is NULL
4192 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4193 Values[1] = GetClassName(PD->getIdentifier());
4194 Values[2] = EmitProtocolList(
4195 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4196 PD->protocol_begin(),
4197 PD->protocol_end());
4198
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004199 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004200 + PD->getNameAsString(),
4201 "__DATA, __objc_const",
4202 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004203 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004204 + PD->getNameAsString(),
4205 "__DATA, __objc_const",
4206 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004207 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004208 + PD->getNameAsString(),
4209 "__DATA, __objc_const",
4210 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004211 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004212 + PD->getNameAsString(),
4213 "__DATA, __objc_const",
4214 OptClassMethods);
4215 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4216 0, PD, ObjCTypes);
4217 uint32_t Size =
4218 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4219 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4220 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4221 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4222 Values);
4223
4224 if (Entry) {
4225 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004226 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004227 Entry->setInitializer(Init);
4228 } else {
4229 Entry =
4230 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004231 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004232 Init,
4233 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4234 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004235 Entry->setAlignment(
4236 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004237 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004238 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004239 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4240
4241 // Use this protocol meta-data to build protocol list table in section
4242 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004243 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004244 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004245 llvm::GlobalValue::WeakLinkage,
4246 Entry,
4247 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4248 +ProtocolName,
4249 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004250 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004251 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004252 PTGV->setSection("__DATA, __objc_protolist");
4253 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4254 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004255 return Entry;
4256}
4257
4258/// EmitProtocolList - Generate protocol list meta-data:
4259/// @code
4260/// struct _protocol_list_t {
4261/// long protocol_count; // Note, this is 32/64 bit
4262/// struct _protocol_t[protocol_count];
4263/// }
4264/// @endcode
4265///
4266llvm::Constant *
4267CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4268 ObjCProtocolDecl::protocol_iterator begin,
4269 ObjCProtocolDecl::protocol_iterator end) {
4270 std::vector<llvm::Constant*> ProtocolRefs;
4271
Fariborz Jahanianda320092009-01-29 19:24:30 +00004272 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004273 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004274 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4275
Daniel Dunbar948e2582009-02-15 07:36:20 +00004276 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004277 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4278 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004279 return llvm::ConstantExpr::getBitCast(GV,
4280 ObjCTypes.ProtocolListnfABIPtrTy);
4281
4282 for (; begin != end; ++begin)
4283 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4284
Fariborz Jahanianda320092009-01-29 19:24:30 +00004285 // This list is null terminated.
4286 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004287 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004288
4289 std::vector<llvm::Constant*> Values(2);
4290 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4291 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004292 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004293 ProtocolRefs.size()),
4294 ProtocolRefs);
4295
4296 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4297 GV = new llvm::GlobalVariable(Init->getType(), false,
4298 llvm::GlobalValue::InternalLinkage,
4299 Init,
4300 Name,
4301 &CGM.getModule());
4302 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004303 GV->setAlignment(
4304 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004305 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004306 return llvm::ConstantExpr::getBitCast(GV,
4307 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004308}
4309
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004310/// GetMethodDescriptionConstant - This routine build following meta-data:
4311/// struct _objc_method {
4312/// SEL _cmd;
4313/// char *method_type;
4314/// char *_imp;
4315/// }
4316
4317llvm::Constant *
4318CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4319 std::vector<llvm::Constant*> Desc(3);
4320 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4321 ObjCTypes.SelectorPtrTy);
4322 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004323 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004324 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4325 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4326}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004327
4328/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4329/// This code gen. amounts to generating code for:
4330/// @code
4331/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4332/// @encode
4333///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004334LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004335 CodeGen::CodeGenFunction &CGF,
4336 QualType ObjectTy,
4337 llvm::Value *BaseValue,
4338 const ObjCIvarDecl *Ivar,
4339 const FieldDecl *Field,
4340 unsigned CVRQualifiers) {
4341 assert(ObjectTy->isObjCInterfaceType() &&
4342 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004343 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004344 std::string ExternalName;
4345 llvm::GlobalVariable *IvarOffsetGV =
4346 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004347
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004348 // (char *) BaseValue
4349 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4350 ObjCTypes.Int8PtrTy);
4351 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4352 // (char*)BaseValue + Offset_symbol
4353 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4354 // (type *)((char*)BaseValue + Offset_symbol)
4355 const llvm::Type *IvarTy =
4356 CGM.getTypes().ConvertType(Ivar->getType());
4357 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4358 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004359
4360 if (Ivar->isBitField())
4361 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4362 CVRQualifiers);
4363
4364 LValue LV = LValue::MakeAddr(V,
4365 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4366 LValue::SetObjCIvar(LV, true);
4367 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004368}
4369
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004370llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4371 CodeGen::CodeGenFunction &CGF,
4372 ObjCInterfaceDecl *Interface,
4373 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004374 std::string ExternalName;
4375 llvm::GlobalVariable *IvarOffsetGV =
4376 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4377
4378 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004379}
4380
Fariborz Jahanian46551122009-02-04 00:22:57 +00004381CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4382 CodeGen::CodeGenFunction &CGF,
4383 QualType ResultType,
4384 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004385 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004386 QualType Arg0Ty,
4387 bool IsSuper,
4388 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004389 // FIXME. Even though IsSuper is passes. This function doese not
4390 // handle calls to 'super' receivers.
4391 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004392 llvm::Value *Arg0 = Receiver;
4393 if (!IsSuper)
4394 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004395
4396 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004397 // FIXME. This is too much work to get the ABI-specific result type
4398 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004399 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4400 llvm::SmallVector<QualType, 16>());
4401 llvm::Constant *Fn;
4402 std::string Name("\01l_");
4403 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004404#if 0
4405 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004406 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4407 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4408 // FIXME. Is there a better way of getting these names.
4409 // They are available in RuntimeFunctions vector pair.
4410 Name += "objc_msgSendId_stret_fixup";
4411 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004412 else
4413#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004414 if (IsSuper) {
4415 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4416 Name += "objc_msgSendSuper2_stret_fixup";
4417 }
4418 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004419 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004420 Fn = ObjCTypes.MessageSendStretFixupFn;
4421 Name += "objc_msgSend_stret_fixup";
4422 }
4423 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004424 else if (ResultType->isFloatingType() &&
4425 // Selection of frret API only happens in 32bit nonfragile ABI.
4426 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004427 Fn = ObjCTypes.MessageSendFpretFixupFn;
4428 Name += "objc_msgSend_fpret_fixup";
4429 }
4430 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004431#if 0
4432// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004433 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4434 Fn = ObjCTypes.MessageSendIdFixupFn;
4435 Name += "objc_msgSendId_fixup";
4436 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004437 else
4438#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004439 if (IsSuper) {
4440 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4441 Name += "objc_msgSendSuper2_fixup";
4442 }
4443 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004444 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004445 Fn = ObjCTypes.MessageSendFixupFn;
4446 Name += "objc_msgSend_fixup";
4447 }
4448 }
4449 Name += '_';
4450 std::string SelName(Sel.getAsString());
4451 // Replace all ':' in selector name with '_' ouch!
4452 for(unsigned i = 0; i < SelName.size(); i++)
4453 if (SelName[i] == ':')
4454 SelName[i] = '_';
4455 Name += SelName;
4456 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4457 if (!GV) {
4458 // Build messafe ref table entry.
4459 std::vector<llvm::Constant*> Values(2);
4460 Values[0] = Fn;
4461 Values[1] = GetMethodVarName(Sel);
4462 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4463 GV = new llvm::GlobalVariable(Init->getType(), false,
4464 llvm::GlobalValue::WeakLinkage,
4465 Init,
4466 Name,
4467 &CGM.getModule());
4468 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4469 GV->setAlignment(
4470 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4471 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4472 UsedGlobals.push_back(GV);
4473 }
4474 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004475
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004476 CallArgList ActualArgs;
4477 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4478 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4479 ObjCTypes.MessageRefCPtrTy));
4480 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004481 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4482 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4483 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004484 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004485 Callee = CGF.Builder.CreateBitCast(Callee,
4486 llvm::PointerType::getUnqual(FTy));
4487 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004488}
4489
4490/// Generate code for a message send expression in the nonfragile abi.
4491CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4492 CodeGen::CodeGenFunction &CGF,
4493 QualType ResultType,
4494 Selector Sel,
4495 llvm::Value *Receiver,
4496 bool IsClassMessage,
4497 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004498 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004499 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004500 false, CallArgs);
4501}
4502
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004503llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004504 const ObjCInterfaceDecl *ID,
4505 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004506
4507 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4508
4509 if (!Entry) {
4510 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4511 llvm::GlobalVariable *ClassGV =
4512 CGM.getModule().getGlobalVariable(ClassName);
4513 if (!ClassGV) {
4514 ClassGV =
4515 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4516 llvm::GlobalValue::ExternalLinkage,
4517 0,
4518 ClassName,
4519 &CGM.getModule());
4520 UsedGlobals.push_back(ClassGV);
4521 }
4522 Entry =
4523 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4524 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004525 ClassGV,
4526 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4527 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004528 &CGM.getModule());
4529 Entry->setAlignment(
4530 CGM.getTargetData().getPrefTypeAlignment(
4531 ObjCTypes.ClassnfABIPtrTy));
4532
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004533 if (IsSuper)
4534 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4535 else
4536 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004537 UsedGlobals.push_back(Entry);
4538 }
4539
4540 return Builder.CreateLoad(Entry, false, "tmp");
4541}
4542
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004543/// EmitMetaClassRef - Return a Value * of the address of _class_t
4544/// meta-data
4545///
4546llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4547 const ObjCInterfaceDecl *ID) {
4548 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4549 if (Entry)
4550 return Builder.CreateLoad(Entry, false, "tmp");
4551
4552 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4553 llvm::GlobalVariable *MetaClassGV =
4554 CGM.getModule().getGlobalVariable(MetaClassName);
4555 if (!MetaClassGV) {
4556 MetaClassGV =
4557 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4558 llvm::GlobalValue::ExternalLinkage,
4559 0,
4560 MetaClassName,
4561 &CGM.getModule());
4562 UsedGlobals.push_back(MetaClassGV);
4563 }
4564
4565 Entry =
4566 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4567 llvm::GlobalValue::InternalLinkage,
4568 MetaClassGV,
4569 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4570 &CGM.getModule());
4571 Entry->setAlignment(
4572 CGM.getTargetData().getPrefTypeAlignment(
4573 ObjCTypes.ClassnfABIPtrTy));
4574
4575 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4576 UsedGlobals.push_back(Entry);
4577
4578 return Builder.CreateLoad(Entry, false, "tmp");
4579}
4580
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004581/// GetClass - Return a reference to the class for the given interface
4582/// decl.
4583llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4584 const ObjCInterfaceDecl *ID) {
4585 return EmitClassRef(Builder, ID);
4586}
4587
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004588/// Generates a message send where the super is the receiver. This is
4589/// a message send to self with special delivery semantics indicating
4590/// which class's method should be called.
4591CodeGen::RValue
4592CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4593 QualType ResultType,
4594 Selector Sel,
4595 const ObjCInterfaceDecl *Class,
4596 llvm::Value *Receiver,
4597 bool IsClassMessage,
4598 const CodeGen::CallArgList &CallArgs) {
4599 // ...
4600 // Create and init a super structure; this is a (receiver, class)
4601 // pair we will pass to objc_msgSendSuper.
4602 llvm::Value *ObjCSuper =
4603 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4604
4605 llvm::Value *ReceiverAsObject =
4606 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4607 CGF.Builder.CreateStore(ReceiverAsObject,
4608 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4609
4610 // If this is a class message the metaclass is passed as the target.
4611 llvm::Value *Target =
4612 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4613 : EmitClassRef(CGF.Builder, Class, true);
4614
4615 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4616 // and ObjCTypes types.
4617 const llvm::Type *ClassTy =
4618 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4619 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4620 CGF.Builder.CreateStore(Target,
4621 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4622
4623 return EmitMessageSend(CGF, ResultType, Sel,
4624 ObjCSuper, ObjCTypes.SuperPtrCTy,
4625 true, CallArgs);
4626}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004627
4628llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4629 Selector Sel) {
4630 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4631
4632 if (!Entry) {
4633 llvm::Constant *Casted =
4634 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4635 ObjCTypes.SelectorPtrTy);
4636 Entry =
4637 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4638 llvm::GlobalValue::InternalLinkage,
4639 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4640 &CGM.getModule());
4641 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4642 UsedGlobals.push_back(Entry);
4643 }
4644
4645 return Builder.CreateLoad(Entry, false, "tmp");
4646}
4647
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004648/* *** */
4649
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004650CodeGen::CGObjCRuntime *
4651CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004652 return new CGObjCMac(CGM);
4653}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004654
4655CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004656CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004657 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004658}