blob: 99c6c04abd3782937fbd6db009574549bc8a799e [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000354public:
355 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
356 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000357
358 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000359
360 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
361 const ObjCContainerDecl *CD=0);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000362};
363
364class CGObjCMac : public CGObjCCommonMac {
365private:
366 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000367 /// EmitImageInfo - Emit the image info marker used to encode some module
368 /// level information.
369 void EmitImageInfo();
370
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000371 /// EmitModuleInfo - Another marker encoding module level
372 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000373 void EmitModuleInfo();
374
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000375 /// EmitModuleSymols - Emit module symbols, the list of defined
376 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000377 llvm::Constant *EmitModuleSymbols();
378
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000379 /// FinishModule - Write out global data structures at the end of
380 /// processing a translation unit.
381 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000382
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000383 /// EmitClassExtension - Generate the class extension structure used
384 /// to store the weak ivar layout and properties. The return value
385 /// has type ClassExtensionPtrTy.
386 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
387
388 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
389 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000390 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000391 const ObjCInterfaceDecl *ID);
392
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000393 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000394 QualType ResultType,
395 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000396 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000397 QualType Arg0Ty,
398 bool IsSuper,
399 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000400
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000401 /// EmitIvarList - Emit the ivar list for the given
402 /// implementation. If ForClass is true the list of class ivars
403 /// (i.e. metaclass ivars) is emitted, otherwise the list of
404 /// interface ivars will be emitted. The return value has type
405 /// IvarListPtrTy.
406 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000407 bool ForClass);
408
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000409 /// EmitMetaClass - Emit a forward reference to the class structure
410 /// for the metaclass of the given interface. The return value has
411 /// type ClassPtrTy.
412 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
413
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000414 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000415 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
417 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000418 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000419 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000420
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000421 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000422
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000423 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000424
425 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000426 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000427 llvm::Constant *EmitMethodList(const std::string &Name,
428 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000429 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000430
431 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000432 /// method declarations.
433 /// - TypeName: The name for the type containing the methods.
434 /// - IsProtocol: True iff these methods are for a protocol.
435 /// - ClassMethds: True iff these are class methods.
436 /// - Required: When true, only "required" methods are
437 /// listed. Similarly, when false only "optional" methods are
438 /// listed. For classes this should always be true.
439 /// - begin, end: The method list to output.
440 ///
441 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000442 llvm::Constant *EmitMethodDescList(const std::string &Name,
443 const char *Section,
444 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000445
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000446 /// EmitPropertyList - Emit the given property list. The return
447 /// value has type PropertyListPtrTy.
448 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000449 const Decl *Container,
450 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000451
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000452 /// GetOrEmitProtocol - Get the protocol object for the given
453 /// declaration, emitting it if necessary. The return value has type
454 /// ProtocolPtrTy.
455 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
456
457 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
458 /// object for the given declaration, emitting it if needed. These
459 /// forward references will be filled in with empty bodies if no
460 /// definition is seen. The return value has type ProtocolPtrTy.
461 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
462
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000463 /// EmitProtocolExtension - Generate the protocol extension
464 /// structure used to store optional instance and class methods, and
465 /// protocol properties. The return value has type
466 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000467 llvm::Constant *
468 EmitProtocolExtension(const ObjCProtocolDecl *PD,
469 const ConstantVector &OptInstanceMethods,
470 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000471
472 /// EmitProtocolList - Generate the list of referenced
473 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000474 llvm::Constant *EmitProtocolList(const std::string &Name,
475 ObjCProtocolDecl::protocol_iterator begin,
476 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000477
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000478 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
479 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000480 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000481
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000482 /// GetProtocolRef - Return a reference to the internal protocol
483 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000484 /// defined. The return value has type ProtocolPtrTy.
485 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000486
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000487public:
488 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000489
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000490 virtual llvm::Function *ModuleInitFunction();
491
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000492 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000493 QualType ResultType,
494 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000495 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000496 bool IsClassMessage,
497 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000498
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000499 virtual CodeGen::RValue
500 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000501 QualType ResultType,
502 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000503 const ObjCInterfaceDecl *Class,
504 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000505 bool IsClassMessage,
506 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000507
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000508 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000509 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000510
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000511 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000513 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000514
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000515 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000516
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000517 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000518 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000519
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000520 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000521
Daniel Dunbar49f66022008-09-24 03:38:44 +0000522 virtual llvm::Function *GetPropertyGetFunction();
523 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000524 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000525
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000526 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
527 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000528 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
529 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000530 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000531 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000532 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
533 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000534 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
535 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000536 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
537 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000538 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
539 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000540};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000541
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000542class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000543private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000544 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000545 llvm::GlobalVariable* ObjCEmptyCacheVar;
546 llvm::GlobalVariable* ObjCEmptyVtableVar;
547
548 /// FinishNonFragileABIModule - Write out global data structures at the end of
549 /// processing a translation unit.
550 void FinishNonFragileABIModule();
551
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000552 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
553 unsigned InstanceStart,
554 unsigned InstanceSize,
555 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000556 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
557 llvm::Constant *IsAGV,
558 llvm::Constant *SuperClassGV,
559 llvm::Constant *ClassRoGV);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000560
561 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
562
563 /// EmitMethodList - Emit the method list for the given
564 /// implementation. The return value has type MethodListnfABITy.
565 llvm::Constant *EmitMethodList(const std::string &Name,
566 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000567 const ConstantVector &Methods);
568 /// EmitIvarList - Emit the ivar list for the given
569 /// implementation. If ForClass is true the list of class ivars
570 /// (i.e. metaclass ivars) is emitted, otherwise the list of
571 /// interface ivars will be emitted. The return value has type
572 /// IvarListnfABIPtrTy.
573 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000574
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000575 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000576 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000577 unsigned long int offset);
578
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000579public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000580 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000581 // FIXME. All stubs for now!
582 virtual llvm::Function *ModuleInitFunction();
583
584 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
585 QualType ResultType,
586 Selector Sel,
587 llvm::Value *Receiver,
588 bool IsClassMessage,
589 const CallArgList &CallArgs)
590 {return RValue::get(0);}
591
592 virtual CodeGen::RValue
593 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
594 QualType ResultType,
595 Selector Sel,
596 const ObjCInterfaceDecl *Class,
597 llvm::Value *Receiver,
598 bool IsClassMessage,
599 const CallArgList &CallArgs){ return RValue::get(0);}
600
601 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
602 const ObjCInterfaceDecl *ID){ return 0; }
603
604 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
605 { return 0; }
606
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000607 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000608
609 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
610
611 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
612 const ObjCProtocolDecl *PD)
613 { return 0; }
614
615 virtual void GenerateProtocol(const ObjCProtocolDecl *PD){ return; }
616
617 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
618 virtual llvm::Function *GetPropertySetFunction()
619 { return 0; }
620 virtual llvm::Function *EnumerationMutationFunction()
621 { return 0; }
622
623 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
624 const Stmt &S)
625 { return; }
626 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
627 const ObjCAtThrowStmt &S)
628 { return; }
629 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
630 llvm::Value *AddrWeakObj)
631 { return 0; }
632 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
633 llvm::Value *src, llvm::Value *dst)
634 { return; }
635 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
636 llvm::Value *src, llvm::Value *dest)
637 { return; }
638 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
639 llvm::Value *src, llvm::Value *dest)
640 { return; }
641 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
642 llvm::Value *src, llvm::Value *dest)
643 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000644};
645
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000646} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000647
648/* *** Helper Functions *** */
649
650/// getConstantGEP() - Help routine to construct simple GEPs.
651static llvm::Constant *getConstantGEP(llvm::Constant *C,
652 unsigned idx0,
653 unsigned idx1) {
654 llvm::Value *Idxs[] = {
655 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
656 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
657 };
658 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
659}
660
661/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000662
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000663CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
664 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000665{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000666 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000667 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000668}
669
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000670/// GetClass - Return a reference to the class for the given interface
671/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000672llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000673 const ObjCInterfaceDecl *ID) {
674 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000675}
676
677/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000678llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000679 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000680}
681
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000682/// Generate a constant CFString object.
683/*
684 struct __builtin_CFString {
685 const int *isa; // point to __CFConstantStringClassReference
686 int flags;
687 const char *str;
688 long length;
689 };
690*/
691
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000692llvm::Constant *CGObjCCommonMac::GenerateConstantString(
693 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000694 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000695}
696
697/// Generates a message send where the super is the receiver. This is
698/// a message send to self with special delivery semantics indicating
699/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000700CodeGen::RValue
701CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000702 QualType ResultType,
703 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000704 const ObjCInterfaceDecl *Class,
705 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000706 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000707 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000708 // Create and init a super structure; this is a (receiver, class)
709 // pair we will pass to objc_msgSendSuper.
710 llvm::Value *ObjCSuper =
711 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
712 llvm::Value *ReceiverAsObject =
713 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
714 CGF.Builder.CreateStore(ReceiverAsObject,
715 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000716
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000717 // If this is a class message the metaclass is passed as the target.
718 llvm::Value *Target;
719 if (IsClassMessage) {
720 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
721 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
722 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
723 Target = Super;
724 } else {
725 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
726 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000727 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
728 // and ObjCTypes types.
729 const llvm::Type *ClassTy =
730 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000731 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000732 CGF.Builder.CreateStore(Target,
733 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
734
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000735 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000736 ObjCSuper, ObjCTypes.SuperPtrCTy,
737 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000738}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000739
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000740/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000741CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000742 QualType ResultType,
743 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000744 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000745 bool IsClassMessage,
746 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000747 llvm::Value *Arg0 =
748 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000749 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000750 Arg0, CGF.getContext().getObjCIdType(),
751 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000752}
753
754CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000755 QualType ResultType,
756 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000757 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000758 QualType Arg0Ty,
759 bool IsSuper,
760 const CallArgList &CallArgs) {
761 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000762 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
763 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
764 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000765 CGF.getContext().getObjCSelType()));
766 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000767
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000768 const llvm::FunctionType *FTy =
769 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
770 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000771
772 llvm::Constant *Fn;
773 if (CGM.ReturnTypeUsesSret(ResultType)) {
774 Fn = ObjCTypes.getSendStretFn(IsSuper);
775 } else if (ResultType->isFloatingType()) {
776 // FIXME: Sadly, this is wrong. This actually depends on the
777 // architecture. This happens to be right for x86-32 though.
778 Fn = ObjCTypes.getSendFpretFn(IsSuper);
779 } else {
780 Fn = ObjCTypes.getSendFn(IsSuper);
781 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000782 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000783 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000784}
785
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000786llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000787 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000788 // FIXME: I don't understand why gcc generates this, or where it is
789 // resolved. Investigate. Its also wasteful to look this up over and
790 // over.
791 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
792
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000793 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
794 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000795}
796
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000797void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
798 // FIXME: We shouldn't need this, the protocol decl should contain
799 // enough information to tell us whether this was a declaration or a
800 // definition.
801 DefinedProtocols.insert(PD->getIdentifier());
802
803 // If we have generated a forward reference to this protocol, emit
804 // it now. Otherwise do nothing, the protocol objects are lazily
805 // emitted.
806 if (Protocols.count(PD->getIdentifier()))
807 GetOrEmitProtocol(PD);
808}
809
810llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
811 if (DefinedProtocols.count(PD->getIdentifier()))
812 return GetOrEmitProtocol(PD);
813 return GetOrEmitProtocolRef(PD);
814}
815
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000816/*
817 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
818 struct _objc_protocol {
819 struct _objc_protocol_extension *isa;
820 char *protocol_name;
821 struct _objc_protocol_list *protocol_list;
822 struct _objc__method_prototype_list *instance_methods;
823 struct _objc__method_prototype_list *class_methods
824 };
825
826 See EmitProtocolExtension().
827*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000828llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
829 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
830
831 // Early exit if a defining object has already been generated.
832 if (Entry && Entry->hasInitializer())
833 return Entry;
834
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000835 // FIXME: I don't understand why gcc generates this, or where it is
836 // resolved. Investigate. Its also wasteful to look this up over and
837 // over.
838 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
839
Chris Lattner8ec03f52008-11-24 03:54:41 +0000840 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000841
842 // Construct method lists.
843 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
844 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
845 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
846 e = PD->instmeth_end(); i != e; ++i) {
847 ObjCMethodDecl *MD = *i;
848 llvm::Constant *C = GetMethodDescriptionConstant(MD);
849 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
850 OptInstanceMethods.push_back(C);
851 } else {
852 InstanceMethods.push_back(C);
853 }
854 }
855
856 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
857 e = PD->classmeth_end(); i != e; ++i) {
858 ObjCMethodDecl *MD = *i;
859 llvm::Constant *C = GetMethodDescriptionConstant(MD);
860 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
861 OptClassMethods.push_back(C);
862 } else {
863 ClassMethods.push_back(C);
864 }
865 }
866
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000867 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000868 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000869 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000870 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000871 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000872 PD->protocol_begin(),
873 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000874 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000875 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
876 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000877 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
878 InstanceMethods);
879 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000880 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
881 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000882 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
883 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000884 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
885 Values);
886
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000887 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000888 // Already created, fix the linkage and update the initializer.
889 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000890 Entry->setInitializer(Init);
891 } else {
892 Entry =
893 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
894 llvm::GlobalValue::InternalLinkage,
895 Init,
896 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
897 &CGM.getModule());
898 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
899 UsedGlobals.push_back(Entry);
900 // FIXME: Is this necessary? Why only for protocol?
901 Entry->setAlignment(4);
902 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000903
904 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000905}
906
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000907llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
909
910 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000911 // We use the initializer as a marker of whether this is a forward
912 // reference or not. At module finalization we add the empty
913 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000914 Entry =
915 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000916 llvm::GlobalValue::ExternalLinkage,
917 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000918 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000919 &CGM.getModule());
920 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
921 UsedGlobals.push_back(Entry);
922 // FIXME: Is this necessary? Why only for protocol?
923 Entry->setAlignment(4);
924 }
925
926 return Entry;
927}
928
929/*
930 struct _objc_protocol_extension {
931 uint32_t size;
932 struct objc_method_description_list *optional_instance_methods;
933 struct objc_method_description_list *optional_class_methods;
934 struct objc_property_list *instance_properties;
935 };
936*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000937llvm::Constant *
938CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
939 const ConstantVector &OptInstanceMethods,
940 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000941 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000942 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000943 std::vector<llvm::Constant*> Values(4);
944 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000945 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000946 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
947 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000948 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
949 OptInstanceMethods);
950 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000951 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
952 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000953 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
954 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000955 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
956 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000957 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000958
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000959 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000960 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
961 Values[3]->isNullValue())
962 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
963
964 llvm::Constant *Init =
965 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
966 llvm::GlobalVariable *GV =
967 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
968 llvm::GlobalValue::InternalLinkage,
969 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000970 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000971 &CGM.getModule());
972 // No special section, but goes in llvm.used
973 UsedGlobals.push_back(GV);
974
975 return GV;
976}
977
978/*
979 struct objc_protocol_list {
980 struct objc_protocol_list *next;
981 long count;
982 Protocol *list[];
983 };
984*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000985llvm::Constant *
986CGObjCMac::EmitProtocolList(const std::string &Name,
987 ObjCProtocolDecl::protocol_iterator begin,
988 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000989 std::vector<llvm::Constant*> ProtocolRefs;
990
Daniel Dunbardbc933702008-08-21 21:57:41 +0000991 for (; begin != end; ++begin)
992 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000993
994 // Just return null for empty protocol lists
995 if (ProtocolRefs.empty())
996 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
997
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000998 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1000
1001 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001002 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001003 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1004 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1005 Values[2] =
1006 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1007 ProtocolRefs.size()),
1008 ProtocolRefs);
1009
1010 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1011 llvm::GlobalVariable *GV =
1012 new llvm::GlobalVariable(Init->getType(), false,
1013 llvm::GlobalValue::InternalLinkage,
1014 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001015 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016 &CGM.getModule());
1017 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1018 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1019}
1020
1021/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001022 struct _objc_property {
1023 const char * const name;
1024 const char * const attributes;
1025 };
1026
1027 struct _objc_property_list {
1028 uint32_t entsize; // sizeof (struct _objc_property)
1029 uint32_t prop_count;
1030 struct _objc_property[prop_count];
1031 };
1032*/
1033llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001034 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +00001035 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001036 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001037 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1038 E = OCD->prop_end(); I != E; ++I) {
1039 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001040 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001041 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001042 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1043 Prop));
1044 }
1045
1046 // Return null for empty list.
1047 if (Properties.empty())
1048 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1049
1050 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001051 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001052 std::vector<llvm::Constant*> Values(3);
1053 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1054 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1055 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1056 Properties.size());
1057 Values[2] = llvm::ConstantArray::get(AT, Properties);
1058 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1059
1060 llvm::GlobalVariable *GV =
1061 new llvm::GlobalVariable(Init->getType(), false,
1062 llvm::GlobalValue::InternalLinkage,
1063 Init,
1064 Name,
1065 &CGM.getModule());
1066 // No special section on property lists?
1067 UsedGlobals.push_back(GV);
1068 return llvm::ConstantExpr::getBitCast(GV,
1069 ObjCTypes.PropertyListPtrTy);
1070
1071}
1072
1073/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001074 struct objc_method_description_list {
1075 int count;
1076 struct objc_method_description list[];
1077 };
1078*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001079llvm::Constant *
1080CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1081 std::vector<llvm::Constant*> Desc(2);
1082 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1083 ObjCTypes.SelectorPtrTy);
1084 Desc[1] = GetMethodVarType(MD);
1085 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1086 Desc);
1087}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001088
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001089llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1090 const char *Section,
1091 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 // Return null for empty list.
1093 if (Methods.empty())
1094 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1095
1096 std::vector<llvm::Constant*> Values(2);
1097 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1098 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1099 Methods.size());
1100 Values[1] = llvm::ConstantArray::get(AT, Methods);
1101 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1102
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001103 llvm::GlobalVariable *GV =
1104 new llvm::GlobalVariable(Init->getType(), false,
1105 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001106 Init, Name, &CGM.getModule());
1107 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001108 UsedGlobals.push_back(GV);
1109 return llvm::ConstantExpr::getBitCast(GV,
1110 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001111}
1112
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001113/*
1114 struct _objc_category {
1115 char *category_name;
1116 char *class_name;
1117 struct _objc_method_list *instance_methods;
1118 struct _objc_method_list *class_methods;
1119 struct _objc_protocol_list *protocols;
1120 uint32_t size; // <rdar://4585769>
1121 struct _objc_property_list *instance_properties;
1122 };
1123 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001124void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001125 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001126
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001127 // FIXME: This is poor design, the OCD should have a pointer to the
1128 // category decl. Additionally, note that Category can be null for
1129 // the @implementation w/o an @interface case. Sema should just
1130 // create one for us as it does for @implementation so everyone else
1131 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001132 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001133 const ObjCCategoryDecl *Category =
1134 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001135 std::string ExtName(Interface->getNameAsString() + "_" +
1136 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001137
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001138 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1139 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1140 e = OCD->instmeth_end(); i != e; ++i) {
1141 // Instance methods should always be defined.
1142 InstanceMethods.push_back(GetMethodConstant(*i));
1143 }
1144 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1145 e = OCD->classmeth_end(); i != e; ++i) {
1146 // Class methods should always be defined.
1147 ClassMethods.push_back(GetMethodConstant(*i));
1148 }
1149
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001150 std::vector<llvm::Constant*> Values(7);
1151 Values[0] = GetClassName(OCD->getIdentifier());
1152 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001153 Values[2] =
1154 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1155 ExtName,
1156 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001157 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001158 Values[3] =
1159 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1160 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001161 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001162 if (Category) {
1163 Values[4] =
1164 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1165 Category->protocol_begin(),
1166 Category->protocol_end());
1167 } else {
1168 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1169 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001170 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001171
1172 // If there is no category @interface then there can be no properties.
1173 if (Category) {
1174 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001175 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001176 } else {
1177 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1178 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001179
1180 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1181 Values);
1182
1183 llvm::GlobalVariable *GV =
1184 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1185 llvm::GlobalValue::InternalLinkage,
1186 Init,
1187 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1188 &CGM.getModule());
1189 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1190 UsedGlobals.push_back(GV);
1191 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001192}
1193
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001194// FIXME: Get from somewhere?
1195enum ClassFlags {
1196 eClassFlags_Factory = 0x00001,
1197 eClassFlags_Meta = 0x00002,
1198 // <rdr://5142207>
1199 eClassFlags_HasCXXStructors = 0x02000,
1200 eClassFlags_Hidden = 0x20000,
1201 eClassFlags_ABI2_Hidden = 0x00010,
1202 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1203};
1204
1205// <rdr://5142207&4705298&4843145>
1206static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1207 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1208 // FIXME: Support -fvisibility
1209 switch (attr->getVisibility()) {
1210 default:
1211 assert(0 && "Unknown visibility");
1212 return false;
1213 case VisibilityAttr::DefaultVisibility:
1214 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1215 return false;
1216 case VisibilityAttr::HiddenVisibility:
1217 return true;
1218 }
1219 } else {
1220 return false; // FIXME: Support -fvisibility
1221 }
1222}
1223
1224/*
1225 struct _objc_class {
1226 Class isa;
1227 Class super_class;
1228 const char *name;
1229 long version;
1230 long info;
1231 long instance_size;
1232 struct _objc_ivar_list *ivars;
1233 struct _objc_method_list *methods;
1234 struct _objc_cache *cache;
1235 struct _objc_protocol_list *protocols;
1236 // Objective-C 1.0 extensions (<rdr://4585769>)
1237 const char *ivar_layout;
1238 struct _objc_class_ext *ext;
1239 };
1240
1241 See EmitClassExtension();
1242 */
1243void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001244 DefinedSymbols.insert(ID->getIdentifier());
1245
Chris Lattner8ec03f52008-11-24 03:54:41 +00001246 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001247 // FIXME: Gross
1248 ObjCInterfaceDecl *Interface =
1249 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001250 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001251 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001252 Interface->protocol_begin(),
1253 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001254 const llvm::Type *InterfaceTy =
1255 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1256 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001257 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001258
1259 // FIXME: Set CXX-structors flag.
1260 if (IsClassHidden(ID->getClassInterface()))
1261 Flags |= eClassFlags_Hidden;
1262
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001263 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1264 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1265 e = ID->instmeth_end(); i != e; ++i) {
1266 // Instance methods should always be defined.
1267 InstanceMethods.push_back(GetMethodConstant(*i));
1268 }
1269 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1270 e = ID->classmeth_end(); i != e; ++i) {
1271 // Class methods should always be defined.
1272 ClassMethods.push_back(GetMethodConstant(*i));
1273 }
1274
1275 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1276 e = ID->propimpl_end(); i != e; ++i) {
1277 ObjCPropertyImplDecl *PID = *i;
1278
1279 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1280 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1281
1282 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1283 if (llvm::Constant *C = GetMethodConstant(MD))
1284 InstanceMethods.push_back(C);
1285 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1286 if (llvm::Constant *C = GetMethodConstant(MD))
1287 InstanceMethods.push_back(C);
1288 }
1289 }
1290
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001291 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001292 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001293 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001294 // Record a reference to the super class.
1295 LazySymbols.insert(Super->getIdentifier());
1296
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001297 Values[ 1] =
1298 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1299 ObjCTypes.ClassPtrTy);
1300 } else {
1301 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1302 }
1303 Values[ 2] = GetClassName(ID->getIdentifier());
1304 // Version is always 0.
1305 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1306 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1307 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001308 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001309 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001310 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001311 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001312 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001313 // cache is always NULL.
1314 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1315 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001316 // FIXME: Set ivar_layout
1317 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001318 Values[11] = EmitClassExtension(ID);
1319 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1320 Values);
1321
1322 llvm::GlobalVariable *GV =
1323 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1324 llvm::GlobalValue::InternalLinkage,
1325 Init,
1326 std::string("\01L_OBJC_CLASS_")+ClassName,
1327 &CGM.getModule());
1328 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1329 UsedGlobals.push_back(GV);
1330 // FIXME: Why?
1331 GV->setAlignment(32);
1332 DefinedClasses.push_back(GV);
1333}
1334
1335llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1336 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001337 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001338 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001339 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001340 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001341
1342 if (IsClassHidden(ID->getClassInterface()))
1343 Flags |= eClassFlags_Hidden;
1344
1345 std::vector<llvm::Constant*> Values(12);
1346 // The isa for the metaclass is the root of the hierarchy.
1347 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1348 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1349 Root = Super;
1350 Values[ 0] =
1351 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1352 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001353 // The super class for the metaclass is emitted as the name of the
1354 // super class. The runtime fixes this up to point to the
1355 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001356 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1357 Values[ 1] =
1358 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1359 ObjCTypes.ClassPtrTy);
1360 } else {
1361 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1362 }
1363 Values[ 2] = GetClassName(ID->getIdentifier());
1364 // Version is always 0.
1365 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1366 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1367 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001368 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001369 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001370 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001371 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001372 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001373 // cache is always NULL.
1374 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1375 Values[ 9] = Protocols;
1376 // ivar_layout for metaclass is always NULL.
1377 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1378 // The class extension is always unused for metaclasses.
1379 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1380 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1381 Values);
1382
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001383 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001384 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001385
1386 // Check for a forward reference.
1387 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1388 if (GV) {
1389 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1390 "Forward metaclass reference has incorrect type.");
1391 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1392 GV->setInitializer(Init);
1393 } else {
1394 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1395 llvm::GlobalValue::InternalLinkage,
1396 Init, Name,
1397 &CGM.getModule());
1398 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001399 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1400 UsedGlobals.push_back(GV);
1401 // FIXME: Why?
1402 GV->setAlignment(32);
1403
1404 return GV;
1405}
1406
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001407llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001408 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001409
1410 // FIXME: Should we look these up somewhere other than the
1411 // module. Its a bit silly since we only generate these while
1412 // processing an implementation, so exactly one pointer would work
1413 // if know when we entered/exitted an implementation block.
1414
1415 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001416 // Previously, metaclass with internal linkage may have been defined.
1417 // pass 'true' as 2nd argument so it is returned.
1418 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001419 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1420 "Forward metaclass reference has incorrect type.");
1421 return GV;
1422 } else {
1423 // Generate as an external reference to keep a consistent
1424 // module. This will be patched up when we emit the metaclass.
1425 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1426 llvm::GlobalValue::ExternalLinkage,
1427 0,
1428 Name,
1429 &CGM.getModule());
1430 }
1431}
1432
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001433/*
1434 struct objc_class_ext {
1435 uint32_t size;
1436 const char *weak_ivar_layout;
1437 struct _objc_property_list *properties;
1438 };
1439*/
1440llvm::Constant *
1441CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1442 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001443 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001444
1445 std::vector<llvm::Constant*> Values(3);
1446 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001447 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001448 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001449 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001450 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001451
1452 // Return null if no extension bits are used.
1453 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1454 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1455
1456 llvm::Constant *Init =
1457 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1458 llvm::GlobalVariable *GV =
1459 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1460 llvm::GlobalValue::InternalLinkage,
1461 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001462 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001463 &CGM.getModule());
1464 // No special section, but goes in llvm.used
1465 UsedGlobals.push_back(GV);
1466
1467 return GV;
1468}
1469
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001470/// countInheritedIvars - count number of ivars in class and its super class(s)
1471///
1472static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1473 int count = 0;
1474 if (!OI)
1475 return 0;
1476 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1477 if (SuperClass)
1478 count += countInheritedIvars(SuperClass);
1479 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1480 E = OI->ivar_end(); I != E; ++I)
1481 ++count;
1482 return count;
1483}
1484
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001485/*
1486 struct objc_ivar {
1487 char *ivar_name;
1488 char *ivar_type;
1489 int ivar_offset;
1490 };
1491
1492 struct objc_ivar_list {
1493 int ivar_count;
1494 struct objc_ivar list[count];
1495 };
1496 */
1497llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001498 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001499 std::vector<llvm::Constant*> Ivars, Ivar(3);
1500
1501 // When emitting the root class GCC emits ivar entries for the
1502 // actual class structure. It is not clear if we need to follow this
1503 // behavior; for now lets try and get away with not doing it. If so,
1504 // the cleanest solution would be to make up an ObjCInterfaceDecl
1505 // for the class.
1506 if (ForClass)
1507 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001508
1509 ObjCInterfaceDecl *OID =
1510 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1511 const llvm::Type *InterfaceTy =
1512 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001513 const llvm::StructLayout *Layout =
1514 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001515
1516 RecordDecl::field_iterator ifield, pfield;
1517 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001518 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1519 FieldDecl *Field = *ifield;
1520 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1521 getLLVMFieldNo(Field));
1522 if (Field->getIdentifier())
1523 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1524 else
1525 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001526 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001527 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001528 Ivar[1] = GetMethodVarType(TypeStr);
1529 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001530 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001531 }
1532
1533 // Return null for empty list.
1534 if (Ivars.empty())
1535 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1536
1537 std::vector<llvm::Constant*> Values(2);
1538 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1539 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1540 Ivars.size());
1541 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1542 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1543
1544 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1545 "\01L_OBJC_INSTANCE_VARIABLES_");
1546 llvm::GlobalVariable *GV =
1547 new llvm::GlobalVariable(Init->getType(), false,
1548 llvm::GlobalValue::InternalLinkage,
1549 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001550 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001551 &CGM.getModule());
1552 if (ForClass) {
1553 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1554 // FIXME: Why is this only here?
1555 GV->setAlignment(32);
1556 } else {
1557 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1558 }
1559 UsedGlobals.push_back(GV);
1560 return llvm::ConstantExpr::getBitCast(GV,
1561 ObjCTypes.IvarListPtrTy);
1562}
1563
1564/*
1565 struct objc_method {
1566 SEL method_name;
1567 char *method_types;
1568 void *method;
1569 };
1570
1571 struct objc_method_list {
1572 struct objc_method_list *obsolete;
1573 int count;
1574 struct objc_method methods_list[count];
1575 };
1576*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001577
1578/// GetMethodConstant - Return a struct objc_method constant for the
1579/// given method if it has been defined. The result is null if the
1580/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001581llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001582 // FIXME: Use DenseMap::lookup
1583 llvm::Function *Fn = MethodDefinitions[MD];
1584 if (!Fn)
1585 return 0;
1586
1587 std::vector<llvm::Constant*> Method(3);
1588 Method[0] =
1589 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1590 ObjCTypes.SelectorPtrTy);
1591 Method[1] = GetMethodVarType(MD);
1592 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1593 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1594}
1595
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001596llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1597 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001598 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001599 // Return null for empty list.
1600 if (Methods.empty())
1601 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1602
1603 std::vector<llvm::Constant*> Values(3);
1604 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1605 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1606 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1607 Methods.size());
1608 Values[2] = llvm::ConstantArray::get(AT, Methods);
1609 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1610
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001611 llvm::GlobalVariable *GV =
1612 new llvm::GlobalVariable(Init->getType(), false,
1613 llvm::GlobalValue::InternalLinkage,
1614 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001615 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001616 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001617 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001618 UsedGlobals.push_back(GV);
1619 return llvm::ConstantExpr::getBitCast(GV,
1620 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001621}
1622
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001623llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001624 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001625 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001626 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001627
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001628 const llvm::FunctionType *MethodTy =
1629 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001630 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001631 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001632 llvm::GlobalValue::InternalLinkage,
1633 Name,
1634 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001635 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001636
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001637 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001638}
1639
1640llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001641 // Abuse this interface function as a place to finalize.
1642 FinishModule();
1643
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001644 return NULL;
1645}
1646
Daniel Dunbar49f66022008-09-24 03:38:44 +00001647llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1648 return ObjCTypes.GetPropertyFn;
1649}
1650
1651llvm::Function *CGObjCMac::GetPropertySetFunction() {
1652 return ObjCTypes.SetPropertyFn;
1653}
1654
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001655llvm::Function *CGObjCMac::EnumerationMutationFunction()
1656{
1657 return ObjCTypes.EnumerationMutationFn;
1658}
1659
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001660/*
1661
1662Objective-C setjmp-longjmp (sjlj) Exception Handling
1663--
1664
1665The basic framework for a @try-catch-finally is as follows:
1666{
1667 objc_exception_data d;
1668 id _rethrow = null;
1669
1670 objc_exception_try_enter(&d);
1671 if (!setjmp(d.jmp_buf)) {
1672 ... try body ...
1673 } else {
1674 // exception path
1675 id _caught = objc_exception_extract(&d);
1676
1677 // enter new try scope for handlers
1678 if (!setjmp(d.jmp_buf)) {
1679 ... match exception and execute catch blocks ...
1680
1681 // fell off end, rethrow.
1682 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001683 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001684 } else {
1685 // exception in catch block
1686 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001687 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001688 }
1689 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001690 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001691
1692finally:
1693 // match either the initial try_enter or the catch try_enter,
1694 // depending on the path followed.
1695 objc_exception_try_exit(&d);
1696finally_no_exit:
1697 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001698 ... dispatch to finally destination ...
1699
1700finally_rethrow:
1701 objc_exception_throw(_rethrow);
1702
1703finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001704}
1705
1706This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001707uses _rethrow to determine if objc_exception_try_exit should be called
1708and if the object should be rethrown. This breaks in the face of
1709throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001710
1711We specialize this framework for a few particular circumstances:
1712
1713 - If there are no catch blocks, then we avoid emitting the second
1714 exception handling context.
1715
1716 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1717 e)) we avoid emitting the code to rethrow an uncaught exception.
1718
1719 - FIXME: If there is no @finally block we can do a few more
1720 simplifications.
1721
1722Rethrows and Jumps-Through-Finally
1723--
1724
1725Support for implicit rethrows and jumping through the finally block is
1726handled by storing the current exception-handling context in
1727ObjCEHStack.
1728
Daniel Dunbar898d5082008-09-30 01:06:03 +00001729In order to implement proper @finally semantics, we support one basic
1730mechanism for jumping through the finally block to an arbitrary
1731destination. Constructs which generate exits from a @try or @catch
1732block use this mechanism to implement the proper semantics by chaining
1733jumps, as necessary.
1734
1735This mechanism works like the one used for indirect goto: we
1736arbitrarily assign an ID to each destination and store the ID for the
1737destination in a variable prior to entering the finally block. At the
1738end of the finally block we simply create a switch to the proper
1739destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001740
1741Code gen for @synchronized(expr) stmt;
1742Effectively generating code for:
1743objc_sync_enter(expr);
1744@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001745*/
1746
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001747void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1748 const Stmt &S) {
1749 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001750 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001751 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1752 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1753 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1754 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001755 llvm::Value *DestCode =
1756 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1757
1758 // Generate jump code. Done here so we can directly add things to
1759 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001760 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001761 llvm::SwitchInst *FinallySwitch =
1762 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1763 FinallyEnd, 10, FinallyJump);
1764
1765 // Push an EH context entry, used for handling rethrows and jumps
1766 // through finally.
1767 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1768 FinallySwitch, DestCode);
1769 CGF.ObjCEHStack.push_back(&EHEntry);
1770
1771 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001772 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1773 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001774 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1775 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001776 if (!isTry) {
1777 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001778 llvm::Value *Arg = CGF.EmitScalarExpr(
1779 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1780 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1781 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001782 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001783
1784 // Enter a new try block and call setjmp.
1785 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1786 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1787 "jmpbufarray");
1788 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1789 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1790 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001791
Daniel Dunbar55e87422008-11-11 02:29:29 +00001792 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1793 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001794 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001795 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001796
1797 // Emit the @try block.
1798 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001799 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1800 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001801 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001802
1803 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001804 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001805
1806 // Retrieve the exception object. We may emit multiple blocks but
1807 // nothing can cross this so the value is already in SSA form.
1808 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1809 ExceptionData,
1810 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001811 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001812 if (!isTry)
1813 {
1814 CGF.Builder.CreateStore(Caught, RethrowPtr);
1815 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1816 }
1817 else if (const ObjCAtCatchStmt* CatchStmt =
1818 cast<ObjCAtTryStmt>(S).getCatchStmts())
1819 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001820 // Enter a new exception try block (in case a @catch block throws
1821 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001822 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001823
Anders Carlsson80f25672008-09-09 17:59:25 +00001824 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1825 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001826 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001827
Daniel Dunbar55e87422008-11-11 02:29:29 +00001828 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1829 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001830 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001831
1832 CGF.EmitBlock(CatchBlock);
1833
Daniel Dunbar55e40722008-09-27 07:03:52 +00001834 // Handle catch list. As a special case we check if everything is
1835 // matched and avoid generating code for falling off the end if
1836 // so.
1837 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001838 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001839 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001840
Anders Carlssondde0a942008-09-11 09:15:33 +00001841 const DeclStmt *CatchParam =
1842 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001843 const VarDecl *VD = 0;
1844 const PointerType *PT = 0;
1845
Anders Carlsson80f25672008-09-09 17:59:25 +00001846 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001847 if (!CatchParam) {
1848 AllMatched = true;
1849 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001850 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001851 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001852
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001853 // catch(id e) always matches.
1854 // FIXME: For the time being we also match id<X>; this should
1855 // be rejected by Sema instead.
1856 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1857 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001858 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001859 }
1860
Daniel Dunbar55e40722008-09-27 07:03:52 +00001861 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001862 if (CatchParam) {
1863 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001864 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001865 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001866 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001867
Anders Carlssondde0a942008-09-11 09:15:33 +00001868 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001869 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001870 break;
1871 }
1872
Daniel Dunbar129271a2008-09-27 07:36:24 +00001873 assert(PT && "Unexpected non-pointer type in @catch");
1874 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001875 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001876 assert(ObjCType && "Catch parameter must have Objective-C type!");
1877
1878 // Check if the @catch block matches the exception object.
1879 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1880
Anders Carlsson80f25672008-09-09 17:59:25 +00001881 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1882 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001883
Daniel Dunbar55e87422008-11-11 02:29:29 +00001884 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001885
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001886 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001887 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001888
1889 // Emit the @catch block.
1890 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001891 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001892 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001893
1894 llvm::Value *Tmp =
1895 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1896 "tmp");
1897 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001898
1899 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001900 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001901
1902 CGF.EmitBlock(NextCatchBlock);
1903 }
1904
Daniel Dunbar55e40722008-09-27 07:03:52 +00001905 if (!AllMatched) {
1906 // None of the handlers caught the exception, so store it to be
1907 // rethrown at the end of the @finally block.
1908 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001909 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001910 }
1911
1912 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001913 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001914 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1915 ExceptionData),
1916 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001917 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001918 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001919 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001920 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001921 }
1922
Daniel Dunbar898d5082008-09-30 01:06:03 +00001923 // Pop the exception-handling stack entry. It is important to do
1924 // this now, because the code in the @finally block is not in this
1925 // context.
1926 CGF.ObjCEHStack.pop_back();
1927
Anders Carlsson80f25672008-09-09 17:59:25 +00001928 // Emit the @finally block.
1929 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001930 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001931
1932 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001933 if (isTry) {
1934 if (const ObjCAtFinallyStmt* FinallyStmt =
1935 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1936 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1937 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001938 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001939 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001940 // For @synchronized, call objc_sync_enter(sync.expr)
1941 llvm::Value *Arg = CGF.EmitScalarExpr(
1942 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1943 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1944 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1945 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001946
Daniel Dunbar898d5082008-09-30 01:06:03 +00001947 CGF.EmitBlock(FinallyJump);
1948
1949 CGF.EmitBlock(FinallyRethrow);
1950 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1951 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001952 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001953
1954 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001955}
1956
1957void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001958 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001959 llvm::Value *ExceptionAsObject;
1960
1961 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1962 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1963 ExceptionAsObject =
1964 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1965 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001966 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001967 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001968 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001969 }
1970
1971 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001972 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001973
1974 // Clear the insertion point to indicate we are in unreachable code.
1975 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001976}
1977
Daniel Dunbar898d5082008-09-30 01:06:03 +00001978void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1979 llvm::BasicBlock *Dst,
1980 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001981 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001982 return;
1983
1984 // Find the destination code for this block. We always use 0 for the
1985 // fallthrough block (default destination).
1986 llvm::SwitchInst *SI = E->FinallySwitch;
1987 llvm::ConstantInt *ID;
1988 if (Dst == SI->getDefaultDest()) {
1989 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1990 } else {
1991 ID = SI->findCaseDest(Dst);
1992 if (!ID) {
1993 // No code found, get a new unique one by just using the number
1994 // of switch successors.
1995 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1996 SI->addCase(ID, Dst);
1997 }
1998 }
1999
2000 // Set the destination code and branch.
2001 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002002 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002003}
2004
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002005/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002006/// object: objc_read_weak (id *src)
2007///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002008llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002009 llvm::Value *AddrWeakObj)
2010{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002011 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002012 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002013 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002014 return read_weak;
2015}
2016
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002017/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2018/// objc_assign_weak (id src, id *dst)
2019///
2020void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2021 llvm::Value *src, llvm::Value *dst)
2022{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002023 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2024 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002025 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2026 src, dst, "weakassign");
2027 return;
2028}
2029
Fariborz Jahanian58626502008-11-19 00:59:10 +00002030/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2031/// objc_assign_global (id src, id *dst)
2032///
2033void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2034 llvm::Value *src, llvm::Value *dst)
2035{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002036 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2037 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002038 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2039 src, dst, "globalassign");
2040 return;
2041}
2042
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002043/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2044/// objc_assign_ivar (id src, id *dst)
2045///
2046void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2047 llvm::Value *src, llvm::Value *dst)
2048{
2049 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2050 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2051 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2052 src, dst, "assignivar");
2053 return;
2054}
2055
Fariborz Jahanian58626502008-11-19 00:59:10 +00002056/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2057/// objc_assign_strongCast (id src, id *dst)
2058///
2059void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2060 llvm::Value *src, llvm::Value *dst)
2061{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002062 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2063 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002064 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2065 src, dst, "weakassign");
2066 return;
2067}
2068
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002069/* *** Private Interface *** */
2070
2071/// EmitImageInfo - Emit the image info marker used to encode some module
2072/// level information.
2073///
2074/// See: <rdr://4810609&4810587&4810587>
2075/// struct IMAGE_INFO {
2076/// unsigned version;
2077/// unsigned flags;
2078/// };
2079enum ImageInfoFlags {
2080 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2081 eImageInfo_GarbageCollected = (1 << 1),
2082 eImageInfo_GCOnly = (1 << 2)
2083};
2084
2085void CGObjCMac::EmitImageInfo() {
2086 unsigned version = 0; // Version is unused?
2087 unsigned flags = 0;
2088
2089 // FIXME: Fix and continue?
2090 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2091 flags |= eImageInfo_GarbageCollected;
2092 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2093 flags |= eImageInfo_GCOnly;
2094
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002095 // Emitted as int[2];
2096 llvm::Constant *values[2] = {
2097 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2098 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2099 };
2100 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002101 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002102 new llvm::GlobalVariable(AT, true,
2103 llvm::GlobalValue::InternalLinkage,
2104 llvm::ConstantArray::get(AT, values, 2),
2105 "\01L_OBJC_IMAGE_INFO",
2106 &CGM.getModule());
2107
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002108 if (ObjCABI == 1) {
2109 GV->setSection("__OBJC, __image_info,regular");
2110 } else {
2111 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2112 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002113
2114 UsedGlobals.push_back(GV);
2115}
2116
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002117
2118// struct objc_module {
2119// unsigned long version;
2120// unsigned long size;
2121// const char *name;
2122// Symtab symtab;
2123// };
2124
2125// FIXME: Get from somewhere
2126static const int ModuleVersion = 7;
2127
2128void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002129 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002130
2131 std::vector<llvm::Constant*> Values(4);
2132 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2133 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002134 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002135 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002136 Values[3] = EmitModuleSymbols();
2137
2138 llvm::GlobalVariable *GV =
2139 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2140 llvm::GlobalValue::InternalLinkage,
2141 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2142 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002143 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002144 &CGM.getModule());
2145 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2146 UsedGlobals.push_back(GV);
2147}
2148
2149llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002150 unsigned NumClasses = DefinedClasses.size();
2151 unsigned NumCategories = DefinedCategories.size();
2152
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002153 // Return null if no symbols were defined.
2154 if (!NumClasses && !NumCategories)
2155 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2156
2157 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002158 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2159 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2160 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2161 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2162
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002163 // The runtime expects exactly the list of defined classes followed
2164 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002165 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002166 for (unsigned i=0; i<NumClasses; i++)
2167 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2168 ObjCTypes.Int8PtrTy);
2169 for (unsigned i=0; i<NumCategories; i++)
2170 Symbols[NumClasses + i] =
2171 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2172 ObjCTypes.Int8PtrTy);
2173
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002174 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002175 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002176 NumClasses + NumCategories),
2177 Symbols);
2178
2179 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2180
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002181 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002183 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002184 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002185 "\01L_OBJC_SYMBOLS",
2186 &CGM.getModule());
2187 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2188 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002189 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2190}
2191
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002192llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002194 LazySymbols.insert(ID->getIdentifier());
2195
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002196 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2197
2198 if (!Entry) {
2199 llvm::Constant *Casted =
2200 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2201 ObjCTypes.ClassPtrTy);
2202 Entry =
2203 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2204 llvm::GlobalValue::InternalLinkage,
2205 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2206 &CGM.getModule());
2207 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2208 UsedGlobals.push_back(Entry);
2209 }
2210
2211 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002212}
2213
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002214llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002215 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2216
2217 if (!Entry) {
2218 llvm::Constant *Casted =
2219 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2220 ObjCTypes.SelectorPtrTy);
2221 Entry =
2222 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2223 llvm::GlobalValue::InternalLinkage,
2224 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2225 &CGM.getModule());
2226 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2227 UsedGlobals.push_back(Entry);
2228 }
2229
2230 return Builder.CreateLoad(Entry, false, "tmp");
2231}
2232
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002233llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002234 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002235
2236 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002237 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002238 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002239 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002240 llvm::GlobalValue::InternalLinkage,
2241 C, "\01L_OBJC_CLASS_NAME_",
2242 &CGM.getModule());
2243 Entry->setSection("__TEXT,__cstring,cstring_literals");
2244 UsedGlobals.push_back(Entry);
2245 }
2246
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002247 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002248}
2249
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002250llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002251 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2252
2253 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002254 // FIXME: Avoid std::string copying.
2255 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002256 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002257 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002258 llvm::GlobalValue::InternalLinkage,
2259 C, "\01L_OBJC_METH_VAR_NAME_",
2260 &CGM.getModule());
2261 Entry->setSection("__TEXT,__cstring,cstring_literals");
2262 UsedGlobals.push_back(Entry);
2263 }
2264
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002265 return getConstantGEP(Entry, 0, 0);
2266}
2267
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002269llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2271}
2272
2273// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002274llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002275 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2276}
2277
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002278llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002279 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002280
2281 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002282 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002283 Entry =
2284 new llvm::GlobalVariable(C->getType(), false,
2285 llvm::GlobalValue::InternalLinkage,
2286 C, "\01L_OBJC_METH_VAR_TYPE_",
2287 &CGM.getModule());
2288 Entry->setSection("__TEXT,__cstring,cstring_literals");
2289 UsedGlobals.push_back(Entry);
2290 }
2291
2292 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002293}
2294
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002295// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002296llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002297 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002298 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2299 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 return GetMethodVarType(TypeStr);
2301}
2302
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002303// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002304llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002305 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2306
2307 if (!Entry) {
2308 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2309 Entry =
2310 new llvm::GlobalVariable(C->getType(), false,
2311 llvm::GlobalValue::InternalLinkage,
2312 C, "\01L_OBJC_PROP_NAME_ATTR_",
2313 &CGM.getModule());
2314 Entry->setSection("__TEXT,__cstring,cstring_literals");
2315 UsedGlobals.push_back(Entry);
2316 }
2317
2318 return getConstantGEP(Entry, 0, 0);
2319}
2320
2321// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002322// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002323llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002324 const Decl *Container) {
2325 std::string TypeStr;
2326 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002327 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2328}
2329
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002330void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2331 const ObjCContainerDecl *CD,
2332 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002333 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002334 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002335 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002336 assert (CD && "Missing container decl in GetNameForMethod");
2337 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002338 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2339 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002340 NameOut += ' ';
2341 NameOut += D->getSelector().getAsString();
2342 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002343}
2344
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002345/// GetFirstIvarInRecord - This routine returns the record for the
2346/// implementation of the fiven class OID. It also returns field
2347/// corresponding to the first ivar in the class in FIV. It also
2348/// returns the one before the first ivar.
2349///
2350const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2351 const ObjCInterfaceDecl *OID,
2352 RecordDecl::field_iterator &FIV,
2353 RecordDecl::field_iterator &PIV) {
2354 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2355 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2356 RecordDecl::field_iterator ifield = RD->field_begin();
2357 RecordDecl::field_iterator pfield = RD->field_end();
2358 while (countSuperClassIvars-- > 0) {
2359 pfield = ifield;
2360 ++ifield;
2361 }
2362 FIV = ifield;
2363 PIV = pfield;
2364 return RD;
2365}
2366
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002367void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002368 EmitModuleInfo();
2369
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002370 // Emit the dummy bodies for any protocols which were referenced but
2371 // never defined.
2372 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2373 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2374 if (i->second->hasInitializer())
2375 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002376
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002377 std::vector<llvm::Constant*> Values(5);
2378 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2379 Values[1] = GetClassName(i->first);
2380 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2381 Values[3] = Values[4] =
2382 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2383 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2384 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2385 Values));
2386 }
2387
2388 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002389 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002390 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002392 }
2393
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002394 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002395 llvm::GlobalValue *GV =
2396 new llvm::GlobalVariable(AT, false,
2397 llvm::GlobalValue::AppendingLinkage,
2398 llvm::ConstantArray::get(AT, Used),
2399 "llvm.used",
2400 &CGM.getModule());
2401
2402 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002403
2404 // Add assembler directives to add lazy undefined symbol references
2405 // for classes which are referenced but not defined. This is
2406 // important for correct linker interaction.
2407
2408 // FIXME: Uh, this isn't particularly portable.
2409 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002410
2411 if (!CGM.getModule().getModuleInlineAsm().empty())
2412 s << "\n";
2413
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002414 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2415 e = LazySymbols.end(); i != e; ++i) {
2416 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2417 }
2418 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2419 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002420 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002421 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2422 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002423
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002424 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002425}
2426
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002427CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002428 : CGObjCCommonMac(cgm),
2429 ObjCTypes(cgm)
2430{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002431 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002432 ObjCABI = 2;
2433}
2434
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002435/* *** */
2436
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002437ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2438: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002439{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002440 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2441 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002442
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002443 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002444 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002445 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2447
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002448 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002449 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002450 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002451
2452 // FIXME: It would be nice to unify this with the opaque type, so
2453 // that the IR comes out a bit cleaner.
2454 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2455 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002456
2457 // I'm not sure I like this. The implicit coordination is a bit
2458 // gross. We should solve this in a reasonable fashion because this
2459 // is a pretty common task (match some runtime data structure with
2460 // an LLVM data structure).
2461
2462 // FIXME: This is leaked.
2463 // FIXME: Merge with rewriter code?
2464
2465 // struct _objc_super {
2466 // id self;
2467 // Class cls;
2468 // }
2469 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2470 SourceLocation(),
2471 &Ctx.Idents.get("_objc_super"));
2472 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2473 Ctx.getObjCIdType(), 0, false));
2474 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2475 Ctx.getObjCClassType(), 0, false));
2476 RD->completeDefinition(Ctx);
2477
2478 SuperCTy = Ctx.getTagDeclType(RD);
2479 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2480
2481 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002482 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2483
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002484 // struct _prop_t {
2485 // char *name;
2486 // char *attributes;
2487 // }
2488 PropertyTy = llvm::StructType::get(Int8PtrTy,
2489 Int8PtrTy,
2490 NULL);
2491 CGM.getModule().addTypeName("struct._prop_t",
2492 PropertyTy);
2493
2494 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002495 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002496 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002497 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002498 // }
2499 PropertyListTy = llvm::StructType::get(IntTy,
2500 IntTy,
2501 llvm::ArrayType::get(PropertyTy, 0),
2502 NULL);
2503 CGM.getModule().addTypeName("struct._prop_list_t",
2504 PropertyListTy);
2505 // struct _prop_list_t *
2506 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2507
2508 // struct _objc_method {
2509 // SEL _cmd;
2510 // char *method_type;
2511 // char *_imp;
2512 // }
2513 MethodTy = llvm::StructType::get(SelectorPtrTy,
2514 Int8PtrTy,
2515 Int8PtrTy,
2516 NULL);
2517 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002518
2519 // struct _objc_cache *
2520 CacheTy = llvm::OpaqueType::get();
2521 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2522 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002523
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002524 // Property manipulation functions.
2525
2526 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2527 std::vector<const llvm::Type*> Params;
2528 Params.push_back(ObjectPtrTy);
2529 Params.push_back(SelectorPtrTy);
2530 Params.push_back(LongTy);
2531 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2532 GetPropertyFn =
2533 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2534 Params,
2535 false),
2536 "objc_getProperty");
2537
2538 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2539 Params.clear();
2540 Params.push_back(ObjectPtrTy);
2541 Params.push_back(SelectorPtrTy);
2542 Params.push_back(LongTy);
2543 Params.push_back(ObjectPtrTy);
2544 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2545 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2546 SetPropertyFn =
2547 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2548 Params,
2549 false),
2550 "objc_setProperty");
2551 // Enumeration mutation.
2552
2553 Params.clear();
2554 Params.push_back(ObjectPtrTy);
2555 EnumerationMutationFn =
2556 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2557 Params,
2558 false),
2559 "objc_enumerationMutation");
2560
2561 // gc's API
2562 // id objc_read_weak (id *)
2563 Params.clear();
2564 Params.push_back(PtrObjectPtrTy);
2565 GcReadWeakFn =
2566 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2567 Params,
2568 false),
2569 "objc_read_weak");
2570 // id objc_assign_weak (id, id *)
2571 Params.clear();
2572 Params.push_back(ObjectPtrTy);
2573 Params.push_back(PtrObjectPtrTy);
2574 GcAssignWeakFn =
2575 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2576 Params,
2577 false),
2578 "objc_assign_weak");
2579 GcAssignGlobalFn =
2580 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2581 Params,
2582 false),
2583 "objc_assign_global");
2584 GcAssignIvarFn =
2585 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2586 Params,
2587 false),
2588 "objc_assign_ivar");
2589 GcAssignStrongCastFn =
2590 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2591 Params,
2592 false),
2593 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002594}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002595
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002596ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2597 : ObjCCommonTypesHelper(cgm)
2598{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002599 // struct _objc_method_description {
2600 // SEL name;
2601 // char *types;
2602 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002603 MethodDescriptionTy =
2604 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002605 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002606 NULL);
2607 CGM.getModule().addTypeName("struct._objc_method_description",
2608 MethodDescriptionTy);
2609
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002610 // struct _objc_method_description_list {
2611 // int count;
2612 // struct _objc_method_description[1];
2613 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002614 MethodDescriptionListTy =
2615 llvm::StructType::get(IntTy,
2616 llvm::ArrayType::get(MethodDescriptionTy, 0),
2617 NULL);
2618 CGM.getModule().addTypeName("struct._objc_method_description_list",
2619 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002620
2621 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002622 MethodDescriptionListPtrTy =
2623 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2624
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002625 // Protocol description structures
2626
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002627 // struct _objc_protocol_extension {
2628 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2629 // struct _objc_method_description_list *optional_instance_methods;
2630 // struct _objc_method_description_list *optional_class_methods;
2631 // struct _objc_property_list *instance_properties;
2632 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002634 llvm::StructType::get(IntTy,
2635 MethodDescriptionListPtrTy,
2636 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002637 PropertyListPtrTy,
2638 NULL);
2639 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2640 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002641
2642 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002643 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2644
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002645 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002646
2647 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2648 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2649
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002650 const llvm::Type *T =
2651 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2652 LongTy,
2653 llvm::ArrayType::get(ProtocolTyHolder, 0),
2654 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002655 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2656
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002657 // struct _objc_protocol {
2658 // struct _objc_protocol_extension *isa;
2659 // char *protocol_name;
2660 // struct _objc_protocol **_objc_protocol_list;
2661 // struct _objc_method_description_list *instance_methods;
2662 // struct _objc_method_description_list *class_methods;
2663 // }
2664 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002665 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002666 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2667 MethodDescriptionListPtrTy,
2668 MethodDescriptionListPtrTy,
2669 NULL);
2670 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2671
2672 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2673 CGM.getModule().addTypeName("struct._objc_protocol_list",
2674 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002675 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002676 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2677
2678 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002679 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002680 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002681
2682 // Class description structures
2683
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002684 // struct _objc_ivar {
2685 // char *ivar_name;
2686 // char *ivar_type;
2687 // int ivar_offset;
2688 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002689 IvarTy = llvm::StructType::get(Int8PtrTy,
2690 Int8PtrTy,
2691 IntTy,
2692 NULL);
2693 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2694
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002695 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002696 IvarListTy = llvm::OpaqueType::get();
2697 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2698 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2699
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002700 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002701 MethodListTy = llvm::OpaqueType::get();
2702 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2703 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2704
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002705 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002706 ClassExtensionTy =
2707 llvm::StructType::get(IntTy,
2708 Int8PtrTy,
2709 PropertyListPtrTy,
2710 NULL);
2711 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2712 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2713
2714 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2715
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002716 // struct _objc_class {
2717 // Class isa;
2718 // Class super_class;
2719 // char *name;
2720 // long version;
2721 // long info;
2722 // long instance_size;
2723 // struct _objc_ivar_list *ivars;
2724 // struct _objc_method_list *methods;
2725 // struct _objc_cache *cache;
2726 // struct _objc_protocol_list *protocols;
2727 // char *ivar_layout;
2728 // struct _objc_class_ext *ext;
2729 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002730 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2731 llvm::PointerType::getUnqual(ClassTyHolder),
2732 Int8PtrTy,
2733 LongTy,
2734 LongTy,
2735 LongTy,
2736 IvarListPtrTy,
2737 MethodListPtrTy,
2738 CachePtrTy,
2739 ProtocolListPtrTy,
2740 Int8PtrTy,
2741 ClassExtensionPtrTy,
2742 NULL);
2743 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2744
2745 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2746 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2747 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2748
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002749 // struct _objc_category {
2750 // char *category_name;
2751 // char *class_name;
2752 // struct _objc_method_list *instance_method;
2753 // struct _objc_method_list *class_method;
2754 // uint32_t size; // sizeof(struct _objc_category)
2755 // struct _objc_property_list *instance_properties;// category's @property
2756 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002757 CategoryTy = llvm::StructType::get(Int8PtrTy,
2758 Int8PtrTy,
2759 MethodListPtrTy,
2760 MethodListPtrTy,
2761 ProtocolListPtrTy,
2762 IntTy,
2763 PropertyListPtrTy,
2764 NULL);
2765 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2766
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002767 // Global metadata structures
2768
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002769 // struct _objc_symtab {
2770 // long sel_ref_cnt;
2771 // SEL *refs;
2772 // short cls_def_cnt;
2773 // short cat_def_cnt;
2774 // char *defs[cls_def_cnt + cat_def_cnt];
2775 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002776 SymtabTy = llvm::StructType::get(LongTy,
2777 SelectorPtrTy,
2778 ShortTy,
2779 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002780 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002781 NULL);
2782 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2783 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2784
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002785 // struct _objc_module {
2786 // long version;
2787 // long size; // sizeof(struct _objc_module)
2788 // char *name;
2789 // struct _objc_symtab* symtab;
2790 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002791 ModuleTy =
2792 llvm::StructType::get(LongTy,
2793 LongTy,
2794 Int8PtrTy,
2795 SymtabPtrTy,
2796 NULL);
2797 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002798
Daniel Dunbar49f66022008-09-24 03:38:44 +00002799 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002800
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002801 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002802 std::vector<const llvm::Type*> Params;
2803 Params.push_back(ObjectPtrTy);
2804 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002805 MessageSendFn =
2806 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2807 Params,
2808 true),
2809 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002810
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002811 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002812 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002813 Params.push_back(ObjectPtrTy);
2814 Params.push_back(SelectorPtrTy);
2815 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002816 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2817 Params,
2818 true),
2819 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002820
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002821 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002822 Params.clear();
2823 Params.push_back(ObjectPtrTy);
2824 Params.push_back(SelectorPtrTy);
2825 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002826 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002827 MessageSendFpretFn =
2828 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2829 Params,
2830 true),
2831 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002832
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002833 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002834 Params.clear();
2835 Params.push_back(SuperPtrTy);
2836 Params.push_back(SelectorPtrTy);
2837 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002838 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2839 Params,
2840 true),
2841 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002842
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002843 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2844 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002845 Params.clear();
2846 Params.push_back(Int8PtrTy);
2847 Params.push_back(SuperPtrTy);
2848 Params.push_back(SelectorPtrTy);
2849 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002850 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2851 Params,
2852 true),
2853 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002854
2855 // There is no objc_msgSendSuper_fpret? How can that work?
2856 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002857
Anders Carlsson124526b2008-09-09 10:10:21 +00002858 // FIXME: This is the size of the setjmp buffer and should be
2859 // target specific. 18 is what's used on 32-bit X86.
2860 uint64_t SetJmpBufferSize = 18;
2861
2862 // Exceptions
2863 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002864 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002865
2866 ExceptionDataTy =
2867 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2868 SetJmpBufferSize),
2869 StackPtrTy, NULL);
2870 CGM.getModule().addTypeName("struct._objc_exception_data",
2871 ExceptionDataTy);
2872
2873 Params.clear();
2874 Params.push_back(ObjectPtrTy);
2875 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002876 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2877 Params,
2878 false),
2879 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002880
2881 Params.clear();
2882 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2883 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002884 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2885 Params,
2886 false),
2887 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002888 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002889 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2890 Params,
2891 false),
2892 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002893 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002894 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2895 Params,
2896 false),
2897 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002898
2899 Params.clear();
2900 Params.push_back(ClassPtrTy);
2901 Params.push_back(ObjectPtrTy);
2902 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002903 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2904 Params,
2905 false),
2906 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002907
2908 // synchronized APIs
2909 // void objc_sync_enter (id)
2910 Params.clear();
2911 Params.push_back(ObjectPtrTy);
2912 SyncEnterFn =
2913 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2914 Params,
2915 false),
2916 "objc_sync_enter");
2917 // void objc_sync_exit (id)
2918 SyncExitFn =
2919 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2920 Params,
2921 false),
2922 "objc_sync_exit");
2923
Anders Carlsson124526b2008-09-09 10:10:21 +00002924
2925 Params.clear();
2926 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2927 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002928 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2929 Params,
2930 false),
2931 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002932
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002933}
2934
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002935ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002936: ObjCCommonTypesHelper(cgm)
2937{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002938 // struct _method_list_t {
2939 // uint32_t entsize; // sizeof(struct _objc_method)
2940 // uint32_t method_count;
2941 // struct _objc_method method_list[method_count];
2942 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002943 MethodListnfABITy = llvm::StructType::get(IntTy,
2944 IntTy,
2945 llvm::ArrayType::get(MethodTy, 0),
2946 NULL);
2947 CGM.getModule().addTypeName("struct.__method_list_t",
2948 MethodListnfABITy);
2949 // struct method_list_t *
2950 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002951
2952 // struct _protocol_t {
2953 // id isa; // NULL
2954 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002955 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002956 // const struct method_list_t * const instance_methods;
2957 // const struct method_list_t * const class_methods;
2958 // const struct method_list_t *optionalInstanceMethods;
2959 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002960 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002961 // const uint32_t size; // sizeof(struct _protocol_t)
2962 // const uint32_t flags; // = 0
2963 // }
2964
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002965 // Holder for struct _protocol_list_t *
2966 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2967
2968 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2969 Int8PtrTy,
2970 llvm::PointerType::getUnqual(
2971 ProtocolListTyHolder),
2972 MethodListnfABIPtrTy,
2973 MethodListnfABIPtrTy,
2974 MethodListnfABIPtrTy,
2975 MethodListnfABIPtrTy,
2976 PropertyListPtrTy,
2977 IntTy,
2978 IntTy,
2979 NULL);
2980 CGM.getModule().addTypeName("struct._protocol_t",
2981 ProtocolnfABITy);
2982
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002983 // struct _objc_protocol_list {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002984 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002985 // struct _protocol_t[protocol_count];
2986 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002987 ProtocolListnfABITy = llvm::StructType::get(LongTy,
2988 llvm::ArrayType::get(
2989 ProtocolnfABITy, 0),
2990 NULL);
2991 CGM.getModule().addTypeName("struct._objc_protocol_list",
2992 ProtocolListnfABITy);
2993
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002994 // struct _objc_protocol_list*
2995 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002996
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002997 // FIXME! Is this doing the right thing?
2998 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
2999 ProtocolListnfABIPtrTy);
3000
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003001 // struct _ivar_t {
3002 // unsigned long int *offset; // pointer to ivar offset location
3003 // char *name;
3004 // char *type;
3005 // uint32_t alignment;
3006 // uint32_t size;
3007 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003008 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3009 Int8PtrTy,
3010 Int8PtrTy,
3011 IntTy,
3012 IntTy,
3013 NULL);
3014 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3015
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003016 // struct _ivar_list_t {
3017 // uint32 entsize; // sizeof(struct _ivar_t)
3018 // uint32 count;
3019 // struct _iver_t list[count];
3020 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003021 IvarListnfABITy = llvm::StructType::get(IntTy,
3022 IntTy,
3023 llvm::ArrayType::get(
3024 IvarnfABITy, 0),
3025 NULL);
3026 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3027
3028 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003029
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003030 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003031 // uint32_t const flags;
3032 // uint32_t const instanceStart;
3033 // uint32_t const instanceSize;
3034 // uint32_t const reserved; // only when building for 64bit targets
3035 // const uint8_t * const ivarLayout;
3036 // const char *const name;
3037 // const struct _method_list_t * const baseMethods;
3038 // const struct _objc_protocol_list *const baseProtocols;
3039 // const struct _ivar_list_t *const ivars;
3040 // const uint8_t * const weakIvarLayout;
3041 // const struct _prop_list_t * const properties;
3042 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003043
3044 // FIXME. Add 'reserved' field in 64bit abi mode!
3045 ClassRonfABITy = llvm::StructType::get(IntTy,
3046 IntTy,
3047 IntTy,
3048 Int8PtrTy,
3049 Int8PtrTy,
3050 MethodListnfABIPtrTy,
3051 ProtocolListnfABIPtrTy,
3052 IvarListnfABIPtrTy,
3053 Int8PtrTy,
3054 PropertyListPtrTy,
3055 NULL);
3056 CGM.getModule().addTypeName("struct._class_ro_t",
3057 ClassRonfABITy);
3058
3059 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3060 std::vector<const llvm::Type*> Params;
3061 Params.push_back(ObjectPtrTy);
3062 Params.push_back(SelectorPtrTy);
3063 ImpnfABITy = llvm::PointerType::getUnqual(
3064 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3065
3066 // struct _class_t {
3067 // struct _class_t *isa;
3068 // struct _class_t * const superclass;
3069 // void *cache;
3070 // IMP *vtable;
3071 // struct class_ro_t *ro;
3072 // }
3073
3074 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3075 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3076 llvm::PointerType::getUnqual(ClassTyHolder),
3077 CachePtrTy,
3078 llvm::PointerType::getUnqual(ImpnfABITy),
3079 llvm::PointerType::getUnqual(
3080 ClassRonfABITy),
3081 NULL);
3082 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3083
3084 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3085 ClassnfABITy);
3086
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003087 // LLVM for struct _class_t *
3088 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3089
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003090 // struct _category_t {
3091 // const char * const name;
3092 // struct _class_t *const cls;
3093 // const struct _method_list_t * const instance_methods;
3094 // const struct _method_list_t * const class_methods;
3095 // const struct _protocol_list_t * const protocols;
3096 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003097 // }
3098 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003099 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003100 MethodListnfABIPtrTy,
3101 MethodListnfABIPtrTy,
3102 ProtocolListnfABIPtrTy,
3103 PropertyListPtrTy,
3104 NULL);
3105 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3106
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003107}
3108
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003109llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3110 FinishNonFragileABIModule();
3111
3112 return NULL;
3113}
3114
3115void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3116 // nonfragile abi has no module definition.
3117 std::vector<llvm::Constant*> Used;
3118 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3119 e = UsedGlobals.end(); i != e; ++i) {
3120 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3121 }
3122
3123 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3124 llvm::GlobalValue *GV =
3125 new llvm::GlobalVariable(AT, false,
3126 llvm::GlobalValue::AppendingLinkage,
3127 llvm::ConstantArray::get(AT, Used),
3128 "llvm.used",
3129 &CGM.getModule());
3130
3131 GV->setSection("llvm.metadata");
3132
3133}
3134
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003135// Metadata flags
3136enum MetaDataDlags {
3137 CLS = 0x0,
3138 CLS_META = 0x1,
3139 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003140 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003141 CLS_EXCEPTION = 0x20
3142};
3143/// BuildClassRoTInitializer - generate meta-data for:
3144/// struct _class_ro_t {
3145/// uint32_t const flags;
3146/// uint32_t const instanceStart;
3147/// uint32_t const instanceSize;
3148/// uint32_t const reserved; // only when building for 64bit targets
3149/// const uint8_t * const ivarLayout;
3150/// const char *const name;
3151/// const struct _method_list_t * const baseMethods;
3152/// const struct _objc_protocol_list *const baseProtocols;
3153/// const struct _ivar_list_t *const ivars;
3154/// const uint8_t * const weakIvarLayout;
3155/// const struct _prop_list_t * const properties;
3156/// }
3157///
3158llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3159 unsigned flags,
3160 unsigned InstanceStart,
3161 unsigned InstanceSize,
3162 const ObjCImplementationDecl *ID) {
3163 std::string ClassName = ID->getNameAsString();
3164 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3165 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3166 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3167 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3168 // FIXME. For 64bit targets add 0 here.
3169 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3170 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003171 // const struct _method_list_t * const baseMethods;
3172 std::vector<llvm::Constant*> Methods;
3173 std::string MethodListName("\01l_OBJC_$_");
3174 if (flags & CLS_META) {
3175 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3176 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3177 e = ID->classmeth_end(); i != e; ++i) {
3178 // Class methods should always be defined.
3179 Methods.push_back(GetMethodConstant(*i));
3180 }
3181 } else {
3182 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3183 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3184 e = ID->instmeth_end(); i != e; ++i) {
3185 // Instance methods should always be defined.
3186 Methods.push_back(GetMethodConstant(*i));
3187 }
3188 }
3189 // FIXME. Section may always be .data
3190 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003191 "__DATA, __objc_const", Methods);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003192 Values[ 6] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003193 if (flags & CLS_META)
3194 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3195 else
3196 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003197 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3198 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3199 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3200 Values);
3201 llvm::GlobalVariable *CLASS_RO_GV =
3202 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3203 llvm::GlobalValue::InternalLinkage,
3204 Init,
3205 (flags & CLS_META) ?
3206 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3207 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3208 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003209 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003210 UsedGlobals.push_back(CLASS_RO_GV);
3211 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003212
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003213}
3214
3215/// BuildClassMetaData - This routine defines that to-level meta-data
3216/// for the given ClassName for:
3217/// struct _class_t {
3218/// struct _class_t *isa;
3219/// struct _class_t * const superclass;
3220/// void *cache;
3221/// IMP *vtable;
3222/// struct class_ro_t *ro;
3223/// }
3224///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003225llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3226 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003227 llvm::Constant *IsAGV,
3228 llvm::Constant *SuperClassGV,
3229 llvm::Constant *ClassRoGV) {
3230 std::vector<llvm::Constant*> Values(5);
3231 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003232 Values[1] = SuperClassGV
3233 ? SuperClassGV
3234 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003235 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3236 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3237 Values[4] = ClassRoGV; // &CLASS_RO_GV
3238 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3239 Values);
3240 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3241 if (GV)
3242 GV->setInitializer(Init);
3243 else
3244 GV =
3245 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3246 llvm::GlobalValue::ExternalLinkage,
3247 Init,
3248 ClassName,
3249 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003250 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003251 UsedGlobals.push_back(GV);
3252 // FIXME! why?
3253 GV->setAlignment(32);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003254 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003255}
3256
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003257void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3258 std::string ClassName = ID->getNameAsString();
3259 if (!ObjCEmptyCacheVar) {
3260 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3261 ObjCTypes.CachePtrTy,
3262 false,
3263 llvm::GlobalValue::ExternalLinkage,
3264 0,
3265 "\01_objc_empty_cache",
3266 &CGM.getModule());
3267 UsedGlobals.push_back(ObjCEmptyCacheVar);
3268
3269 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3270 llvm::PointerType::getUnqual(
3271 ObjCTypes.ImpnfABITy),
3272 false,
3273 llvm::GlobalValue::ExternalLinkage,
3274 0,
3275 "\01_objc_empty_vtable",
3276 &CGM.getModule());
3277 UsedGlobals.push_back(ObjCEmptyVtableVar);
3278 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003279 assert(ID->getClassInterface() &&
3280 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003281 uint32_t InstanceStart =
3282 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3283 uint32_t InstanceSize = InstanceStart;
3284 uint32_t flags = CLS_META;
3285 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3286 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003287
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003288 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003289
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003290 if (IsClassHidden(ID->getClassInterface()))
3291 flags |= OBJC2_CLS_HIDDEN;
3292 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003293 // class is root
3294 flags |= CLS_ROOT;
3295 std::string SuperClassName = ObjCClassName + ClassName;
3296 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3297 if (!SuperClassGV)
3298 SuperClassGV =
3299 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3300 llvm::GlobalValue::ExternalLinkage,
3301 0,
3302 SuperClassName,
3303 &CGM.getModule());
3304 UsedGlobals.push_back(SuperClassGV);
3305 std::string IsAClassName = ObjCMetaClassName + ClassName;
3306 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3307 if (!IsAGV)
3308 IsAGV =
3309 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3310 llvm::GlobalValue::ExternalLinkage,
3311 0,
3312 IsAClassName,
3313 &CGM.getModule());
3314 UsedGlobals.push_back(IsAGV);
3315 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003316 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003317 std::string RootClassName =
3318 ID->getClassInterface()->getSuperClass()->getNameAsString();
3319 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3320 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3321 if (!SuperClassGV)
3322 SuperClassGV =
3323 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3324 llvm::GlobalValue::ExternalLinkage,
3325 0,
3326 SuperClassName,
3327 &CGM.getModule());
3328 UsedGlobals.push_back(SuperClassGV);
3329 IsAGV = SuperClassGV;
3330 }
3331 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3332 InstanceStart,
3333 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003334 std::string TClassName = ObjCMetaClassName + ClassName;
3335 llvm::GlobalVariable *MetaTClass =
3336 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3337
3338 // Metadata for the class
3339 flags = CLS;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003340 if (IsClassHidden(ID->getClassInterface()))
3341 flags |= OBJC2_CLS_HIDDEN;
3342 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003343 flags |= CLS_ROOT;
3344 SuperClassGV = 0;
3345 }
3346 else {
3347 // Has a root. Current class is not a root.
3348 std::string RootClassName =
3349 ID->getClassInterface()->getSuperClass()->getNameAsString();
3350 std::string SuperClassName = ObjCClassName + RootClassName;
3351 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3352 if (!SuperClassGV)
3353 SuperClassGV =
3354 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3355 llvm::GlobalValue::ExternalLinkage,
3356 0,
3357 SuperClassName,
3358 &CGM.getModule());
3359 UsedGlobals.push_back(SuperClassGV);
3360
3361 }
3362
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003363 InstanceStart = InstanceSize = 0;
3364 if (ObjCInterfaceDecl *OID =
3365 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3366 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003367 const llvm::Type *InterfaceTy =
3368 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3369 const llvm::StructLayout *Layout =
3370 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003371
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003372 RecordDecl::field_iterator firstField, lastField;
3373 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003374
3375 for (RecordDecl::field_iterator e = RD->field_end(),
3376 ifield = firstField; ifield != e; ++ifield)
3377 lastField = ifield;
3378
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003379 if (lastField != RD->field_end()) {
3380 FieldDecl *Field = *lastField;
3381 const llvm::Type *FieldTy =
3382 CGM.getTypes().ConvertTypeForMem(Field->getType());
3383 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3384 InstanceSize = Layout->getElementOffset(
3385 CGM.getTypes().getLLVMFieldNo(Field)) +
3386 Size;
3387 if (firstField == RD->field_end())
3388 InstanceStart = InstanceSize;
3389 else
3390 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3391 getLLVMFieldNo(*firstField));
3392 }
3393 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003394 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003395 InstanceStart,
3396 InstanceSize,
3397 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003398
3399 TClassName = ObjCClassName + ClassName;
3400 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003401}
3402
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003403/// GenerateCategory - Build metadata for a category implementation.
3404/// struct _category_t {
3405/// const char * const name;
3406/// struct _class_t *const cls;
3407/// const struct _method_list_t * const instance_methods;
3408/// const struct _method_list_t * const class_methods;
3409/// const struct _protocol_list_t * const protocols;
3410/// const struct _prop_list_t * const properties;
3411/// }
3412///
3413void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3414{
3415 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003416 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3417 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003418 "_$_" + OCD->getNameAsString());
3419 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3420
3421 std::vector<llvm::Constant*> Values(6);
3422 Values[0] = GetClassName(OCD->getIdentifier());
3423 // meta-class entry symbol
3424 llvm::GlobalVariable *ClassGV =
3425 CGM.getModule().getGlobalVariable(ExtClassName);
3426 if (!ClassGV)
3427 ClassGV =
3428 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3429 llvm::GlobalValue::ExternalLinkage,
3430 0,
3431 ExtClassName,
3432 &CGM.getModule());
3433 UsedGlobals.push_back(ClassGV);
3434 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003435 std::vector<llvm::Constant*> Methods;
3436 std::string MethodListName(Prefix);
3437 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3438 "_$_" + OCD->getNameAsString();
3439
3440 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3441 e = OCD->instmeth_end(); i != e; ++i) {
3442 // Instance methods should always be defined.
3443 Methods.push_back(GetMethodConstant(*i));
3444 }
3445
3446 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003447 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003448 Methods);
3449
3450 MethodListName = Prefix;
3451 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3452 OCD->getNameAsString();
3453 Methods.clear();
3454 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3455 e = OCD->classmeth_end(); i != e; ++i) {
3456 // Class methods should always be defined.
3457 Methods.push_back(GetMethodConstant(*i));
3458 }
3459
3460 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003461 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003462 Methods);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003463 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3464 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3465
3466 llvm::Constant *Init =
3467 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3468 Values);
3469 llvm::GlobalVariable *GCATV
3470 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3471 false,
3472 llvm::GlobalValue::InternalLinkage,
3473 Init,
3474 ExtCatName,
3475 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003476 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003477 UsedGlobals.push_back(GCATV);
3478 DefinedCategories.push_back(GCATV);
3479}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003480
3481/// GetMethodConstant - Return a struct objc_method constant for the
3482/// given method if it has been defined. The result is null if the
3483/// method has not been defined. The return value has type MethodPtrTy.
3484llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3485 const ObjCMethodDecl *MD) {
3486 // FIXME: Use DenseMap::lookup
3487 llvm::Function *Fn = MethodDefinitions[MD];
3488 if (!Fn)
3489 return 0;
3490
3491 std::vector<llvm::Constant*> Method(3);
3492 Method[0] =
3493 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3494 ObjCTypes.SelectorPtrTy);
3495 Method[1] = GetMethodVarType(MD);
3496 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3497 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3498}
3499
3500/// EmitMethodList - Build meta-data for method declarations
3501/// struct _method_list_t {
3502/// uint32_t entsize; // sizeof(struct _objc_method)
3503/// uint32_t method_count;
3504/// struct _objc_method method_list[method_count];
3505/// }
3506///
3507llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3508 const std::string &Name,
3509 const char *Section,
3510 const ConstantVector &Methods) {
3511 // Return null for empty list.
3512 if (Methods.empty())
3513 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3514
3515 std::vector<llvm::Constant*> Values(3);
3516 // sizeof(struct _objc_method)
3517 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3518 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3519 // method_count
3520 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3521 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3522 Methods.size());
3523 Values[2] = llvm::ConstantArray::get(AT, Methods);
3524 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3525
3526 llvm::GlobalVariable *GV =
3527 new llvm::GlobalVariable(Init->getType(), false,
3528 llvm::GlobalValue::InternalLinkage,
3529 Init,
3530 Name,
3531 &CGM.getModule());
3532 GV->setSection(Section);
3533 UsedGlobals.push_back(GV);
3534 return llvm::ConstantExpr::getBitCast(GV,
3535 ObjCTypes.MethodListnfABIPtrTy);
3536}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003537
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003538llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3539 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003540 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003541 unsigned long int Offset) {
3542
3543 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003544 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003545 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3546
3547 llvm::GlobalVariable *IvarOffsetGV =
3548 CGM.getModule().getGlobalVariable(ExternalName);
3549 if (IvarOffsetGV) {
3550 // ivar offset symbol already built due to user code referencing it.
3551 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003552 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003553 return IvarOffsetGV;
3554 }
3555
3556 IvarOffsetGV =
3557 new llvm::GlobalVariable(Init->getType(),
3558 false,
3559 llvm::GlobalValue::ExternalLinkage,
3560 Init,
3561 ExternalName,
3562 &CGM.getModule());
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003563 // @private and @package have hidden visibility.
3564 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3565 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3566 if (!globalVisibility)
3567 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003568 IvarOffsetGV->setSection("__DATA, __objc_const");
3569 UsedGlobals.push_back(IvarOffsetGV);
3570
3571 return llvm::ConstantExpr::getBitCast(
3572 IvarOffsetGV,
3573 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3574}
3575
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003576/// EmitIvarList - Emit the ivar list for the given
3577/// implementation. If ForClass is true the list of class ivars
3578/// (i.e. metaclass ivars) is emitted, otherwise the list of
3579/// interface ivars will be emitted. The return value has type
3580/// IvarListnfABIPtrTy.
3581/// struct _ivar_t {
3582/// unsigned long int *offset; // pointer to ivar offset location
3583/// char *name;
3584/// char *type;
3585/// uint32_t alignment;
3586/// uint32_t size;
3587/// }
3588/// struct _ivar_list_t {
3589/// uint32 entsize; // sizeof(struct _ivar_t)
3590/// uint32 count;
3591/// struct _iver_t list[count];
3592/// }
3593///
3594llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3595 const ObjCImplementationDecl *ID) {
3596
3597 std::vector<llvm::Constant*> Ivars, Ivar(5);
3598
3599 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3600 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3601
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003602 // FIXME. Consolidate this with similar code in GenerateClass.
3603 const llvm::Type *InterfaceTy =
3604 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3605 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003606 const llvm::StructLayout *Layout =
3607 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003608
3609 RecordDecl::field_iterator i,p;
3610 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003611 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3612
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003613 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003614 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003615 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3616 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003617 const ObjCIvarDecl *ivarDecl = *I++;
3618 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003619 if (Field->getIdentifier())
3620 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3621 else
3622 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3623 std::string TypeStr;
3624 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3625 Ivar[2] = GetMethodVarType(TypeStr);
3626 const llvm::Type *FieldTy =
3627 CGM.getTypes().ConvertTypeForMem(Field->getType());
3628 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3629 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3630 Field->getType().getTypePtr()) >> 3;
3631 Align = llvm::Log2_32(Align);
3632 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003633 // NOTE. Size of a bitfield does not match gcc's, because of the way
3634 // bitfields are treated special in each. But I am told that 'size'
3635 // for bitfield ivars is ignored by the runtime so it does not matter.
3636 // (even if it matters, some day, there is enough info. to get the bitfield
3637 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003638 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3639 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3640 }
3641 // Return null for empty list.
3642 if (Ivars.empty())
3643 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3644 std::vector<llvm::Constant*> Values(3);
3645 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3646 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3647 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3648 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3649 Ivars.size());
3650 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3651 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3652 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3653 llvm::GlobalVariable *GV =
3654 new llvm::GlobalVariable(Init->getType(), false,
3655 llvm::GlobalValue::InternalLinkage,
3656 Init,
3657 Prefix + OID->getNameAsString(),
3658 &CGM.getModule());
3659
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003660 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003661
3662 UsedGlobals.push_back(GV);
3663 return llvm::ConstantExpr::getBitCast(GV,
3664 ObjCTypes.IvarListnfABIPtrTy);
3665}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003666/* *** */
3667
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003668CodeGen::CGObjCRuntime *
3669CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003670 return new CGObjCMac(CGM);
3671}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003672
3673CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003674CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003675 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003676}