blob: 021fe15483d3598222e89973437a6fae3ffb7bb1 [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;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000104
105 /// ExceptionThrowFn - LLVM objc_exception_throw function.
106 llvm::Function *ExceptionThrowFn;
107
Daniel Dunbar1c566672009-02-24 01:43:46 +0000108 /// SyncEnterFn - LLVM object_sync_enter function.
109 llvm::Function *SyncEnterFn;
110
111 /// SyncExitFn - LLVM object_sync_exit function.
112 llvm::Function *SyncExitFn;
113
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000114 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
115 ~ObjCCommonTypesHelper(){}
116};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000117
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000118/// ObjCTypesHelper - Helper class that encapsulates lazy
119/// construction of varies types used during ObjC generation.
120class ObjCTypesHelper : public ObjCCommonTypesHelper {
121private:
122
123 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
124 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
125 *MessageSendSuperFpretFn;
126
127public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000128 /// SymtabTy - LLVM type for struct objc_symtab.
129 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000130 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
131 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000132 /// ModuleTy - LLVM type for struct objc_module.
133 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000134
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000135 /// ProtocolTy - LLVM type for struct objc_protocol.
136 const llvm::StructType *ProtocolTy;
137 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
138 const llvm::Type *ProtocolPtrTy;
139 /// ProtocolExtensionTy - LLVM type for struct
140 /// objc_protocol_extension.
141 const llvm::StructType *ProtocolExtensionTy;
142 /// ProtocolExtensionTy - LLVM type for struct
143 /// objc_protocol_extension *.
144 const llvm::Type *ProtocolExtensionPtrTy;
145 /// MethodDescriptionTy - LLVM type for struct
146 /// objc_method_description.
147 const llvm::StructType *MethodDescriptionTy;
148 /// MethodDescriptionListTy - LLVM type for struct
149 /// objc_method_description_list.
150 const llvm::StructType *MethodDescriptionListTy;
151 /// MethodDescriptionListPtrTy - LLVM type for struct
152 /// objc_method_description_list *.
153 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000154 /// ProtocolListTy - LLVM type for struct objc_property_list.
155 const llvm::Type *ProtocolListTy;
156 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
157 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000158 /// CategoryTy - LLVM type for struct objc_category.
159 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000160 /// ClassTy - LLVM type for struct objc_class.
161 const llvm::StructType *ClassTy;
162 /// ClassPtrTy - LLVM type for struct objc_class *.
163 const llvm::Type *ClassPtrTy;
164 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
165 const llvm::StructType *ClassExtensionTy;
166 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
167 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000168 // IvarTy - LLVM type for struct objc_ivar.
169 const llvm::StructType *IvarTy;
170 /// IvarListTy - LLVM type for struct objc_ivar_list.
171 const llvm::Type *IvarListTy;
172 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
173 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000174 /// MethodListTy - LLVM type for struct objc_method_list.
175 const llvm::Type *MethodListTy;
176 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
177 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000178
179 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
180 const llvm::Type *ExceptionDataTy;
181
Anders Carlsson124526b2008-09-09 10:10:21 +0000182 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
183 llvm::Function *ExceptionTryEnterFn;
184
185 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
186 llvm::Function *ExceptionTryExitFn;
187
188 /// ExceptionExtractFn - LLVM objc_exception_extract function.
189 llvm::Function *ExceptionExtractFn;
190
191 /// ExceptionMatchFn - LLVM objc_exception_match function.
192 llvm::Function *ExceptionMatchFn;
193
194 /// SetJmpFn - LLVM _setjmp function.
195 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000196
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,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000738 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000739 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000740 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000741 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000742 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000743 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000744 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000745 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000746 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000747 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000748 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000749 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
750 QualType ObjectTy,
751 llvm::Value *BaseValue,
752 const ObjCIvarDecl *Ivar,
753 const FieldDecl *Field,
754 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000755 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
756 ObjCInterfaceDecl *Interface,
757 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000758};
759
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000760} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000761
762/* *** Helper Functions *** */
763
764/// getConstantGEP() - Help routine to construct simple GEPs.
765static llvm::Constant *getConstantGEP(llvm::Constant *C,
766 unsigned idx0,
767 unsigned idx1) {
768 llvm::Value *Idxs[] = {
769 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
770 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
771 };
772 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
773}
774
775/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000776
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000777CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
778 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000779{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000780 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000781 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000782}
783
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000784/// GetClass - Return a reference to the class for the given interface
785/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000786llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000787 const ObjCInterfaceDecl *ID) {
788 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000789}
790
791/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000792llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000793 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000794}
795
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000796/// Generate a constant CFString object.
797/*
798 struct __builtin_CFString {
799 const int *isa; // point to __CFConstantStringClassReference
800 int flags;
801 const char *str;
802 long length;
803 };
804*/
805
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000806llvm::Constant *CGObjCCommonMac::GenerateConstantString(
807 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000808 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000809}
810
811/// Generates a message send where the super is the receiver. This is
812/// a message send to self with special delivery semantics indicating
813/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000814CodeGen::RValue
815CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000816 QualType ResultType,
817 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000818 const ObjCInterfaceDecl *Class,
819 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000820 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000821 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000822 // Create and init a super structure; this is a (receiver, class)
823 // pair we will pass to objc_msgSendSuper.
824 llvm::Value *ObjCSuper =
825 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
826 llvm::Value *ReceiverAsObject =
827 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
828 CGF.Builder.CreateStore(ReceiverAsObject,
829 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000830
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000831 // If this is a class message the metaclass is passed as the target.
832 llvm::Value *Target;
833 if (IsClassMessage) {
834 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
835 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
836 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
837 Target = Super;
838 } else {
839 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
840 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000841 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
842 // and ObjCTypes types.
843 const llvm::Type *ClassTy =
844 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000845 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000846 CGF.Builder.CreateStore(Target,
847 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
848
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000849 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000850 ObjCSuper, ObjCTypes.SuperPtrCTy,
851 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000852}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000853
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000854/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000855CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000856 QualType ResultType,
857 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000858 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000859 bool IsClassMessage,
860 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000861 llvm::Value *Arg0 =
862 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000863 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000864 Arg0, CGF.getContext().getObjCIdType(),
865 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000866}
867
868CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000869 QualType ResultType,
870 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000871 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000872 QualType Arg0Ty,
873 bool IsSuper,
874 const CallArgList &CallArgs) {
875 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000876 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
877 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
878 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000879 CGF.getContext().getObjCSelType()));
880 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000881
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000882 CodeGenTypes &Types = CGM.getTypes();
883 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
884 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000885
886 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000887 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000888 Fn = ObjCTypes.getSendStretFn(IsSuper);
889 } else if (ResultType->isFloatingType()) {
890 // FIXME: Sadly, this is wrong. This actually depends on the
891 // architecture. This happens to be right for x86-32 though.
892 Fn = ObjCTypes.getSendFpretFn(IsSuper);
893 } else {
894 Fn = ObjCTypes.getSendFn(IsSuper);
895 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000896 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000897 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000898}
899
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000900llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000901 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000902 // FIXME: I don't understand why gcc generates this, or where it is
903 // resolved. Investigate. Its also wasteful to look this up over and
904 // over.
905 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
906
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000907 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
908 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000909}
910
Fariborz Jahanianda320092009-01-29 19:24:30 +0000911void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000912 // FIXME: We shouldn't need this, the protocol decl should contain
913 // enough information to tell us whether this was a declaration or a
914 // definition.
915 DefinedProtocols.insert(PD->getIdentifier());
916
917 // If we have generated a forward reference to this protocol, emit
918 // it now. Otherwise do nothing, the protocol objects are lazily
919 // emitted.
920 if (Protocols.count(PD->getIdentifier()))
921 GetOrEmitProtocol(PD);
922}
923
Fariborz Jahanianda320092009-01-29 19:24:30 +0000924llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000925 if (DefinedProtocols.count(PD->getIdentifier()))
926 return GetOrEmitProtocol(PD);
927 return GetOrEmitProtocolRef(PD);
928}
929
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000930/*
931 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
932 struct _objc_protocol {
933 struct _objc_protocol_extension *isa;
934 char *protocol_name;
935 struct _objc_protocol_list *protocol_list;
936 struct _objc__method_prototype_list *instance_methods;
937 struct _objc__method_prototype_list *class_methods
938 };
939
940 See EmitProtocolExtension().
941*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000942llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
943 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
944
945 // Early exit if a defining object has already been generated.
946 if (Entry && Entry->hasInitializer())
947 return Entry;
948
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000949 // FIXME: I don't understand why gcc generates this, or where it is
950 // resolved. Investigate. Its also wasteful to look this up over and
951 // over.
952 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
953
Chris Lattner8ec03f52008-11-24 03:54:41 +0000954 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000955
956 // Construct method lists.
957 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
958 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
959 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
960 e = PD->instmeth_end(); i != e; ++i) {
961 ObjCMethodDecl *MD = *i;
962 llvm::Constant *C = GetMethodDescriptionConstant(MD);
963 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
964 OptInstanceMethods.push_back(C);
965 } else {
966 InstanceMethods.push_back(C);
967 }
968 }
969
970 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
971 e = PD->classmeth_end(); i != e; ++i) {
972 ObjCMethodDecl *MD = *i;
973 llvm::Constant *C = GetMethodDescriptionConstant(MD);
974 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
975 OptClassMethods.push_back(C);
976 } else {
977 ClassMethods.push_back(C);
978 }
979 }
980
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000981 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000982 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000984 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000985 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000986 PD->protocol_begin(),
987 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000988 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000989 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
990 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000991 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
992 InstanceMethods);
993 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000994 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
995 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000996 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
997 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
999 Values);
1000
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001002 // Already created, fix the linkage and update the initializer.
1003 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001004 Entry->setInitializer(Init);
1005 } else {
1006 Entry =
1007 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1008 llvm::GlobalValue::InternalLinkage,
1009 Init,
1010 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1011 &CGM.getModule());
1012 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1013 UsedGlobals.push_back(Entry);
1014 // FIXME: Is this necessary? Why only for protocol?
1015 Entry->setAlignment(4);
1016 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001017
1018 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019}
1020
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001021llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1023
1024 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001025 // We use the initializer as a marker of whether this is a forward
1026 // reference or not. At module finalization we add the empty
1027 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001028 Entry =
1029 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001030 llvm::GlobalValue::ExternalLinkage,
1031 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001032 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001033 &CGM.getModule());
1034 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1035 UsedGlobals.push_back(Entry);
1036 // FIXME: Is this necessary? Why only for protocol?
1037 Entry->setAlignment(4);
1038 }
1039
1040 return Entry;
1041}
1042
1043/*
1044 struct _objc_protocol_extension {
1045 uint32_t size;
1046 struct objc_method_description_list *optional_instance_methods;
1047 struct objc_method_description_list *optional_class_methods;
1048 struct objc_property_list *instance_properties;
1049 };
1050*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001051llvm::Constant *
1052CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1053 const ConstantVector &OptInstanceMethods,
1054 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001055 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001056 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001057 std::vector<llvm::Constant*> Values(4);
1058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001059 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001060 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1061 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001062 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1063 OptInstanceMethods);
1064 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001065 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1066 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001067 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1068 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001069 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1070 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001071 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001074 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1075 Values[3]->isNullValue())
1076 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1077
1078 llvm::Constant *Init =
1079 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1080 llvm::GlobalVariable *GV =
1081 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1082 llvm::GlobalValue::InternalLinkage,
1083 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001084 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085 &CGM.getModule());
1086 // No special section, but goes in llvm.used
1087 UsedGlobals.push_back(GV);
1088
1089 return GV;
1090}
1091
1092/*
1093 struct objc_protocol_list {
1094 struct objc_protocol_list *next;
1095 long count;
1096 Protocol *list[];
1097 };
1098*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001099llvm::Constant *
1100CGObjCMac::EmitProtocolList(const std::string &Name,
1101 ObjCProtocolDecl::protocol_iterator begin,
1102 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001103 std::vector<llvm::Constant*> ProtocolRefs;
1104
Daniel Dunbardbc93372008-08-21 21:57:41 +00001105 for (; begin != end; ++begin)
1106 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107
1108 // Just return null for empty protocol lists
1109 if (ProtocolRefs.empty())
1110 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1111
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001112 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001113 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1114
1115 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001116 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001117 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1118 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1119 Values[2] =
1120 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1121 ProtocolRefs.size()),
1122 ProtocolRefs);
1123
1124 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1125 llvm::GlobalVariable *GV =
1126 new llvm::GlobalVariable(Init->getType(), false,
1127 llvm::GlobalValue::InternalLinkage,
1128 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001129 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 &CGM.getModule());
1131 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1132 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1133}
1134
1135/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001136 struct _objc_property {
1137 const char * const name;
1138 const char * const attributes;
1139 };
1140
1141 struct _objc_property_list {
1142 uint32_t entsize; // sizeof (struct _objc_property)
1143 uint32_t prop_count;
1144 struct _objc_property[prop_count];
1145 };
1146*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001147llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1148 const Decl *Container,
1149 const ObjCContainerDecl *OCD,
1150 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001151 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001152 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1153 E = OCD->prop_end(); I != E; ++I) {
1154 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001155 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001156 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001157 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1158 Prop));
1159 }
1160
1161 // Return null for empty list.
1162 if (Properties.empty())
1163 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1164
1165 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001166 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001167 std::vector<llvm::Constant*> Values(3);
1168 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1169 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1170 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1171 Properties.size());
1172 Values[2] = llvm::ConstantArray::get(AT, Properties);
1173 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1174
1175 llvm::GlobalVariable *GV =
1176 new llvm::GlobalVariable(Init->getType(), false,
1177 llvm::GlobalValue::InternalLinkage,
1178 Init,
1179 Name,
1180 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001181 if (ObjCABI == 2)
1182 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001183 // No special section on property lists?
1184 UsedGlobals.push_back(GV);
1185 return llvm::ConstantExpr::getBitCast(GV,
1186 ObjCTypes.PropertyListPtrTy);
1187
1188}
1189
1190/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191 struct objc_method_description_list {
1192 int count;
1193 struct objc_method_description list[];
1194 };
1195*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001196llvm::Constant *
1197CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1198 std::vector<llvm::Constant*> Desc(2);
1199 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1200 ObjCTypes.SelectorPtrTy);
1201 Desc[1] = GetMethodVarType(MD);
1202 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1203 Desc);
1204}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001205
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001206llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1207 const char *Section,
1208 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001209 // Return null for empty list.
1210 if (Methods.empty())
1211 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1212
1213 std::vector<llvm::Constant*> Values(2);
1214 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1215 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1216 Methods.size());
1217 Values[1] = llvm::ConstantArray::get(AT, Methods);
1218 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1219
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001220 llvm::GlobalVariable *GV =
1221 new llvm::GlobalVariable(Init->getType(), false,
1222 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001223 Init, Name, &CGM.getModule());
1224 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001225 UsedGlobals.push_back(GV);
1226 return llvm::ConstantExpr::getBitCast(GV,
1227 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001228}
1229
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001230/*
1231 struct _objc_category {
1232 char *category_name;
1233 char *class_name;
1234 struct _objc_method_list *instance_methods;
1235 struct _objc_method_list *class_methods;
1236 struct _objc_protocol_list *protocols;
1237 uint32_t size; // <rdar://4585769>
1238 struct _objc_property_list *instance_properties;
1239 };
1240 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001241void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001242 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001243
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001244 // FIXME: This is poor design, the OCD should have a pointer to the
1245 // category decl. Additionally, note that Category can be null for
1246 // the @implementation w/o an @interface case. Sema should just
1247 // create one for us as it does for @implementation so everyone else
1248 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001249 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001250 const ObjCCategoryDecl *Category =
1251 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001252 std::string ExtName(Interface->getNameAsString() + "_" +
1253 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001254
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001255 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1256 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1257 e = OCD->instmeth_end(); i != e; ++i) {
1258 // Instance methods should always be defined.
1259 InstanceMethods.push_back(GetMethodConstant(*i));
1260 }
1261 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1262 e = OCD->classmeth_end(); i != e; ++i) {
1263 // Class methods should always be defined.
1264 ClassMethods.push_back(GetMethodConstant(*i));
1265 }
1266
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001267 std::vector<llvm::Constant*> Values(7);
1268 Values[0] = GetClassName(OCD->getIdentifier());
1269 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001270 Values[2] =
1271 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1272 ExtName,
1273 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001274 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001275 Values[3] =
1276 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1277 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001278 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001279 if (Category) {
1280 Values[4] =
1281 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1282 Category->protocol_begin(),
1283 Category->protocol_end());
1284 } else {
1285 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1286 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001287 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001288
1289 // If there is no category @interface then there can be no properties.
1290 if (Category) {
1291 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001292 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001293 } else {
1294 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1295 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001296
1297 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1298 Values);
1299
1300 llvm::GlobalVariable *GV =
1301 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1302 llvm::GlobalValue::InternalLinkage,
1303 Init,
1304 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1305 &CGM.getModule());
1306 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1307 UsedGlobals.push_back(GV);
1308 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001309}
1310
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001311// FIXME: Get from somewhere?
1312enum ClassFlags {
1313 eClassFlags_Factory = 0x00001,
1314 eClassFlags_Meta = 0x00002,
1315 // <rdr://5142207>
1316 eClassFlags_HasCXXStructors = 0x02000,
1317 eClassFlags_Hidden = 0x20000,
1318 eClassFlags_ABI2_Hidden = 0x00010,
1319 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1320};
1321
1322// <rdr://5142207&4705298&4843145>
1323static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1324 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1325 // FIXME: Support -fvisibility
1326 switch (attr->getVisibility()) {
1327 default:
1328 assert(0 && "Unknown visibility");
1329 return false;
1330 case VisibilityAttr::DefaultVisibility:
1331 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1332 return false;
1333 case VisibilityAttr::HiddenVisibility:
1334 return true;
1335 }
1336 } else {
1337 return false; // FIXME: Support -fvisibility
1338 }
1339}
1340
1341/*
1342 struct _objc_class {
1343 Class isa;
1344 Class super_class;
1345 const char *name;
1346 long version;
1347 long info;
1348 long instance_size;
1349 struct _objc_ivar_list *ivars;
1350 struct _objc_method_list *methods;
1351 struct _objc_cache *cache;
1352 struct _objc_protocol_list *protocols;
1353 // Objective-C 1.0 extensions (<rdr://4585769>)
1354 const char *ivar_layout;
1355 struct _objc_class_ext *ext;
1356 };
1357
1358 See EmitClassExtension();
1359 */
1360void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001361 DefinedSymbols.insert(ID->getIdentifier());
1362
Chris Lattner8ec03f52008-11-24 03:54:41 +00001363 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001364 // FIXME: Gross
1365 ObjCInterfaceDecl *Interface =
1366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001367 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001368 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001369 Interface->protocol_begin(),
1370 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001371 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001372 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001373 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001374 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001375
1376 // FIXME: Set CXX-structors flag.
1377 if (IsClassHidden(ID->getClassInterface()))
1378 Flags |= eClassFlags_Hidden;
1379
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001380 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1381 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1382 e = ID->instmeth_end(); i != e; ++i) {
1383 // Instance methods should always be defined.
1384 InstanceMethods.push_back(GetMethodConstant(*i));
1385 }
1386 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1387 e = ID->classmeth_end(); i != e; ++i) {
1388 // Class methods should always be defined.
1389 ClassMethods.push_back(GetMethodConstant(*i));
1390 }
1391
1392 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1393 e = ID->propimpl_end(); i != e; ++i) {
1394 ObjCPropertyImplDecl *PID = *i;
1395
1396 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1397 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1398
1399 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1400 if (llvm::Constant *C = GetMethodConstant(MD))
1401 InstanceMethods.push_back(C);
1402 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1403 if (llvm::Constant *C = GetMethodConstant(MD))
1404 InstanceMethods.push_back(C);
1405 }
1406 }
1407
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001408 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001409 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001410 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001411 // Record a reference to the super class.
1412 LazySymbols.insert(Super->getIdentifier());
1413
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001414 Values[ 1] =
1415 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1416 ObjCTypes.ClassPtrTy);
1417 } else {
1418 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1419 }
1420 Values[ 2] = GetClassName(ID->getIdentifier());
1421 // Version is always 0.
1422 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1423 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1424 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001425 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001426 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001427 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001428 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001429 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001430 // cache is always NULL.
1431 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1432 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001433 // FIXME: Set ivar_layout
1434 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001435 Values[11] = EmitClassExtension(ID);
1436 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1437 Values);
1438
1439 llvm::GlobalVariable *GV =
1440 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1441 llvm::GlobalValue::InternalLinkage,
1442 Init,
1443 std::string("\01L_OBJC_CLASS_")+ClassName,
1444 &CGM.getModule());
1445 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1446 UsedGlobals.push_back(GV);
1447 // FIXME: Why?
1448 GV->setAlignment(32);
1449 DefinedClasses.push_back(GV);
1450}
1451
1452llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1453 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001454 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001455 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001456 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001457 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001458
1459 if (IsClassHidden(ID->getClassInterface()))
1460 Flags |= eClassFlags_Hidden;
1461
1462 std::vector<llvm::Constant*> Values(12);
1463 // The isa for the metaclass is the root of the hierarchy.
1464 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1465 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1466 Root = Super;
1467 Values[ 0] =
1468 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1469 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001470 // The super class for the metaclass is emitted as the name of the
1471 // super class. The runtime fixes this up to point to the
1472 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001473 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1474 Values[ 1] =
1475 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1476 ObjCTypes.ClassPtrTy);
1477 } else {
1478 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1479 }
1480 Values[ 2] = GetClassName(ID->getIdentifier());
1481 // Version is always 0.
1482 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1483 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1484 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001485 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001486 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001487 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001488 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001489 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001490 // cache is always NULL.
1491 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1492 Values[ 9] = Protocols;
1493 // ivar_layout for metaclass is always NULL.
1494 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1495 // The class extension is always unused for metaclasses.
1496 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1497 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1498 Values);
1499
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001500 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001501 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001502
1503 // Check for a forward reference.
1504 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1505 if (GV) {
1506 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1507 "Forward metaclass reference has incorrect type.");
1508 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1509 GV->setInitializer(Init);
1510 } else {
1511 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1512 llvm::GlobalValue::InternalLinkage,
1513 Init, Name,
1514 &CGM.getModule());
1515 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1517 UsedGlobals.push_back(GV);
1518 // FIXME: Why?
1519 GV->setAlignment(32);
1520
1521 return GV;
1522}
1523
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001524llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001525 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001526
1527 // FIXME: Should we look these up somewhere other than the
1528 // module. Its a bit silly since we only generate these while
1529 // processing an implementation, so exactly one pointer would work
1530 // if know when we entered/exitted an implementation block.
1531
1532 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001533 // Previously, metaclass with internal linkage may have been defined.
1534 // pass 'true' as 2nd argument so it is returned.
1535 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001536 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1537 "Forward metaclass reference has incorrect type.");
1538 return GV;
1539 } else {
1540 // Generate as an external reference to keep a consistent
1541 // module. This will be patched up when we emit the metaclass.
1542 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1543 llvm::GlobalValue::ExternalLinkage,
1544 0,
1545 Name,
1546 &CGM.getModule());
1547 }
1548}
1549
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001550/*
1551 struct objc_class_ext {
1552 uint32_t size;
1553 const char *weak_ivar_layout;
1554 struct _objc_property_list *properties;
1555 };
1556*/
1557llvm::Constant *
1558CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1559 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001560 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001561
1562 std::vector<llvm::Constant*> Values(3);
1563 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001564 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001565 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001566 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001567 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001568
1569 // Return null if no extension bits are used.
1570 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1571 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1572
1573 llvm::Constant *Init =
1574 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1575 llvm::GlobalVariable *GV =
1576 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1577 llvm::GlobalValue::InternalLinkage,
1578 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001579 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001580 &CGM.getModule());
1581 // No special section, but goes in llvm.used
1582 UsedGlobals.push_back(GV);
1583
1584 return GV;
1585}
1586
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001587/// countInheritedIvars - count number of ivars in class and its super class(s)
1588///
1589static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1590 int count = 0;
1591 if (!OI)
1592 return 0;
1593 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1594 if (SuperClass)
1595 count += countInheritedIvars(SuperClass);
1596 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1597 E = OI->ivar_end(); I != E; ++I)
1598 ++count;
1599 return count;
1600}
1601
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001602/// getInterfaceDeclForIvar - Get the interface declaration node where
1603/// this ivar is declared in.
1604/// FIXME. Ideally, this info should be in the ivar node. But currently
1605/// it is not and prevailing wisdom is that ASTs should not have more
1606/// info than is absolutely needed, even though this info reflects the
1607/// source language.
1608///
1609static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1610 const ObjCInterfaceDecl *OI,
1611 const ObjCIvarDecl *IVD) {
1612 if (!OI)
1613 return 0;
1614 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1615 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1616 E = OI->ivar_end(); I != E; ++I)
1617 if ((*I)->getIdentifier() == IVD->getIdentifier())
1618 return OI;
1619 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1620}
1621
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001622/*
1623 struct objc_ivar {
1624 char *ivar_name;
1625 char *ivar_type;
1626 int ivar_offset;
1627 };
1628
1629 struct objc_ivar_list {
1630 int ivar_count;
1631 struct objc_ivar list[count];
1632 };
1633 */
1634llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001635 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001636 std::vector<llvm::Constant*> Ivars, Ivar(3);
1637
1638 // When emitting the root class GCC emits ivar entries for the
1639 // actual class structure. It is not clear if we need to follow this
1640 // behavior; for now lets try and get away with not doing it. If so,
1641 // the cleanest solution would be to make up an ObjCInterfaceDecl
1642 // for the class.
1643 if (ForClass)
1644 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001645
1646 ObjCInterfaceDecl *OID =
1647 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1648 const llvm::Type *InterfaceTy =
1649 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001650 const llvm::StructLayout *Layout =
1651 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001652
1653 RecordDecl::field_iterator ifield, pfield;
1654 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001655 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1656 FieldDecl *Field = *ifield;
1657 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1658 getLLVMFieldNo(Field));
1659 if (Field->getIdentifier())
1660 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1661 else
1662 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001663 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001664 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001665 Ivar[1] = GetMethodVarType(TypeStr);
1666 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001667 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001668 }
1669
1670 // Return null for empty list.
1671 if (Ivars.empty())
1672 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1673
1674 std::vector<llvm::Constant*> Values(2);
1675 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1676 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1677 Ivars.size());
1678 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1679 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1680
1681 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1682 "\01L_OBJC_INSTANCE_VARIABLES_");
1683 llvm::GlobalVariable *GV =
1684 new llvm::GlobalVariable(Init->getType(), false,
1685 llvm::GlobalValue::InternalLinkage,
1686 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001687 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001688 &CGM.getModule());
1689 if (ForClass) {
1690 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1691 // FIXME: Why is this only here?
1692 GV->setAlignment(32);
1693 } else {
1694 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1695 }
1696 UsedGlobals.push_back(GV);
1697 return llvm::ConstantExpr::getBitCast(GV,
1698 ObjCTypes.IvarListPtrTy);
1699}
1700
1701/*
1702 struct objc_method {
1703 SEL method_name;
1704 char *method_types;
1705 void *method;
1706 };
1707
1708 struct objc_method_list {
1709 struct objc_method_list *obsolete;
1710 int count;
1711 struct objc_method methods_list[count];
1712 };
1713*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001714
1715/// GetMethodConstant - Return a struct objc_method constant for the
1716/// given method if it has been defined. The result is null if the
1717/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001718llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001719 // FIXME: Use DenseMap::lookup
1720 llvm::Function *Fn = MethodDefinitions[MD];
1721 if (!Fn)
1722 return 0;
1723
1724 std::vector<llvm::Constant*> Method(3);
1725 Method[0] =
1726 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1727 ObjCTypes.SelectorPtrTy);
1728 Method[1] = GetMethodVarType(MD);
1729 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1730 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1731}
1732
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001733llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1734 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001735 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001736 // Return null for empty list.
1737 if (Methods.empty())
1738 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1739
1740 std::vector<llvm::Constant*> Values(3);
1741 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1742 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1743 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1744 Methods.size());
1745 Values[2] = llvm::ConstantArray::get(AT, Methods);
1746 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1747
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001748 llvm::GlobalVariable *GV =
1749 new llvm::GlobalVariable(Init->getType(), false,
1750 llvm::GlobalValue::InternalLinkage,
1751 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001752 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001753 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001754 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001755 UsedGlobals.push_back(GV);
1756 return llvm::ConstantExpr::getBitCast(GV,
1757 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001758}
1759
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001760llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001761 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001762 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001763 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001764
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001765 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001766 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001767 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001768 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001769 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001770 llvm::GlobalValue::InternalLinkage,
1771 Name,
1772 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001773 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001774
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001775 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001776}
1777
1778llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001779 // Abuse this interface function as a place to finalize.
1780 FinishModule();
1781
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001782 return NULL;
1783}
1784
Daniel Dunbar49f66022008-09-24 03:38:44 +00001785llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1786 return ObjCTypes.GetPropertyFn;
1787}
1788
1789llvm::Function *CGObjCMac::GetPropertySetFunction() {
1790 return ObjCTypes.SetPropertyFn;
1791}
1792
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001793llvm::Function *CGObjCMac::EnumerationMutationFunction()
1794{
1795 return ObjCTypes.EnumerationMutationFn;
1796}
1797
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001798/*
1799
1800Objective-C setjmp-longjmp (sjlj) Exception Handling
1801--
1802
1803The basic framework for a @try-catch-finally is as follows:
1804{
1805 objc_exception_data d;
1806 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001807 bool _call_try_exit = true;
1808
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001809 objc_exception_try_enter(&d);
1810 if (!setjmp(d.jmp_buf)) {
1811 ... try body ...
1812 } else {
1813 // exception path
1814 id _caught = objc_exception_extract(&d);
1815
1816 // enter new try scope for handlers
1817 if (!setjmp(d.jmp_buf)) {
1818 ... match exception and execute catch blocks ...
1819
1820 // fell off end, rethrow.
1821 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001822 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001823 } else {
1824 // exception in catch block
1825 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001826 _call_try_exit = false;
1827 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001828 }
1829 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001830 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001831
1832finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001833 if (_call_try_exit)
1834 objc_exception_try_exit(&d);
1835
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001836 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001837 ... dispatch to finally destination ...
1838
1839finally_rethrow:
1840 objc_exception_throw(_rethrow);
1841
1842finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001843}
1844
1845This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001846uses _rethrow to determine if objc_exception_try_exit should be called
1847and if the object should be rethrown. This breaks in the face of
1848throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001849
1850We specialize this framework for a few particular circumstances:
1851
1852 - If there are no catch blocks, then we avoid emitting the second
1853 exception handling context.
1854
1855 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1856 e)) we avoid emitting the code to rethrow an uncaught exception.
1857
1858 - FIXME: If there is no @finally block we can do a few more
1859 simplifications.
1860
1861Rethrows and Jumps-Through-Finally
1862--
1863
1864Support for implicit rethrows and jumping through the finally block is
1865handled by storing the current exception-handling context in
1866ObjCEHStack.
1867
Daniel Dunbar898d5082008-09-30 01:06:03 +00001868In order to implement proper @finally semantics, we support one basic
1869mechanism for jumping through the finally block to an arbitrary
1870destination. Constructs which generate exits from a @try or @catch
1871block use this mechanism to implement the proper semantics by chaining
1872jumps, as necessary.
1873
1874This mechanism works like the one used for indirect goto: we
1875arbitrarily assign an ID to each destination and store the ID for the
1876destination in a variable prior to entering the finally block. At the
1877end of the finally block we simply create a switch to the proper
1878destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001879
1880Code gen for @synchronized(expr) stmt;
1881Effectively generating code for:
1882objc_sync_enter(expr);
1883@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001884*/
1885
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001886void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1887 const Stmt &S) {
1888 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001889 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001890 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001891 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001892 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1893 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1894 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00001895
1896 // For @synchronized, call objc_sync_enter(sync.expr). The
1897 // evaluation of the expression must occur before we enter the
1898 // @synchronized. We can safely avoid a temp here because jumps into
1899 // @synchronized are illegal & this will dominate uses.
1900 llvm::Value *SyncArg = 0;
1901 if (!isTry) {
1902 SyncArg =
1903 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1904 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
1905 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
1906 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001907
1908 // Push an EH context entry, used for handling rethrows and jumps
1909 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001910 CGF.PushCleanupBlock(FinallyBlock);
1911
Anders Carlsson273558f2009-02-07 21:37:21 +00001912 CGF.ObjCEHValueStack.push_back(0);
1913
Daniel Dunbar898d5082008-09-30 01:06:03 +00001914 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001915 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1916 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001917 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1918 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001919 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1920 "_call_try_exit");
1921 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1922
Anders Carlsson80f25672008-09-09 17:59:25 +00001923 // Enter a new try block and call setjmp.
1924 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1925 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1926 "jmpbufarray");
1927 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1928 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1929 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001930
Daniel Dunbar55e87422008-11-11 02:29:29 +00001931 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1932 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001933 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001934 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001935
1936 // Emit the @try block.
1937 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001938 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1939 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001940 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001941
1942 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001943 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001944
1945 // Retrieve the exception object. We may emit multiple blocks but
1946 // nothing can cross this so the value is already in SSA form.
1947 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1948 ExceptionData,
1949 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001950 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001951 if (!isTry)
1952 {
1953 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001954 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001955 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001956 }
1957 else if (const ObjCAtCatchStmt* CatchStmt =
1958 cast<ObjCAtTryStmt>(S).getCatchStmts())
1959 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001960 // Enter a new exception try block (in case a @catch block throws
1961 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001962 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001963
Anders Carlsson80f25672008-09-09 17:59:25 +00001964 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1965 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001966 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001967
Daniel Dunbar55e87422008-11-11 02:29:29 +00001968 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1969 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001970 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001971
1972 CGF.EmitBlock(CatchBlock);
1973
Daniel Dunbar55e40722008-09-27 07:03:52 +00001974 // Handle catch list. As a special case we check if everything is
1975 // matched and avoid generating code for falling off the end if
1976 // so.
1977 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001978 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001979 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001980
Anders Carlssondde0a942008-09-11 09:15:33 +00001981 const DeclStmt *CatchParam =
1982 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001983 const VarDecl *VD = 0;
1984 const PointerType *PT = 0;
1985
Anders Carlsson80f25672008-09-09 17:59:25 +00001986 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001987 if (!CatchParam) {
1988 AllMatched = true;
1989 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001990 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001991 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001992
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001993 // catch(id e) always matches.
1994 // FIXME: For the time being we also match id<X>; this should
1995 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00001996 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001997 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001998 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001999 }
2000
Daniel Dunbar55e40722008-09-27 07:03:52 +00002001 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002002 if (CatchParam) {
2003 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002004 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002005 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002006 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002007
Anders Carlssondde0a942008-09-11 09:15:33 +00002008 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002009 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002010 break;
2011 }
2012
Daniel Dunbar129271a2008-09-27 07:36:24 +00002013 assert(PT && "Unexpected non-pointer type in @catch");
2014 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002015 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002016 assert(ObjCType && "Catch parameter must have Objective-C type!");
2017
2018 // Check if the @catch block matches the exception object.
2019 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2020
Anders Carlsson80f25672008-09-09 17:59:25 +00002021 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2022 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002023
Daniel Dunbar55e87422008-11-11 02:29:29 +00002024 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002025
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002026 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002027 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002028
2029 // Emit the @catch block.
2030 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002031 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002032 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002033
2034 llvm::Value *Tmp =
2035 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2036 "tmp");
2037 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002038
2039 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002040 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002041
2042 CGF.EmitBlock(NextCatchBlock);
2043 }
2044
Daniel Dunbar55e40722008-09-27 07:03:52 +00002045 if (!AllMatched) {
2046 // None of the handlers caught the exception, so store it to be
2047 // rethrown at the end of the @finally block.
2048 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002049 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002050 }
2051
2052 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002053 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002054 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2055 ExceptionData),
2056 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002057 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002058 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002059 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002060 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002061 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002062 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002063 }
2064
Daniel Dunbar898d5082008-09-30 01:06:03 +00002065 // Pop the exception-handling stack entry. It is important to do
2066 // this now, because the code in the @finally block is not in this
2067 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002068 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2069
Anders Carlsson273558f2009-02-07 21:37:21 +00002070 CGF.ObjCEHValueStack.pop_back();
2071
Anders Carlsson80f25672008-09-09 17:59:25 +00002072 // Emit the @finally block.
2073 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002074 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2075
2076 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2077
2078 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002079 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002080
2081 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002082 if (isTry) {
2083 if (const ObjCAtFinallyStmt* FinallyStmt =
2084 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2085 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002086 } else {
2087 // Emit objc_sync_exit(expr); as finally's sole statement for
2088 // @synchronized.
2089 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002090 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002091
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002092 // Emit the switch block
2093 if (Info.SwitchBlock)
2094 CGF.EmitBlock(Info.SwitchBlock);
2095 if (Info.EndBlock)
2096 CGF.EmitBlock(Info.EndBlock);
2097
Daniel Dunbar898d5082008-09-30 01:06:03 +00002098 CGF.EmitBlock(FinallyRethrow);
2099 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2100 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002101 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002102
2103 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002104}
2105
2106void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002107 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002108 llvm::Value *ExceptionAsObject;
2109
2110 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2111 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2112 ExceptionAsObject =
2113 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2114 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002115 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002116 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002117 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002118 }
2119
2120 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002121 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002122
2123 // Clear the insertion point to indicate we are in unreachable code.
2124 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002125}
2126
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002127/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002128/// object: objc_read_weak (id *src)
2129///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002130llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002131 llvm::Value *AddrWeakObj)
2132{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002133 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002134 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002135 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002136 return read_weak;
2137}
2138
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002139/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2140/// objc_assign_weak (id src, id *dst)
2141///
2142void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2143 llvm::Value *src, llvm::Value *dst)
2144{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002145 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2146 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002147 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2148 src, dst, "weakassign");
2149 return;
2150}
2151
Fariborz Jahanian58626502008-11-19 00:59:10 +00002152/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2153/// objc_assign_global (id src, id *dst)
2154///
2155void CGObjCMac::EmitObjCGlobalAssign(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 Jahanian58626502008-11-19 00:59:10 +00002160 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2161 src, dst, "globalassign");
2162 return;
2163}
2164
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002165/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2166/// objc_assign_ivar (id src, id *dst)
2167///
2168void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2169 llvm::Value *src, llvm::Value *dst)
2170{
2171 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2172 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2173 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2174 src, dst, "assignivar");
2175 return;
2176}
2177
Fariborz Jahanian58626502008-11-19 00:59:10 +00002178/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2179/// objc_assign_strongCast (id src, id *dst)
2180///
2181void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2182 llvm::Value *src, llvm::Value *dst)
2183{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002184 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2185 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002186 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2187 src, dst, "weakassign");
2188 return;
2189}
2190
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002191/// EmitObjCValueForIvar - Code Gen for ivar reference.
2192///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002193LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2194 QualType ObjectTy,
2195 llvm::Value *BaseValue,
2196 const ObjCIvarDecl *Ivar,
2197 const FieldDecl *Field,
2198 unsigned CVRQualifiers) {
2199 if (Ivar->isBitField())
2200 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2201 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002202 // TODO: Add a special case for isa (index 0)
2203 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2204 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002205 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002206 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2207 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002208 LValue::SetObjCIvar(LV, true);
2209 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002210}
2211
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002212llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2213 ObjCInterfaceDecl *Interface,
2214 const ObjCIvarDecl *Ivar) {
2215 const llvm::Type *InterfaceLTy =
2216 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2217 const llvm::StructLayout *Layout =
2218 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2219 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2220 uint64_t Offset =
2221 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2222
2223 return llvm::ConstantInt::get(
2224 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2225 Offset);
2226}
2227
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002228/* *** Private Interface *** */
2229
2230/// EmitImageInfo - Emit the image info marker used to encode some module
2231/// level information.
2232///
2233/// See: <rdr://4810609&4810587&4810587>
2234/// struct IMAGE_INFO {
2235/// unsigned version;
2236/// unsigned flags;
2237/// };
2238enum ImageInfoFlags {
2239 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2240 eImageInfo_GarbageCollected = (1 << 1),
2241 eImageInfo_GCOnly = (1 << 2)
2242};
2243
2244void CGObjCMac::EmitImageInfo() {
2245 unsigned version = 0; // Version is unused?
2246 unsigned flags = 0;
2247
2248 // FIXME: Fix and continue?
2249 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2250 flags |= eImageInfo_GarbageCollected;
2251 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2252 flags |= eImageInfo_GCOnly;
2253
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002254 // Emitted as int[2];
2255 llvm::Constant *values[2] = {
2256 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2257 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2258 };
2259 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002260 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002261 new llvm::GlobalVariable(AT, true,
2262 llvm::GlobalValue::InternalLinkage,
2263 llvm::ConstantArray::get(AT, values, 2),
2264 "\01L_OBJC_IMAGE_INFO",
2265 &CGM.getModule());
2266
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002267 if (ObjCABI == 1) {
2268 GV->setSection("__OBJC, __image_info,regular");
2269 } else {
2270 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2271 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002272
2273 UsedGlobals.push_back(GV);
2274}
2275
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002276
2277// struct objc_module {
2278// unsigned long version;
2279// unsigned long size;
2280// const char *name;
2281// Symtab symtab;
2282// };
2283
2284// FIXME: Get from somewhere
2285static const int ModuleVersion = 7;
2286
2287void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002288 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002289
2290 std::vector<llvm::Constant*> Values(4);
2291 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2292 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002293 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002294 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002295 Values[3] = EmitModuleSymbols();
2296
2297 llvm::GlobalVariable *GV =
2298 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2299 llvm::GlobalValue::InternalLinkage,
2300 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2301 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002302 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002303 &CGM.getModule());
2304 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2305 UsedGlobals.push_back(GV);
2306}
2307
2308llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 unsigned NumClasses = DefinedClasses.size();
2310 unsigned NumCategories = DefinedCategories.size();
2311
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002312 // Return null if no symbols were defined.
2313 if (!NumClasses && !NumCategories)
2314 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2315
2316 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2318 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2319 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2320 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2321
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002322 // The runtime expects exactly the list of defined classes followed
2323 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002324 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002325 for (unsigned i=0; i<NumClasses; i++)
2326 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2327 ObjCTypes.Int8PtrTy);
2328 for (unsigned i=0; i<NumCategories; i++)
2329 Symbols[NumClasses + i] =
2330 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2331 ObjCTypes.Int8PtrTy);
2332
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002334 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335 NumClasses + NumCategories),
2336 Symbols);
2337
2338 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2339
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002340 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002341 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002342 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002344 "\01L_OBJC_SYMBOLS",
2345 &CGM.getModule());
2346 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2347 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002348 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2349}
2350
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002351llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002352 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002353 LazySymbols.insert(ID->getIdentifier());
2354
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2356
2357 if (!Entry) {
2358 llvm::Constant *Casted =
2359 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2360 ObjCTypes.ClassPtrTy);
2361 Entry =
2362 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2363 llvm::GlobalValue::InternalLinkage,
2364 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2365 &CGM.getModule());
2366 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2367 UsedGlobals.push_back(Entry);
2368 }
2369
2370 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002371}
2372
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002373llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002374 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2375
2376 if (!Entry) {
2377 llvm::Constant *Casted =
2378 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2379 ObjCTypes.SelectorPtrTy);
2380 Entry =
2381 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2382 llvm::GlobalValue::InternalLinkage,
2383 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2384 &CGM.getModule());
2385 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2386 UsedGlobals.push_back(Entry);
2387 }
2388
2389 return Builder.CreateLoad(Entry, false, "tmp");
2390}
2391
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002392llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002393 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002394
2395 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002396 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002397 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002398 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002399 llvm::GlobalValue::InternalLinkage,
2400 C, "\01L_OBJC_CLASS_NAME_",
2401 &CGM.getModule());
2402 Entry->setSection("__TEXT,__cstring,cstring_literals");
2403 UsedGlobals.push_back(Entry);
2404 }
2405
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002406 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002407}
2408
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002409llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002410 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2411
2412 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002413 // FIXME: Avoid std::string copying.
2414 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002415 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002416 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002417 llvm::GlobalValue::InternalLinkage,
2418 C, "\01L_OBJC_METH_VAR_NAME_",
2419 &CGM.getModule());
2420 Entry->setSection("__TEXT,__cstring,cstring_literals");
2421 UsedGlobals.push_back(Entry);
2422 }
2423
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002424 return getConstantGEP(Entry, 0, 0);
2425}
2426
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002428llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2430}
2431
2432// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002433llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2435}
2436
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002437llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002439
2440 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002442 Entry =
2443 new llvm::GlobalVariable(C->getType(), false,
2444 llvm::GlobalValue::InternalLinkage,
2445 C, "\01L_OBJC_METH_VAR_TYPE_",
2446 &CGM.getModule());
2447 Entry->setSection("__TEXT,__cstring,cstring_literals");
2448 UsedGlobals.push_back(Entry);
2449 }
2450
2451 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002452}
2453
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002455llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002456 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002457 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2458 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 return GetMethodVarType(TypeStr);
2460}
2461
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002462// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002463llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002464 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2465
2466 if (!Entry) {
2467 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2468 Entry =
2469 new llvm::GlobalVariable(C->getType(), false,
2470 llvm::GlobalValue::InternalLinkage,
2471 C, "\01L_OBJC_PROP_NAME_ATTR_",
2472 &CGM.getModule());
2473 Entry->setSection("__TEXT,__cstring,cstring_literals");
2474 UsedGlobals.push_back(Entry);
2475 }
2476
2477 return getConstantGEP(Entry, 0, 0);
2478}
2479
2480// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002481// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002482llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002483 const Decl *Container) {
2484 std::string TypeStr;
2485 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002486 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2487}
2488
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002489void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2490 const ObjCContainerDecl *CD,
2491 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002492 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002493 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002494 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002495 assert (CD && "Missing container decl in GetNameForMethod");
2496 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002497 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2498 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002499 NameOut += ' ';
2500 NameOut += D->getSelector().getAsString();
2501 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002502}
2503
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002504/// GetFirstIvarInRecord - This routine returns the record for the
2505/// implementation of the fiven class OID. It also returns field
2506/// corresponding to the first ivar in the class in FIV. It also
2507/// returns the one before the first ivar.
2508///
2509const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2510 const ObjCInterfaceDecl *OID,
2511 RecordDecl::field_iterator &FIV,
2512 RecordDecl::field_iterator &PIV) {
2513 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2514 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2515 RecordDecl::field_iterator ifield = RD->field_begin();
2516 RecordDecl::field_iterator pfield = RD->field_end();
2517 while (countSuperClassIvars-- > 0) {
2518 pfield = ifield;
2519 ++ifield;
2520 }
2521 FIV = ifield;
2522 PIV = pfield;
2523 return RD;
2524}
2525
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002526void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002527 EmitModuleInfo();
2528
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002529 // Emit the dummy bodies for any protocols which were referenced but
2530 // never defined.
2531 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2532 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2533 if (i->second->hasInitializer())
2534 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002535
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002536 std::vector<llvm::Constant*> Values(5);
2537 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2538 Values[1] = GetClassName(i->first);
2539 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2540 Values[3] = Values[4] =
2541 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2542 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2543 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2544 Values));
2545 }
2546
2547 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002548 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002549 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002550 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002551 }
2552
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002553 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002554 llvm::GlobalValue *GV =
2555 new llvm::GlobalVariable(AT, false,
2556 llvm::GlobalValue::AppendingLinkage,
2557 llvm::ConstantArray::get(AT, Used),
2558 "llvm.used",
2559 &CGM.getModule());
2560
2561 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002562
2563 // Add assembler directives to add lazy undefined symbol references
2564 // for classes which are referenced but not defined. This is
2565 // important for correct linker interaction.
2566
2567 // FIXME: Uh, this isn't particularly portable.
2568 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002569
2570 if (!CGM.getModule().getModuleInlineAsm().empty())
2571 s << "\n";
2572
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002573 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2574 e = LazySymbols.end(); i != e; ++i) {
2575 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2576 }
2577 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2578 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002579 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002580 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2581 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002582
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002583 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002584}
2585
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002586CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002587 : CGObjCCommonMac(cgm),
2588 ObjCTypes(cgm)
2589{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002590 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002591 ObjCABI = 2;
2592}
2593
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002594/* *** */
2595
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002596ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2597: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002598{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002599 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2600 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002601
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002602 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002603 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002604 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002605 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2606
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002607 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002608 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002609 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002610
2611 // FIXME: It would be nice to unify this with the opaque type, so
2612 // that the IR comes out a bit cleaner.
2613 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2614 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002615
2616 // I'm not sure I like this. The implicit coordination is a bit
2617 // gross. We should solve this in a reasonable fashion because this
2618 // is a pretty common task (match some runtime data structure with
2619 // an LLVM data structure).
2620
2621 // FIXME: This is leaked.
2622 // FIXME: Merge with rewriter code?
2623
2624 // struct _objc_super {
2625 // id self;
2626 // Class cls;
2627 // }
2628 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2629 SourceLocation(),
2630 &Ctx.Idents.get("_objc_super"));
2631 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2632 Ctx.getObjCIdType(), 0, false));
2633 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2634 Ctx.getObjCClassType(), 0, false));
2635 RD->completeDefinition(Ctx);
2636
2637 SuperCTy = Ctx.getTagDeclType(RD);
2638 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2639
2640 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002641 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2642
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002643 // struct _prop_t {
2644 // char *name;
2645 // char *attributes;
2646 // }
2647 PropertyTy = llvm::StructType::get(Int8PtrTy,
2648 Int8PtrTy,
2649 NULL);
2650 CGM.getModule().addTypeName("struct._prop_t",
2651 PropertyTy);
2652
2653 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002654 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002655 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002656 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002657 // }
2658 PropertyListTy = llvm::StructType::get(IntTy,
2659 IntTy,
2660 llvm::ArrayType::get(PropertyTy, 0),
2661 NULL);
2662 CGM.getModule().addTypeName("struct._prop_list_t",
2663 PropertyListTy);
2664 // struct _prop_list_t *
2665 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2666
2667 // struct _objc_method {
2668 // SEL _cmd;
2669 // char *method_type;
2670 // char *_imp;
2671 // }
2672 MethodTy = llvm::StructType::get(SelectorPtrTy,
2673 Int8PtrTy,
2674 Int8PtrTy,
2675 NULL);
2676 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002677
2678 // struct _objc_cache *
2679 CacheTy = llvm::OpaqueType::get();
2680 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2681 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002682
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002683 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002684
2685 QualType IdType = Ctx.getObjCIdType();
2686 QualType SelType = Ctx.getObjCSelType();
2687 llvm::SmallVector<QualType,16> Params;
2688 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002689
2690 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002691 Params.push_back(IdType);
2692 Params.push_back(SelType);
2693 Params.push_back(Ctx.LongTy);
2694 Params.push_back(Ctx.BoolTy);
2695 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2696 false);
2697 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002698
2699 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2700 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002701 Params.push_back(IdType);
2702 Params.push_back(SelType);
2703 Params.push_back(Ctx.LongTy);
2704 Params.push_back(IdType);
2705 Params.push_back(Ctx.BoolTy);
2706 Params.push_back(Ctx.BoolTy);
2707 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2708 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2709
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002710 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002711
2712 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002713 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002714 Params.push_back(IdType);
2715 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2716 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2717 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002718
2719 // gc's API
2720 // id objc_read_weak (id *)
2721 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002722 Params.push_back(Ctx.getPointerType(IdType));
2723 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2724 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2725
2726 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002727 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002728 Params.push_back(IdType);
2729 Params.push_back(Ctx.getPointerType(IdType));
2730
2731 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2732 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2733 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2734 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2735 GcAssignStrongCastFn =
2736 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002737
2738 // void objc_exception_throw(id)
2739 Params.clear();
2740 Params.push_back(IdType);
2741
2742 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002743 ExceptionThrowFn =
2744 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002745
2746 // synchronized APIs
2747 // void objc_sync_enter (id)
2748 // void objc_sync_exit (id)
2749 Params.clear();
2750 Params.push_back(IdType);
2751
2752 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2753 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
2754 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002755}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002756
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002757ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2758 : ObjCCommonTypesHelper(cgm)
2759{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002760 // struct _objc_method_description {
2761 // SEL name;
2762 // char *types;
2763 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002764 MethodDescriptionTy =
2765 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002766 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002767 NULL);
2768 CGM.getModule().addTypeName("struct._objc_method_description",
2769 MethodDescriptionTy);
2770
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002771 // struct _objc_method_description_list {
2772 // int count;
2773 // struct _objc_method_description[1];
2774 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002775 MethodDescriptionListTy =
2776 llvm::StructType::get(IntTy,
2777 llvm::ArrayType::get(MethodDescriptionTy, 0),
2778 NULL);
2779 CGM.getModule().addTypeName("struct._objc_method_description_list",
2780 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002781
2782 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002783 MethodDescriptionListPtrTy =
2784 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2785
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002786 // Protocol description structures
2787
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002788 // struct _objc_protocol_extension {
2789 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2790 // struct _objc_method_description_list *optional_instance_methods;
2791 // struct _objc_method_description_list *optional_class_methods;
2792 // struct _objc_property_list *instance_properties;
2793 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002794 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002795 llvm::StructType::get(IntTy,
2796 MethodDescriptionListPtrTy,
2797 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002798 PropertyListPtrTy,
2799 NULL);
2800 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2801 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002802
2803 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002804 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2805
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002806 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002807
2808 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2809 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2810
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002811 const llvm::Type *T =
2812 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2813 LongTy,
2814 llvm::ArrayType::get(ProtocolTyHolder, 0),
2815 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002816 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2817
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002818 // struct _objc_protocol {
2819 // struct _objc_protocol_extension *isa;
2820 // char *protocol_name;
2821 // struct _objc_protocol **_objc_protocol_list;
2822 // struct _objc_method_description_list *instance_methods;
2823 // struct _objc_method_description_list *class_methods;
2824 // }
2825 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002826 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002827 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2828 MethodDescriptionListPtrTy,
2829 MethodDescriptionListPtrTy,
2830 NULL);
2831 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2832
2833 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2834 CGM.getModule().addTypeName("struct._objc_protocol_list",
2835 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002836 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002837 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2838
2839 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002840 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002841 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002842
2843 // Class description structures
2844
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002845 // struct _objc_ivar {
2846 // char *ivar_name;
2847 // char *ivar_type;
2848 // int ivar_offset;
2849 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002850 IvarTy = llvm::StructType::get(Int8PtrTy,
2851 Int8PtrTy,
2852 IntTy,
2853 NULL);
2854 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2855
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002856 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002857 IvarListTy = llvm::OpaqueType::get();
2858 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2859 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2860
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002861 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002862 MethodListTy = llvm::OpaqueType::get();
2863 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2864 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2865
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002866 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002867 ClassExtensionTy =
2868 llvm::StructType::get(IntTy,
2869 Int8PtrTy,
2870 PropertyListPtrTy,
2871 NULL);
2872 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2873 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2874
2875 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2876
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002877 // struct _objc_class {
2878 // Class isa;
2879 // Class super_class;
2880 // char *name;
2881 // long version;
2882 // long info;
2883 // long instance_size;
2884 // struct _objc_ivar_list *ivars;
2885 // struct _objc_method_list *methods;
2886 // struct _objc_cache *cache;
2887 // struct _objc_protocol_list *protocols;
2888 // char *ivar_layout;
2889 // struct _objc_class_ext *ext;
2890 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002891 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2892 llvm::PointerType::getUnqual(ClassTyHolder),
2893 Int8PtrTy,
2894 LongTy,
2895 LongTy,
2896 LongTy,
2897 IvarListPtrTy,
2898 MethodListPtrTy,
2899 CachePtrTy,
2900 ProtocolListPtrTy,
2901 Int8PtrTy,
2902 ClassExtensionPtrTy,
2903 NULL);
2904 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2905
2906 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2907 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2908 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2909
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002910 // struct _objc_category {
2911 // char *category_name;
2912 // char *class_name;
2913 // struct _objc_method_list *instance_method;
2914 // struct _objc_method_list *class_method;
2915 // uint32_t size; // sizeof(struct _objc_category)
2916 // struct _objc_property_list *instance_properties;// category's @property
2917 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002918 CategoryTy = llvm::StructType::get(Int8PtrTy,
2919 Int8PtrTy,
2920 MethodListPtrTy,
2921 MethodListPtrTy,
2922 ProtocolListPtrTy,
2923 IntTy,
2924 PropertyListPtrTy,
2925 NULL);
2926 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2927
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002928 // Global metadata structures
2929
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002930 // struct _objc_symtab {
2931 // long sel_ref_cnt;
2932 // SEL *refs;
2933 // short cls_def_cnt;
2934 // short cat_def_cnt;
2935 // char *defs[cls_def_cnt + cat_def_cnt];
2936 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002937 SymtabTy = llvm::StructType::get(LongTy,
2938 SelectorPtrTy,
2939 ShortTy,
2940 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002941 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002942 NULL);
2943 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2944 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2945
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002946 // struct _objc_module {
2947 // long version;
2948 // long size; // sizeof(struct _objc_module)
2949 // char *name;
2950 // struct _objc_symtab* symtab;
2951 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002952 ModuleTy =
2953 llvm::StructType::get(LongTy,
2954 LongTy,
2955 Int8PtrTy,
2956 SymtabPtrTy,
2957 NULL);
2958 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002959
Daniel Dunbar49f66022008-09-24 03:38:44 +00002960 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002961
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002962 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002963 std::vector<const llvm::Type*> Params;
2964 Params.push_back(ObjectPtrTy);
2965 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002966 MessageSendFn =
2967 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2968 Params,
2969 true),
2970 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002971
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002972 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002973 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002974 Params.push_back(ObjectPtrTy);
2975 Params.push_back(SelectorPtrTy);
2976 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002977 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2978 Params,
2979 true),
2980 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002981
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002982 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002983 Params.clear();
2984 Params.push_back(ObjectPtrTy);
2985 Params.push_back(SelectorPtrTy);
2986 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002987 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002988 MessageSendFpretFn =
2989 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2990 Params,
2991 true),
2992 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002993
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002994 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002995 Params.clear();
2996 Params.push_back(SuperPtrTy);
2997 Params.push_back(SelectorPtrTy);
2998 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002999 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3000 Params,
3001 true),
3002 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003003
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003004 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3005 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003006 Params.clear();
3007 Params.push_back(Int8PtrTy);
3008 Params.push_back(SuperPtrTy);
3009 Params.push_back(SelectorPtrTy);
3010 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003011 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3012 Params,
3013 true),
3014 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003015
3016 // There is no objc_msgSendSuper_fpret? How can that work?
3017 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003018
Anders Carlsson124526b2008-09-09 10:10:21 +00003019 // FIXME: This is the size of the setjmp buffer and should be
3020 // target specific. 18 is what's used on 32-bit X86.
3021 uint64_t SetJmpBufferSize = 18;
3022
3023 // Exceptions
3024 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003025 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003026
3027 ExceptionDataTy =
3028 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3029 SetJmpBufferSize),
3030 StackPtrTy, NULL);
3031 CGM.getModule().addTypeName("struct._objc_exception_data",
3032 ExceptionDataTy);
3033
3034 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003035 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3036 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003037 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3038 Params,
3039 false),
3040 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003041 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003042 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3043 Params,
3044 false),
3045 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003046 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003047 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3048 Params,
3049 false),
3050 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003051
3052 Params.clear();
3053 Params.push_back(ClassPtrTy);
3054 Params.push_back(ObjectPtrTy);
3055 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003056 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3057 Params,
3058 false),
3059 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003060
Anders Carlsson124526b2008-09-09 10:10:21 +00003061 Params.clear();
3062 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3063 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003064 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3065 Params,
3066 false),
3067 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003068
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003069}
3070
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003071ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003072: ObjCCommonTypesHelper(cgm)
3073{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003074 // struct _method_list_t {
3075 // uint32_t entsize; // sizeof(struct _objc_method)
3076 // uint32_t method_count;
3077 // struct _objc_method method_list[method_count];
3078 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003079 MethodListnfABITy = llvm::StructType::get(IntTy,
3080 IntTy,
3081 llvm::ArrayType::get(MethodTy, 0),
3082 NULL);
3083 CGM.getModule().addTypeName("struct.__method_list_t",
3084 MethodListnfABITy);
3085 // struct method_list_t *
3086 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003087
3088 // struct _protocol_t {
3089 // id isa; // NULL
3090 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003091 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003092 // const struct method_list_t * const instance_methods;
3093 // const struct method_list_t * const class_methods;
3094 // const struct method_list_t *optionalInstanceMethods;
3095 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003096 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003097 // const uint32_t size; // sizeof(struct _protocol_t)
3098 // const uint32_t flags; // = 0
3099 // }
3100
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101 // Holder for struct _protocol_list_t *
3102 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3103
3104 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3105 Int8PtrTy,
3106 llvm::PointerType::getUnqual(
3107 ProtocolListTyHolder),
3108 MethodListnfABIPtrTy,
3109 MethodListnfABIPtrTy,
3110 MethodListnfABIPtrTy,
3111 MethodListnfABIPtrTy,
3112 PropertyListPtrTy,
3113 IntTy,
3114 IntTy,
3115 NULL);
3116 CGM.getModule().addTypeName("struct._protocol_t",
3117 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003118
3119 // struct _protocol_t*
3120 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003121
Fariborz Jahanianda320092009-01-29 19:24:30 +00003122 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003123 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003124 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003125 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003126 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3127 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003128 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003129 NULL);
3130 CGM.getModule().addTypeName("struct._objc_protocol_list",
3131 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003132 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3133 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003134
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003135 // struct _objc_protocol_list*
3136 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003137
3138 // struct _ivar_t {
3139 // unsigned long int *offset; // pointer to ivar offset location
3140 // char *name;
3141 // char *type;
3142 // uint32_t alignment;
3143 // uint32_t size;
3144 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003145 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3146 Int8PtrTy,
3147 Int8PtrTy,
3148 IntTy,
3149 IntTy,
3150 NULL);
3151 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3152
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003153 // struct _ivar_list_t {
3154 // uint32 entsize; // sizeof(struct _ivar_t)
3155 // uint32 count;
3156 // struct _iver_t list[count];
3157 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003158 IvarListnfABITy = llvm::StructType::get(IntTy,
3159 IntTy,
3160 llvm::ArrayType::get(
3161 IvarnfABITy, 0),
3162 NULL);
3163 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3164
3165 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003166
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003167 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003168 // uint32_t const flags;
3169 // uint32_t const instanceStart;
3170 // uint32_t const instanceSize;
3171 // uint32_t const reserved; // only when building for 64bit targets
3172 // const uint8_t * const ivarLayout;
3173 // const char *const name;
3174 // const struct _method_list_t * const baseMethods;
3175 // const struct _objc_protocol_list *const baseProtocols;
3176 // const struct _ivar_list_t *const ivars;
3177 // const uint8_t * const weakIvarLayout;
3178 // const struct _prop_list_t * const properties;
3179 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003180
3181 // FIXME. Add 'reserved' field in 64bit abi mode!
3182 ClassRonfABITy = llvm::StructType::get(IntTy,
3183 IntTy,
3184 IntTy,
3185 Int8PtrTy,
3186 Int8PtrTy,
3187 MethodListnfABIPtrTy,
3188 ProtocolListnfABIPtrTy,
3189 IvarListnfABIPtrTy,
3190 Int8PtrTy,
3191 PropertyListPtrTy,
3192 NULL);
3193 CGM.getModule().addTypeName("struct._class_ro_t",
3194 ClassRonfABITy);
3195
3196 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3197 std::vector<const llvm::Type*> Params;
3198 Params.push_back(ObjectPtrTy);
3199 Params.push_back(SelectorPtrTy);
3200 ImpnfABITy = llvm::PointerType::getUnqual(
3201 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3202
3203 // struct _class_t {
3204 // struct _class_t *isa;
3205 // struct _class_t * const superclass;
3206 // void *cache;
3207 // IMP *vtable;
3208 // struct class_ro_t *ro;
3209 // }
3210
3211 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3212 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3213 llvm::PointerType::getUnqual(ClassTyHolder),
3214 CachePtrTy,
3215 llvm::PointerType::getUnqual(ImpnfABITy),
3216 llvm::PointerType::getUnqual(
3217 ClassRonfABITy),
3218 NULL);
3219 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3220
3221 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3222 ClassnfABITy);
3223
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003224 // LLVM for struct _class_t *
3225 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3226
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003227 // struct _category_t {
3228 // const char * const name;
3229 // struct _class_t *const cls;
3230 // const struct _method_list_t * const instance_methods;
3231 // const struct _method_list_t * const class_methods;
3232 // const struct _protocol_list_t * const protocols;
3233 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003234 // }
3235 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003236 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003237 MethodListnfABIPtrTy,
3238 MethodListnfABIPtrTy,
3239 ProtocolListnfABIPtrTy,
3240 PropertyListPtrTy,
3241 NULL);
3242 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003243
3244 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003245 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3246 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003247
3248 // MessageRefTy - LLVM for:
3249 // struct _message_ref_t {
3250 // IMP messenger;
3251 // SEL name;
3252 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003253
3254 // First the clang type for struct _message_ref_t
3255 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3256 SourceLocation(),
3257 &Ctx.Idents.get("_message_ref_t"));
3258 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3259 Ctx.VoidPtrTy, 0, false));
3260 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3261 Ctx.getObjCSelType(), 0, false));
3262 RD->completeDefinition(Ctx);
3263
3264 MessageRefCTy = Ctx.getTagDeclType(RD);
3265 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3266 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003267
3268 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3269 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3270
3271 // SuperMessageRefTy - LLVM for:
3272 // struct _super_message_ref_t {
3273 // SUPER_IMP messenger;
3274 // SEL name;
3275 // };
3276 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3277 SelectorPtrTy,
3278 NULL);
3279 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3280
3281 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3282 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3283
3284 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3285 Params.clear();
3286 Params.push_back(ObjectPtrTy);
3287 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003288 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3289 Params,
3290 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003291 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003292 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003293 "objc_msgSend_fixup");
3294
3295 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3296 MessageSendFpretFixupFn =
3297 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3298 Params,
3299 true),
3300 "objc_msgSend_fpret_fixup");
3301
3302 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3303 MessageSendStretFixupFn =
3304 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3305 Params,
3306 true),
3307 "objc_msgSend_stret_fixup");
3308
3309 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3310 MessageSendIdFixupFn =
3311 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3312 Params,
3313 true),
3314 "objc_msgSendId_fixup");
3315
3316
3317 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3318 MessageSendIdStretFixupFn =
3319 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3320 Params,
3321 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003322 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003323
3324 // id objc_msgSendSuper2_fixup (struct objc_super *,
3325 // struct _super_message_ref_t*, ...)
3326 Params.clear();
3327 Params.push_back(SuperPtrTy);
3328 Params.push_back(SuperMessageRefPtrTy);
3329 MessageSendSuper2FixupFn =
3330 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3331 Params,
3332 true),
3333 "objc_msgSendSuper2_fixup");
3334
3335
3336 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3337 // struct _super_message_ref_t*, ...)
3338 MessageSendSuper2StretFixupFn =
3339 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3340 Params,
3341 true),
3342 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003343
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003344}
3345
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003346llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3347 FinishNonFragileABIModule();
3348
3349 return NULL;
3350}
3351
3352void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3353 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003354
3355 // Build list of all implemented classe addresses in array
3356 // L_OBJC_LABEL_CLASS_$.
3357 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3358 // list of 'nonlazy' implementations (defined as those with a +load{}
3359 // method!!).
3360 unsigned NumClasses = DefinedClasses.size();
3361 if (NumClasses) {
3362 std::vector<llvm::Constant*> Symbols(NumClasses);
3363 for (unsigned i=0; i<NumClasses; i++)
3364 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3365 ObjCTypes.Int8PtrTy);
3366 llvm::Constant* Init =
3367 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3368 NumClasses),
3369 Symbols);
3370
3371 llvm::GlobalVariable *GV =
3372 new llvm::GlobalVariable(Init->getType(), false,
3373 llvm::GlobalValue::InternalLinkage,
3374 Init,
3375 "\01L_OBJC_LABEL_CLASS_$",
3376 &CGM.getModule());
3377 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3378 UsedGlobals.push_back(GV);
3379 }
3380
3381 // Build list of all implemented category addresses in array
3382 // L_OBJC_LABEL_CATEGORY_$.
3383 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3384 // list of 'nonlazy' category implementations (defined as those with a +load{}
3385 // method!!).
3386 unsigned NumCategory = DefinedCategories.size();
3387 if (NumCategory) {
3388 std::vector<llvm::Constant*> Symbols(NumCategory);
3389 for (unsigned i=0; i<NumCategory; i++)
3390 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3391 ObjCTypes.Int8PtrTy);
3392 llvm::Constant* Init =
3393 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3394 NumCategory),
3395 Symbols);
3396
3397 llvm::GlobalVariable *GV =
3398 new llvm::GlobalVariable(Init->getType(), false,
3399 llvm::GlobalValue::InternalLinkage,
3400 Init,
3401 "\01L_OBJC_LABEL_CATEGORY_$",
3402 &CGM.getModule());
3403 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3404 UsedGlobals.push_back(GV);
3405 }
3406
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003407 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3408 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3409 std::vector<llvm::Constant*> Values(2);
3410 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3411 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3412 llvm::Constant* Init = llvm::ConstantArray::get(
3413 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3414 Values);
3415 llvm::GlobalVariable *IMGV =
3416 new llvm::GlobalVariable(Init->getType(), false,
3417 llvm::GlobalValue::InternalLinkage,
3418 Init,
3419 "\01L_OBJC_IMAGE_INFO",
3420 &CGM.getModule());
3421 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3422 UsedGlobals.push_back(IMGV);
3423
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003424 std::vector<llvm::Constant*> Used;
3425 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3426 e = UsedGlobals.end(); i != e; ++i) {
3427 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3428 }
3429
3430 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3431 llvm::GlobalValue *GV =
3432 new llvm::GlobalVariable(AT, false,
3433 llvm::GlobalValue::AppendingLinkage,
3434 llvm::ConstantArray::get(AT, Used),
3435 "llvm.used",
3436 &CGM.getModule());
3437
3438 GV->setSection("llvm.metadata");
3439
3440}
3441
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003442// Metadata flags
3443enum MetaDataDlags {
3444 CLS = 0x0,
3445 CLS_META = 0x1,
3446 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003447 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003448 CLS_EXCEPTION = 0x20
3449};
3450/// BuildClassRoTInitializer - generate meta-data for:
3451/// struct _class_ro_t {
3452/// uint32_t const flags;
3453/// uint32_t const instanceStart;
3454/// uint32_t const instanceSize;
3455/// uint32_t const reserved; // only when building for 64bit targets
3456/// const uint8_t * const ivarLayout;
3457/// const char *const name;
3458/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003459/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003460/// const struct _ivar_list_t *const ivars;
3461/// const uint8_t * const weakIvarLayout;
3462/// const struct _prop_list_t * const properties;
3463/// }
3464///
3465llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3466 unsigned flags,
3467 unsigned InstanceStart,
3468 unsigned InstanceSize,
3469 const ObjCImplementationDecl *ID) {
3470 std::string ClassName = ID->getNameAsString();
3471 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3472 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3473 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3474 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3475 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003476 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003477 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3478 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003479 // const struct _method_list_t * const baseMethods;
3480 std::vector<llvm::Constant*> Methods;
3481 std::string MethodListName("\01l_OBJC_$_");
3482 if (flags & CLS_META) {
3483 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3484 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3485 e = ID->classmeth_end(); i != e; ++i) {
3486 // Class methods should always be defined.
3487 Methods.push_back(GetMethodConstant(*i));
3488 }
3489 } else {
3490 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3491 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3492 e = ID->instmeth_end(); i != e; ++i) {
3493 // Instance methods should always be defined.
3494 Methods.push_back(GetMethodConstant(*i));
3495 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003496 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3497 e = ID->propimpl_end(); i != e; ++i) {
3498 ObjCPropertyImplDecl *PID = *i;
3499
3500 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3501 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3502
3503 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3504 if (llvm::Constant *C = GetMethodConstant(MD))
3505 Methods.push_back(C);
3506 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3507 if (llvm::Constant *C = GetMethodConstant(MD))
3508 Methods.push_back(C);
3509 }
3510 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003511 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003512 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003513 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003514
3515 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3516 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3517 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3518 + OID->getNameAsString(),
3519 OID->protocol_begin(),
3520 OID->protocol_end());
3521
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003522 if (flags & CLS_META)
3523 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3524 else
3525 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003526 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003527 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003528 if (flags & CLS_META)
3529 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3530 else
3531 Values[ 9] =
3532 EmitPropertyList(
3533 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3534 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003535 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3536 Values);
3537 llvm::GlobalVariable *CLASS_RO_GV =
3538 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3539 llvm::GlobalValue::InternalLinkage,
3540 Init,
3541 (flags & CLS_META) ?
3542 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3543 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3544 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003545 CLASS_RO_GV->setAlignment(
3546 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003547 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003548 UsedGlobals.push_back(CLASS_RO_GV);
3549 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003550
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003551}
3552
3553/// BuildClassMetaData - This routine defines that to-level meta-data
3554/// for the given ClassName for:
3555/// struct _class_t {
3556/// struct _class_t *isa;
3557/// struct _class_t * const superclass;
3558/// void *cache;
3559/// IMP *vtable;
3560/// struct class_ro_t *ro;
3561/// }
3562///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003563llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3564 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003565 llvm::Constant *IsAGV,
3566 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003567 llvm::Constant *ClassRoGV,
3568 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003569 std::vector<llvm::Constant*> Values(5);
3570 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003571 Values[1] = SuperClassGV
3572 ? SuperClassGV
3573 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003574 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3575 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3576 Values[4] = ClassRoGV; // &CLASS_RO_GV
3577 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3578 Values);
3579 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3580 if (GV)
3581 GV->setInitializer(Init);
3582 else
3583 GV =
3584 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3585 llvm::GlobalValue::ExternalLinkage,
3586 Init,
3587 ClassName,
3588 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003589 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003590 GV->setAlignment(
3591 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003592 if (HiddenVisibility)
3593 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003594 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003595 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003596}
3597
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003598void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3599 std::string ClassName = ID->getNameAsString();
3600 if (!ObjCEmptyCacheVar) {
3601 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003602 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003603 false,
3604 llvm::GlobalValue::ExternalLinkage,
3605 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003606 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003607 &CGM.getModule());
3608 UsedGlobals.push_back(ObjCEmptyCacheVar);
3609
3610 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003611 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003612 false,
3613 llvm::GlobalValue::ExternalLinkage,
3614 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003615 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003616 &CGM.getModule());
3617 UsedGlobals.push_back(ObjCEmptyVtableVar);
3618 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003619 assert(ID->getClassInterface() &&
3620 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003621 uint32_t InstanceStart =
3622 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3623 uint32_t InstanceSize = InstanceStart;
3624 uint32_t flags = CLS_META;
3625 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3626 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003627
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003628 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003629
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003630 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3631 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003632 flags |= OBJC2_CLS_HIDDEN;
3633 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003634 // class is root
3635 flags |= CLS_ROOT;
3636 std::string SuperClassName = ObjCClassName + ClassName;
3637 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003638 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003639 SuperClassGV =
3640 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3641 llvm::GlobalValue::ExternalLinkage,
3642 0,
3643 SuperClassName,
3644 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003645 UsedGlobals.push_back(SuperClassGV);
3646 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003647 std::string IsAClassName = ObjCMetaClassName + ClassName;
3648 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003649 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003650 IsAGV =
3651 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3652 llvm::GlobalValue::ExternalLinkage,
3653 0,
3654 IsAClassName,
3655 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003656 UsedGlobals.push_back(IsAGV);
3657 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003658 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003659 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003660 std::string RootClassName =
3661 ID->getClassInterface()->getSuperClass()->getNameAsString();
3662 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3663 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003664 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003665 SuperClassGV =
3666 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3667 llvm::GlobalValue::ExternalLinkage,
3668 0,
3669 SuperClassName,
3670 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003671 UsedGlobals.push_back(SuperClassGV);
3672 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003673 IsAGV = SuperClassGV;
3674 }
3675 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3676 InstanceStart,
3677 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003678 std::string TClassName = ObjCMetaClassName + ClassName;
3679 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003680 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3681 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003682
3683 // Metadata for the class
3684 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003685 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003686 flags |= OBJC2_CLS_HIDDEN;
3687 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003688 flags |= CLS_ROOT;
3689 SuperClassGV = 0;
3690 }
3691 else {
3692 // Has a root. Current class is not a root.
3693 std::string RootClassName =
3694 ID->getClassInterface()->getSuperClass()->getNameAsString();
3695 std::string SuperClassName = ObjCClassName + RootClassName;
3696 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003697 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003698 SuperClassGV =
3699 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3700 llvm::GlobalValue::ExternalLinkage,
3701 0,
3702 SuperClassName,
3703 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003704 UsedGlobals.push_back(SuperClassGV);
3705 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003706 }
3707
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003708 InstanceStart = InstanceSize = 0;
3709 if (ObjCInterfaceDecl *OID =
3710 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3711 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003712 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003713 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003714 const llvm::StructLayout *Layout =
3715 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003716
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003717 RecordDecl::field_iterator firstField, lastField;
3718 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003719
3720 for (RecordDecl::field_iterator e = RD->field_end(),
3721 ifield = firstField; ifield != e; ++ifield)
3722 lastField = ifield;
3723
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003724 if (lastField != RD->field_end()) {
3725 FieldDecl *Field = *lastField;
3726 const llvm::Type *FieldTy =
3727 CGM.getTypes().ConvertTypeForMem(Field->getType());
3728 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3729 InstanceSize = Layout->getElementOffset(
3730 CGM.getTypes().getLLVMFieldNo(Field)) +
3731 Size;
3732 if (firstField == RD->field_end())
3733 InstanceStart = InstanceSize;
3734 else
3735 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3736 getLLVMFieldNo(*firstField));
3737 }
3738 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003739 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003740 InstanceStart,
3741 InstanceSize,
3742 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003743
3744 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003745 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003746 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3747 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003748 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003749}
3750
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003751/// GenerateProtocolRef - This routine is called to generate code for
3752/// a protocol reference expression; as in:
3753/// @code
3754/// @protocol(Proto1);
3755/// @endcode
3756/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3757/// which will hold address of the protocol meta-data.
3758///
3759llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3760 const ObjCProtocolDecl *PD) {
3761
3762 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3763 ObjCTypes.ExternalProtocolPtrTy);
3764
3765 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3766 ProtocolName += PD->getNameAsCString();
3767
3768 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3769 if (PTGV)
3770 return Builder.CreateLoad(PTGV, false, "tmp");
3771 PTGV = new llvm::GlobalVariable(
3772 Init->getType(), false,
3773 llvm::GlobalValue::WeakLinkage,
3774 Init,
3775 ProtocolName,
3776 &CGM.getModule());
3777 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3778 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3779 UsedGlobals.push_back(PTGV);
3780 return Builder.CreateLoad(PTGV, false, "tmp");
3781}
3782
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003783/// GenerateCategory - Build metadata for a category implementation.
3784/// struct _category_t {
3785/// const char * const name;
3786/// struct _class_t *const cls;
3787/// const struct _method_list_t * const instance_methods;
3788/// const struct _method_list_t * const class_methods;
3789/// const struct _protocol_list_t * const protocols;
3790/// const struct _prop_list_t * const properties;
3791/// }
3792///
3793void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3794{
3795 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003796 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3797 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003798 "_$_" + OCD->getNameAsString());
3799 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3800
3801 std::vector<llvm::Constant*> Values(6);
3802 Values[0] = GetClassName(OCD->getIdentifier());
3803 // meta-class entry symbol
3804 llvm::GlobalVariable *ClassGV =
3805 CGM.getModule().getGlobalVariable(ExtClassName);
3806 if (!ClassGV)
3807 ClassGV =
3808 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3809 llvm::GlobalValue::ExternalLinkage,
3810 0,
3811 ExtClassName,
3812 &CGM.getModule());
3813 UsedGlobals.push_back(ClassGV);
3814 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003815 std::vector<llvm::Constant*> Methods;
3816 std::string MethodListName(Prefix);
3817 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3818 "_$_" + OCD->getNameAsString();
3819
3820 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3821 e = OCD->instmeth_end(); i != e; ++i) {
3822 // Instance methods should always be defined.
3823 Methods.push_back(GetMethodConstant(*i));
3824 }
3825
3826 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003827 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003828 Methods);
3829
3830 MethodListName = Prefix;
3831 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3832 OCD->getNameAsString();
3833 Methods.clear();
3834 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3835 e = OCD->classmeth_end(); i != e; ++i) {
3836 // Class methods should always be defined.
3837 Methods.push_back(GetMethodConstant(*i));
3838 }
3839
3840 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003841 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003842 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003843 const ObjCCategoryDecl *Category =
3844 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003845 if (Category) {
3846 std::string ExtName(Interface->getNameAsString() + "_$_" +
3847 OCD->getNameAsString());
3848 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3849 + Interface->getNameAsString() + "_$_"
3850 + Category->getNameAsString(),
3851 Category->protocol_begin(),
3852 Category->protocol_end());
3853 Values[5] =
3854 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3855 OCD, Category, ObjCTypes);
3856 }
3857 else {
3858 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3859 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3860 }
3861
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003862 llvm::Constant *Init =
3863 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3864 Values);
3865 llvm::GlobalVariable *GCATV
3866 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3867 false,
3868 llvm::GlobalValue::InternalLinkage,
3869 Init,
3870 ExtCatName,
3871 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003872 GCATV->setAlignment(
3873 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003874 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003875 UsedGlobals.push_back(GCATV);
3876 DefinedCategories.push_back(GCATV);
3877}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003878
3879/// GetMethodConstant - Return a struct objc_method constant for the
3880/// given method if it has been defined. The result is null if the
3881/// method has not been defined. The return value has type MethodPtrTy.
3882llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3883 const ObjCMethodDecl *MD) {
3884 // FIXME: Use DenseMap::lookup
3885 llvm::Function *Fn = MethodDefinitions[MD];
3886 if (!Fn)
3887 return 0;
3888
3889 std::vector<llvm::Constant*> Method(3);
3890 Method[0] =
3891 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3892 ObjCTypes.SelectorPtrTy);
3893 Method[1] = GetMethodVarType(MD);
3894 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3895 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3896}
3897
3898/// EmitMethodList - Build meta-data for method declarations
3899/// struct _method_list_t {
3900/// uint32_t entsize; // sizeof(struct _objc_method)
3901/// uint32_t method_count;
3902/// struct _objc_method method_list[method_count];
3903/// }
3904///
3905llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3906 const std::string &Name,
3907 const char *Section,
3908 const ConstantVector &Methods) {
3909 // Return null for empty list.
3910 if (Methods.empty())
3911 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3912
3913 std::vector<llvm::Constant*> Values(3);
3914 // sizeof(struct _objc_method)
3915 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3916 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3917 // method_count
3918 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3919 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3920 Methods.size());
3921 Values[2] = llvm::ConstantArray::get(AT, Methods);
3922 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3923
3924 llvm::GlobalVariable *GV =
3925 new llvm::GlobalVariable(Init->getType(), false,
3926 llvm::GlobalValue::InternalLinkage,
3927 Init,
3928 Name,
3929 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003930 GV->setAlignment(
3931 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003932 GV->setSection(Section);
3933 UsedGlobals.push_back(GV);
3934 return llvm::ConstantExpr::getBitCast(GV,
3935 ObjCTypes.MethodListnfABIPtrTy);
3936}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003937
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003938/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3939/// the given ivar.
3940///
3941llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3942 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003943 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003944 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003945 Name += "\01_OBJC_IVAR_$_" +
3946 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003947 + Ivar->getNameAsString();
3948 llvm::GlobalVariable *IvarOffsetGV =
3949 CGM.getModule().getGlobalVariable(Name);
3950 if (!IvarOffsetGV)
3951 IvarOffsetGV =
3952 new llvm::GlobalVariable(ObjCTypes.LongTy,
3953 false,
3954 llvm::GlobalValue::ExternalLinkage,
3955 0,
3956 Name,
3957 &CGM.getModule());
3958 return IvarOffsetGV;
3959}
3960
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003961llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003962 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003963 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003964 unsigned long int Offset) {
3965
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003966 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003967 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003968 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003969 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003970
3971 llvm::GlobalVariable *IvarOffsetGV =
3972 CGM.getModule().getGlobalVariable(ExternalName);
3973 if (IvarOffsetGV) {
3974 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003975 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003976 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003977 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003978 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003979 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003980 return IvarOffsetGV;
3981 }
3982
3983 IvarOffsetGV =
3984 new llvm::GlobalVariable(Init->getType(),
3985 false,
3986 llvm::GlobalValue::ExternalLinkage,
3987 Init,
3988 ExternalName,
3989 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003990 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003991 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003992 // @private and @package have hidden visibility.
3993 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3994 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3995 if (!globalVisibility)
3996 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003997 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003998 if (IsClassHidden(ID))
3999 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004000
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004001 IvarOffsetGV->setSection("__DATA, __objc_const");
4002 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004003 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004004}
4005
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004006/// EmitIvarList - Emit the ivar list for the given
4007/// implementation. If ForClass is true the list of class ivars
4008/// (i.e. metaclass ivars) is emitted, otherwise the list of
4009/// interface ivars will be emitted. The return value has type
4010/// IvarListnfABIPtrTy.
4011/// struct _ivar_t {
4012/// unsigned long int *offset; // pointer to ivar offset location
4013/// char *name;
4014/// char *type;
4015/// uint32_t alignment;
4016/// uint32_t size;
4017/// }
4018/// struct _ivar_list_t {
4019/// uint32 entsize; // sizeof(struct _ivar_t)
4020/// uint32 count;
4021/// struct _iver_t list[count];
4022/// }
4023///
4024llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4025 const ObjCImplementationDecl *ID) {
4026
4027 std::vector<llvm::Constant*> Ivars, Ivar(5);
4028
4029 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4030 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4031
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004032 // FIXME. Consolidate this with similar code in GenerateClass.
4033 const llvm::Type *InterfaceTy =
4034 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4035 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004036 const llvm::StructLayout *Layout =
4037 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004038
4039 RecordDecl::field_iterator i,p;
4040 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004041 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4042
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004043 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004044 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004045 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4046 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004047 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004048 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004049 if (Field->getIdentifier())
4050 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4051 else
4052 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4053 std::string TypeStr;
4054 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4055 Ivar[2] = GetMethodVarType(TypeStr);
4056 const llvm::Type *FieldTy =
4057 CGM.getTypes().ConvertTypeForMem(Field->getType());
4058 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4059 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4060 Field->getType().getTypePtr()) >> 3;
4061 Align = llvm::Log2_32(Align);
4062 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004063 // NOTE. Size of a bitfield does not match gcc's, because of the way
4064 // bitfields are treated special in each. But I am told that 'size'
4065 // for bitfield ivars is ignored by the runtime so it does not matter.
4066 // (even if it matters, some day, there is enough info. to get the bitfield
4067 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004068 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4069 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4070 }
4071 // Return null for empty list.
4072 if (Ivars.empty())
4073 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4074 std::vector<llvm::Constant*> Values(3);
4075 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4076 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4077 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4078 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4079 Ivars.size());
4080 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4081 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4082 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4083 llvm::GlobalVariable *GV =
4084 new llvm::GlobalVariable(Init->getType(), false,
4085 llvm::GlobalValue::InternalLinkage,
4086 Init,
4087 Prefix + OID->getNameAsString(),
4088 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004089 GV->setAlignment(
4090 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004091 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004092
4093 UsedGlobals.push_back(GV);
4094 return llvm::ConstantExpr::getBitCast(GV,
4095 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004096}
4097
4098llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4099 const ObjCProtocolDecl *PD) {
4100 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4101
4102 if (!Entry) {
4103 // We use the initializer as a marker of whether this is a forward
4104 // reference or not. At module finalization we add the empty
4105 // contents for protocols which were referenced but never defined.
4106 Entry =
4107 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4108 llvm::GlobalValue::ExternalLinkage,
4109 0,
4110 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4111 &CGM.getModule());
4112 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4113 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004114 }
4115
4116 return Entry;
4117}
4118
4119/// GetOrEmitProtocol - Generate the protocol meta-data:
4120/// @code
4121/// struct _protocol_t {
4122/// id isa; // NULL
4123/// const char * const protocol_name;
4124/// const struct _protocol_list_t * protocol_list; // super protocols
4125/// const struct method_list_t * const instance_methods;
4126/// const struct method_list_t * const class_methods;
4127/// const struct method_list_t *optionalInstanceMethods;
4128/// const struct method_list_t *optionalClassMethods;
4129/// const struct _prop_list_t * properties;
4130/// const uint32_t size; // sizeof(struct _protocol_t)
4131/// const uint32_t flags; // = 0
4132/// }
4133/// @endcode
4134///
4135
4136llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4137 const ObjCProtocolDecl *PD) {
4138 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4139
4140 // Early exit if a defining object has already been generated.
4141 if (Entry && Entry->hasInitializer())
4142 return Entry;
4143
4144 const char *ProtocolName = PD->getNameAsCString();
4145
4146 // Construct method lists.
4147 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4148 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4149 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4150 e = PD->instmeth_end(); i != e; ++i) {
4151 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004152 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004153 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4154 OptInstanceMethods.push_back(C);
4155 } else {
4156 InstanceMethods.push_back(C);
4157 }
4158 }
4159
4160 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4161 e = PD->classmeth_end(); i != e; ++i) {
4162 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004163 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004164 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4165 OptClassMethods.push_back(C);
4166 } else {
4167 ClassMethods.push_back(C);
4168 }
4169 }
4170
4171 std::vector<llvm::Constant*> Values(10);
4172 // isa is NULL
4173 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4174 Values[1] = GetClassName(PD->getIdentifier());
4175 Values[2] = EmitProtocolList(
4176 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4177 PD->protocol_begin(),
4178 PD->protocol_end());
4179
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004180 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004181 + PD->getNameAsString(),
4182 "__DATA, __objc_const",
4183 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004184 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004185 + PD->getNameAsString(),
4186 "__DATA, __objc_const",
4187 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004188 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004189 + PD->getNameAsString(),
4190 "__DATA, __objc_const",
4191 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004192 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004193 + PD->getNameAsString(),
4194 "__DATA, __objc_const",
4195 OptClassMethods);
4196 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4197 0, PD, ObjCTypes);
4198 uint32_t Size =
4199 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4200 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4201 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4202 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4203 Values);
4204
4205 if (Entry) {
4206 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004207 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004208 Entry->setInitializer(Init);
4209 } else {
4210 Entry =
4211 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004212 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004213 Init,
4214 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4215 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004216 Entry->setAlignment(
4217 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004218 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004219 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004220 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4221
4222 // Use this protocol meta-data to build protocol list table in section
4223 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004224 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004225 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004226 llvm::GlobalValue::WeakLinkage,
4227 Entry,
4228 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4229 +ProtocolName,
4230 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004231 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004232 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004233 PTGV->setSection("__DATA, __objc_protolist");
4234 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4235 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004236 return Entry;
4237}
4238
4239/// EmitProtocolList - Generate protocol list meta-data:
4240/// @code
4241/// struct _protocol_list_t {
4242/// long protocol_count; // Note, this is 32/64 bit
4243/// struct _protocol_t[protocol_count];
4244/// }
4245/// @endcode
4246///
4247llvm::Constant *
4248CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4249 ObjCProtocolDecl::protocol_iterator begin,
4250 ObjCProtocolDecl::protocol_iterator end) {
4251 std::vector<llvm::Constant*> ProtocolRefs;
4252
Fariborz Jahanianda320092009-01-29 19:24:30 +00004253 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004254 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004255 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4256
Daniel Dunbar948e2582009-02-15 07:36:20 +00004257 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004258 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4259 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004260 return llvm::ConstantExpr::getBitCast(GV,
4261 ObjCTypes.ProtocolListnfABIPtrTy);
4262
4263 for (; begin != end; ++begin)
4264 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4265
Fariborz Jahanianda320092009-01-29 19:24:30 +00004266 // This list is null terminated.
4267 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004268 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004269
4270 std::vector<llvm::Constant*> Values(2);
4271 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4272 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004273 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004274 ProtocolRefs.size()),
4275 ProtocolRefs);
4276
4277 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4278 GV = new llvm::GlobalVariable(Init->getType(), false,
4279 llvm::GlobalValue::InternalLinkage,
4280 Init,
4281 Name,
4282 &CGM.getModule());
4283 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004284 GV->setAlignment(
4285 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004286 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004287 return llvm::ConstantExpr::getBitCast(GV,
4288 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004289}
4290
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004291/// GetMethodDescriptionConstant - This routine build following meta-data:
4292/// struct _objc_method {
4293/// SEL _cmd;
4294/// char *method_type;
4295/// char *_imp;
4296/// }
4297
4298llvm::Constant *
4299CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4300 std::vector<llvm::Constant*> Desc(3);
4301 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4302 ObjCTypes.SelectorPtrTy);
4303 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004304 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004305 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4306 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4307}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004308
4309/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4310/// This code gen. amounts to generating code for:
4311/// @code
4312/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4313/// @encode
4314///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004315LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004316 CodeGen::CodeGenFunction &CGF,
4317 QualType ObjectTy,
4318 llvm::Value *BaseValue,
4319 const ObjCIvarDecl *Ivar,
4320 const FieldDecl *Field,
4321 unsigned CVRQualifiers) {
4322 assert(ObjectTy->isObjCInterfaceType() &&
4323 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004324 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004325 std::string ExternalName;
4326 llvm::GlobalVariable *IvarOffsetGV =
4327 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004328
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004329 // (char *) BaseValue
4330 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4331 ObjCTypes.Int8PtrTy);
4332 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4333 // (char*)BaseValue + Offset_symbol
4334 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4335 // (type *)((char*)BaseValue + Offset_symbol)
4336 const llvm::Type *IvarTy =
4337 CGM.getTypes().ConvertType(Ivar->getType());
4338 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4339 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004340
4341 if (Ivar->isBitField())
4342 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4343 CVRQualifiers);
4344
4345 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004346 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4347 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004348 LValue::SetObjCIvar(LV, true);
4349 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004350}
4351
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004352llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4353 CodeGen::CodeGenFunction &CGF,
4354 ObjCInterfaceDecl *Interface,
4355 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004356 std::string ExternalName;
4357 llvm::GlobalVariable *IvarOffsetGV =
4358 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4359
4360 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004361}
4362
Fariborz Jahanian46551122009-02-04 00:22:57 +00004363CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4364 CodeGen::CodeGenFunction &CGF,
4365 QualType ResultType,
4366 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004367 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004368 QualType Arg0Ty,
4369 bool IsSuper,
4370 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004371 // FIXME. Even though IsSuper is passes. This function doese not
4372 // handle calls to 'super' receivers.
4373 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004374 llvm::Value *Arg0 = Receiver;
4375 if (!IsSuper)
4376 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004377
4378 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004379 // FIXME. This is too much work to get the ABI-specific result type
4380 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004381 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4382 llvm::SmallVector<QualType, 16>());
4383 llvm::Constant *Fn;
4384 std::string Name("\01l_");
4385 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004386#if 0
4387 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004388 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4389 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4390 // FIXME. Is there a better way of getting these names.
4391 // They are available in RuntimeFunctions vector pair.
4392 Name += "objc_msgSendId_stret_fixup";
4393 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004394 else
4395#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004396 if (IsSuper) {
4397 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4398 Name += "objc_msgSendSuper2_stret_fixup";
4399 }
4400 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004401 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004402 Fn = ObjCTypes.MessageSendStretFixupFn;
4403 Name += "objc_msgSend_stret_fixup";
4404 }
4405 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004406 else if (ResultType->isFloatingType() &&
4407 // Selection of frret API only happens in 32bit nonfragile ABI.
4408 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004409 Fn = ObjCTypes.MessageSendFpretFixupFn;
4410 Name += "objc_msgSend_fpret_fixup";
4411 }
4412 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004413#if 0
4414// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004415 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4416 Fn = ObjCTypes.MessageSendIdFixupFn;
4417 Name += "objc_msgSendId_fixup";
4418 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004419 else
4420#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004421 if (IsSuper) {
4422 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4423 Name += "objc_msgSendSuper2_fixup";
4424 }
4425 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004426 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004427 Fn = ObjCTypes.MessageSendFixupFn;
4428 Name += "objc_msgSend_fixup";
4429 }
4430 }
4431 Name += '_';
4432 std::string SelName(Sel.getAsString());
4433 // Replace all ':' in selector name with '_' ouch!
4434 for(unsigned i = 0; i < SelName.size(); i++)
4435 if (SelName[i] == ':')
4436 SelName[i] = '_';
4437 Name += SelName;
4438 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4439 if (!GV) {
4440 // Build messafe ref table entry.
4441 std::vector<llvm::Constant*> Values(2);
4442 Values[0] = Fn;
4443 Values[1] = GetMethodVarName(Sel);
4444 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4445 GV = new llvm::GlobalVariable(Init->getType(), false,
4446 llvm::GlobalValue::WeakLinkage,
4447 Init,
4448 Name,
4449 &CGM.getModule());
4450 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4451 GV->setAlignment(
4452 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4453 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4454 UsedGlobals.push_back(GV);
4455 }
4456 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004457
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004458 CallArgList ActualArgs;
4459 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4460 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4461 ObjCTypes.MessageRefCPtrTy));
4462 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004463 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4464 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4465 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004466 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004467 Callee = CGF.Builder.CreateBitCast(Callee,
4468 llvm::PointerType::getUnqual(FTy));
4469 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004470}
4471
4472/// Generate code for a message send expression in the nonfragile abi.
4473CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4474 CodeGen::CodeGenFunction &CGF,
4475 QualType ResultType,
4476 Selector Sel,
4477 llvm::Value *Receiver,
4478 bool IsClassMessage,
4479 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004480 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004481 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004482 false, CallArgs);
4483}
4484
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004485llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004486 const ObjCInterfaceDecl *ID,
4487 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004488
4489 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4490
4491 if (!Entry) {
4492 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4493 llvm::GlobalVariable *ClassGV =
4494 CGM.getModule().getGlobalVariable(ClassName);
4495 if (!ClassGV) {
4496 ClassGV =
4497 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4498 llvm::GlobalValue::ExternalLinkage,
4499 0,
4500 ClassName,
4501 &CGM.getModule());
4502 UsedGlobals.push_back(ClassGV);
4503 }
4504 Entry =
4505 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4506 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004507 ClassGV,
4508 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4509 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004510 &CGM.getModule());
4511 Entry->setAlignment(
4512 CGM.getTargetData().getPrefTypeAlignment(
4513 ObjCTypes.ClassnfABIPtrTy));
4514
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004515 if (IsSuper)
4516 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4517 else
4518 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004519 UsedGlobals.push_back(Entry);
4520 }
4521
4522 return Builder.CreateLoad(Entry, false, "tmp");
4523}
4524
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004525/// EmitMetaClassRef - Return a Value * of the address of _class_t
4526/// meta-data
4527///
4528llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4529 const ObjCInterfaceDecl *ID) {
4530 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4531 if (Entry)
4532 return Builder.CreateLoad(Entry, false, "tmp");
4533
4534 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4535 llvm::GlobalVariable *MetaClassGV =
4536 CGM.getModule().getGlobalVariable(MetaClassName);
4537 if (!MetaClassGV) {
4538 MetaClassGV =
4539 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4540 llvm::GlobalValue::ExternalLinkage,
4541 0,
4542 MetaClassName,
4543 &CGM.getModule());
4544 UsedGlobals.push_back(MetaClassGV);
4545 }
4546
4547 Entry =
4548 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4549 llvm::GlobalValue::InternalLinkage,
4550 MetaClassGV,
4551 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4552 &CGM.getModule());
4553 Entry->setAlignment(
4554 CGM.getTargetData().getPrefTypeAlignment(
4555 ObjCTypes.ClassnfABIPtrTy));
4556
4557 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4558 UsedGlobals.push_back(Entry);
4559
4560 return Builder.CreateLoad(Entry, false, "tmp");
4561}
4562
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004563/// GetClass - Return a reference to the class for the given interface
4564/// decl.
4565llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4566 const ObjCInterfaceDecl *ID) {
4567 return EmitClassRef(Builder, ID);
4568}
4569
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004570/// Generates a message send where the super is the receiver. This is
4571/// a message send to self with special delivery semantics indicating
4572/// which class's method should be called.
4573CodeGen::RValue
4574CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4575 QualType ResultType,
4576 Selector Sel,
4577 const ObjCInterfaceDecl *Class,
4578 llvm::Value *Receiver,
4579 bool IsClassMessage,
4580 const CodeGen::CallArgList &CallArgs) {
4581 // ...
4582 // Create and init a super structure; this is a (receiver, class)
4583 // pair we will pass to objc_msgSendSuper.
4584 llvm::Value *ObjCSuper =
4585 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4586
4587 llvm::Value *ReceiverAsObject =
4588 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4589 CGF.Builder.CreateStore(ReceiverAsObject,
4590 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4591
4592 // If this is a class message the metaclass is passed as the target.
4593 llvm::Value *Target =
4594 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4595 : EmitClassRef(CGF.Builder, Class, true);
4596
4597 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4598 // and ObjCTypes types.
4599 const llvm::Type *ClassTy =
4600 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4601 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4602 CGF.Builder.CreateStore(Target,
4603 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4604
4605 return EmitMessageSend(CGF, ResultType, Sel,
4606 ObjCSuper, ObjCTypes.SuperPtrCTy,
4607 true, CallArgs);
4608}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004609
4610llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4611 Selector Sel) {
4612 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4613
4614 if (!Entry) {
4615 llvm::Constant *Casted =
4616 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4617 ObjCTypes.SelectorPtrTy);
4618 Entry =
4619 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4620 llvm::GlobalValue::InternalLinkage,
4621 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4622 &CGM.getModule());
4623 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4624 UsedGlobals.push_back(Entry);
4625 }
4626
4627 return Builder.CreateLoad(Entry, false, "tmp");
4628}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004629/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4630/// objc_assign_ivar (id src, id *dst)
4631///
4632void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4633 llvm::Value *src, llvm::Value *dst)
4634{
4635 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4636 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4637 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4638 src, dst, "assignivar");
4639 return;
4640}
4641
4642/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4643/// objc_assign_strongCast (id src, id *dst)
4644///
4645void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4646 CodeGen::CodeGenFunction &CGF,
4647 llvm::Value *src, llvm::Value *dst)
4648{
4649 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4650 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4651 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4652 src, dst, "weakassign");
4653 return;
4654}
4655
4656/// EmitObjCWeakRead - Code gen for loading value of a __weak
4657/// object: objc_read_weak (id *src)
4658///
4659llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4660 CodeGen::CodeGenFunction &CGF,
4661 llvm::Value *AddrWeakObj)
4662{
4663 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4664 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4665 AddrWeakObj, "weakread");
4666 return read_weak;
4667}
4668
4669/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4670/// objc_assign_weak (id src, id *dst)
4671///
4672void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4673 llvm::Value *src, llvm::Value *dst)
4674{
4675 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4676 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4677 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4678 src, dst, "weakassign");
4679 return;
4680}
4681
4682/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4683/// objc_assign_global (id src, id *dst)
4684///
4685void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4686 llvm::Value *src, llvm::Value *dst)
4687{
4688 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4689 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4690 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4691 src, dst, "globalassign");
4692 return;
4693}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004694
Anders Carlssonf57c5b22009-02-16 22:59:18 +00004695/// EmitThrowStmt - Generate code for a throw statement.
4696void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
4697 const ObjCAtThrowStmt &S) {
4698 llvm::Value *ExceptionAsObject;
4699
4700 if (const Expr *ThrowExpr = S.getThrowExpr()) {
4701 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
4702 ExceptionAsObject =
4703 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
4704
4705 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
4706 CGF.Builder.CreateUnreachable();
4707 } else {
4708 CGF.ErrorUnsupported(&S, "rethrow statement");
4709 }
4710
4711 // Clear the insertion point to indicate we are in unreachable code.
4712 CGF.Builder.ClearInsertionPoint();
4713}
4714
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004715/* *** */
4716
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004717CodeGen::CGObjCRuntime *
4718CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004719 return new CGObjCMac(CGM);
4720}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004721
4722CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004723CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004724 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004725}