blob: 9d106816d02d2997be1f800b71c1d8c31d83bcc0 [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
237 // IvarnfABITy - LLVM for struct _ivar_t
238 const llvm::StructType *IvarnfABITy;
239
240 // IvarListnfABITy - LLVM for struct _ivar_list_t
241 const llvm::StructType *IvarListnfABITy;
242
243 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
244 const llvm::Type *IvarListnfABIPtrTy;
245
246 // ClassRonfABITy - LLVM for struct _class_ro_t
247 const llvm::StructType *ClassRonfABITy;
248
249 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
250 const llvm::Type *ImpnfABITy;
251
252 // CategorynfABITy - LLVM for struct _category_t
253 const llvm::StructType *CategorynfABITy;
254
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000255 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
256 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000257};
258
259class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
260protected:
261 CodeGen::CodeGenModule &CGM;
262 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000263 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000264
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000265 /// LazySymbols - Symbols to generate a lazy reference for. See
266 /// DefinedSymbols and FinishModule().
267 std::set<IdentifierInfo*> LazySymbols;
268
269 /// DefinedSymbols - External symbols which are defined by this
270 /// module. The symbols in this list and LazySymbols are used to add
271 /// special linker symbols which ensure that Objective-C modules are
272 /// linked properly.
273 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000274
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000275 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000276 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000278 /// MethodVarNames - uniqued method variable names.
279 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000281 /// MethodVarTypes - uniqued method type signatures. We have to use
282 /// a StringMap here because have no other unique reference.
283 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000284
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000285 /// MethodDefinitions - map of methods which have been defined in
286 /// this translation unit.
287 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000288
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000289 /// PropertyNames - uniqued method variable names.
290 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000292 /// ClassReferences - uniqued class references.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000295 /// SelectorReferences - uniqued selector references.
296 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000298 /// Protocols - Protocols for which an objc_protocol structure has
299 /// been emitted. Forward declarations are handled by creating an
300 /// empty structure whose initializer is filled in when/if defined.
301 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000302
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000303 /// DefinedProtocols - Protocols which have actually been
304 /// defined. We should not need this, see FIXME in GenerateProtocol.
305 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000306
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000307 /// DefinedClasses - List of defined classes.
308 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedCategories - List of defined categories.
311 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000313 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000314 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000315 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000316
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000317 /// GetNameForMethod - Return a name for the given method.
318 /// \param[out] NameOut - The return value.
319 void GetNameForMethod(const ObjCMethodDecl *OMD,
320 const ObjCContainerDecl *CD,
321 std::string &NameOut);
322
323 /// GetMethodVarName - Return a unique constant for the given
324 /// selector's name. The return value has type char *.
325 llvm::Constant *GetMethodVarName(Selector Sel);
326 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
327 llvm::Constant *GetMethodVarName(const std::string &Name);
328
329 /// GetMethodVarType - Return a unique constant for the given
330 /// selector's name. The return value has type char *.
331
332 // FIXME: This is a horrible name.
333 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
334 llvm::Constant *GetMethodVarType(const std::string &Name);
335
336 /// GetPropertyName - Return a unique constant for the given
337 /// name. The return value has type char *.
338 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
339
340 // FIXME: This can be dropped once string functions are unified.
341 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
342 const Decl *Container);
343
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000344public:
345 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
346 { }
347};
348
349class CGObjCMac : public CGObjCCommonMac {
350private:
351 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000352 /// EmitImageInfo - Emit the image info marker used to encode some module
353 /// level information.
354 void EmitImageInfo();
355
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000356 /// EmitModuleInfo - Another marker encoding module level
357 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000358 void EmitModuleInfo();
359
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000360 /// EmitModuleSymols - Emit module symbols, the list of defined
361 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000362 llvm::Constant *EmitModuleSymbols();
363
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000364 /// FinishModule - Write out global data structures at the end of
365 /// processing a translation unit.
366 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000367
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000368 /// EmitClassExtension - Generate the class extension structure used
369 /// to store the weak ivar layout and properties. The return value
370 /// has type ClassExtensionPtrTy.
371 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
372
373 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
374 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000375 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000376 const ObjCInterfaceDecl *ID);
377
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000378 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000379 QualType ResultType,
380 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000381 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000382 QualType Arg0Ty,
383 bool IsSuper,
384 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000385
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000386 /// EmitIvarList - Emit the ivar list for the given
387 /// implementation. If ForClass is true the list of class ivars
388 /// (i.e. metaclass ivars) is emitted, otherwise the list of
389 /// interface ivars will be emitted. The return value has type
390 /// IvarListPtrTy.
391 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
392 bool ForClass,
393 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000394
395 /// EmitMetaClass - Emit a forward reference to the class structure
396 /// for the metaclass of the given interface. The return value has
397 /// type ClassPtrTy.
398 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
399
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000400 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000401 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000402 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
403 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000404 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000405 const ConstantVector &Methods);
406
407 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
408
409 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000410
411 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000412 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000413 llvm::Constant *EmitMethodList(const std::string &Name,
414 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000415 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416
417 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000418 /// method declarations.
419 /// - TypeName: The name for the type containing the methods.
420 /// - IsProtocol: True iff these methods are for a protocol.
421 /// - ClassMethds: True iff these are class methods.
422 /// - Required: When true, only "required" methods are
423 /// listed. Similarly, when false only "optional" methods are
424 /// listed. For classes this should always be true.
425 /// - begin, end: The method list to output.
426 ///
427 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000428 llvm::Constant *EmitMethodDescList(const std::string &Name,
429 const char *Section,
430 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000431
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000432 /// EmitPropertyList - Emit the given property list. The return
433 /// value has type PropertyListPtrTy.
434 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000435 const Decl *Container,
436 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000437
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000438 /// GetOrEmitProtocol - Get the protocol object for the given
439 /// declaration, emitting it if necessary. The return value has type
440 /// ProtocolPtrTy.
441 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
442
443 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
444 /// object for the given declaration, emitting it if needed. These
445 /// forward references will be filled in with empty bodies if no
446 /// definition is seen. The return value has type ProtocolPtrTy.
447 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
448
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000449 /// EmitProtocolExtension - Generate the protocol extension
450 /// structure used to store optional instance and class methods, and
451 /// protocol properties. The return value has type
452 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000453 llvm::Constant *
454 EmitProtocolExtension(const ObjCProtocolDecl *PD,
455 const ConstantVector &OptInstanceMethods,
456 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000457
458 /// EmitProtocolList - Generate the list of referenced
459 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +0000460 llvm::Constant *EmitProtocolList(const std::string &Name,
461 ObjCProtocolDecl::protocol_iterator begin,
462 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000463
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000464 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
465 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000466 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000467
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000468 /// GetProtocolRef - Return a reference to the internal protocol
469 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000470 /// defined. The return value has type ProtocolPtrTy.
471 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000472
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000473 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000474 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000475 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000476
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000477public:
478 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000479 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000480
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000481 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000482 QualType ResultType,
483 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000484 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000485 bool IsClassMessage,
486 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000487
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000488 virtual CodeGen::RValue
489 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000490 QualType ResultType,
491 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000492 const ObjCInterfaceDecl *Class,
493 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000494 bool IsClassMessage,
495 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000496
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000497 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000498 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000499
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000500 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000501
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000502 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
503 const ObjCContainerDecl *CD=0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000504
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000505 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000506
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000507 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000508
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000509 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000510 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000511
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000512 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000513
514 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000515 virtual llvm::Function *GetPropertyGetFunction();
516 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000517 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000518
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000519 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
520 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000521 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
522 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000523 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000524 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000525 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
526 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000527 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
528 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000529 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
530 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000531 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
532 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000533};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000534
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000535class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000536private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000537 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000538public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000539 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000540};
541
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000542} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000543
544/* *** Helper Functions *** */
545
546/// getConstantGEP() - Help routine to construct simple GEPs.
547static llvm::Constant *getConstantGEP(llvm::Constant *C,
548 unsigned idx0,
549 unsigned idx1) {
550 llvm::Value *Idxs[] = {
551 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
552 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
553 };
554 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
555}
556
557/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000559CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
560 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000561{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000562 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000563 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000564}
565
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000566/// GetClass - Return a reference to the class for the given interface
567/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000568llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000569 const ObjCInterfaceDecl *ID) {
570 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000571}
572
573/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000574llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000575 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000576}
577
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000578/// Generate a constant CFString object.
579/*
580 struct __builtin_CFString {
581 const int *isa; // point to __CFConstantStringClassReference
582 int flags;
583 const char *str;
584 long length;
585 };
586*/
587
588llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000589 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000590}
591
592/// Generates a message send where the super is the receiver. This is
593/// a message send to self with special delivery semantics indicating
594/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000595CodeGen::RValue
596CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000597 QualType ResultType,
598 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000599 const ObjCInterfaceDecl *Class,
600 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000601 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000602 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000603 // Create and init a super structure; this is a (receiver, class)
604 // pair we will pass to objc_msgSendSuper.
605 llvm::Value *ObjCSuper =
606 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
607 llvm::Value *ReceiverAsObject =
608 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
609 CGF.Builder.CreateStore(ReceiverAsObject,
610 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000611
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000612 // If this is a class message the metaclass is passed as the target.
613 llvm::Value *Target;
614 if (IsClassMessage) {
615 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
616 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
617 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
618 Target = Super;
619 } else {
620 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
621 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000622 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
623 // and ObjCTypes types.
624 const llvm::Type *ClassTy =
625 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000626 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000627 CGF.Builder.CreateStore(Target,
628 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
629
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000630 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000631 ObjCSuper, ObjCTypes.SuperPtrCTy,
632 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000633}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000634
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000635/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000636CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000637 QualType ResultType,
638 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000639 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000640 bool IsClassMessage,
641 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000642 llvm::Value *Arg0 =
643 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000644 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000645 Arg0, CGF.getContext().getObjCIdType(),
646 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000647}
648
649CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000650 QualType ResultType,
651 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000652 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000653 QualType Arg0Ty,
654 bool IsSuper,
655 const CallArgList &CallArgs) {
656 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000657 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
658 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
659 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000660 CGF.getContext().getObjCSelType()));
661 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000662
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000663 const llvm::FunctionType *FTy =
664 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
665 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000666
667 llvm::Constant *Fn;
668 if (CGM.ReturnTypeUsesSret(ResultType)) {
669 Fn = ObjCTypes.getSendStretFn(IsSuper);
670 } else if (ResultType->isFloatingType()) {
671 // FIXME: Sadly, this is wrong. This actually depends on the
672 // architecture. This happens to be right for x86-32 though.
673 Fn = ObjCTypes.getSendFpretFn(IsSuper);
674 } else {
675 Fn = ObjCTypes.getSendFn(IsSuper);
676 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000677 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000678 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000679}
680
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000681llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000682 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000683 // FIXME: I don't understand why gcc generates this, or where it is
684 // resolved. Investigate. Its also wasteful to look this up over and
685 // over.
686 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
687
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000688 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
689 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000690}
691
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000692void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
693 // FIXME: We shouldn't need this, the protocol decl should contain
694 // enough information to tell us whether this was a declaration or a
695 // definition.
696 DefinedProtocols.insert(PD->getIdentifier());
697
698 // If we have generated a forward reference to this protocol, emit
699 // it now. Otherwise do nothing, the protocol objects are lazily
700 // emitted.
701 if (Protocols.count(PD->getIdentifier()))
702 GetOrEmitProtocol(PD);
703}
704
705llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
706 if (DefinedProtocols.count(PD->getIdentifier()))
707 return GetOrEmitProtocol(PD);
708 return GetOrEmitProtocolRef(PD);
709}
710
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000711/*
712 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
713 struct _objc_protocol {
714 struct _objc_protocol_extension *isa;
715 char *protocol_name;
716 struct _objc_protocol_list *protocol_list;
717 struct _objc__method_prototype_list *instance_methods;
718 struct _objc__method_prototype_list *class_methods
719 };
720
721 See EmitProtocolExtension().
722*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000723llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
724 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
725
726 // Early exit if a defining object has already been generated.
727 if (Entry && Entry->hasInitializer())
728 return Entry;
729
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000730 // FIXME: I don't understand why gcc generates this, or where it is
731 // resolved. Investigate. Its also wasteful to look this up over and
732 // over.
733 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
734
Chris Lattner8ec03f52008-11-24 03:54:41 +0000735 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000736
737 // Construct method lists.
738 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
739 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
740 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
741 e = PD->instmeth_end(); i != e; ++i) {
742 ObjCMethodDecl *MD = *i;
743 llvm::Constant *C = GetMethodDescriptionConstant(MD);
744 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
745 OptInstanceMethods.push_back(C);
746 } else {
747 InstanceMethods.push_back(C);
748 }
749 }
750
751 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
752 e = PD->classmeth_end(); i != e; ++i) {
753 ObjCMethodDecl *MD = *i;
754 llvm::Constant *C = GetMethodDescriptionConstant(MD);
755 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
756 OptClassMethods.push_back(C);
757 } else {
758 ClassMethods.push_back(C);
759 }
760 }
761
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000762 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000763 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000764 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000765 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000766 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000767 PD->protocol_begin(),
768 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000769 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000770 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
771 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000772 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
773 InstanceMethods);
774 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000775 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
776 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000777 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
778 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000779 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
780 Values);
781
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000782 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000783 // Already created, fix the linkage and update the initializer.
784 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000785 Entry->setInitializer(Init);
786 } else {
787 Entry =
788 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
789 llvm::GlobalValue::InternalLinkage,
790 Init,
791 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
792 &CGM.getModule());
793 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
794 UsedGlobals.push_back(Entry);
795 // FIXME: Is this necessary? Why only for protocol?
796 Entry->setAlignment(4);
797 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000798
799 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000800}
801
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000802llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000803 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
804
805 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000806 // We use the initializer as a marker of whether this is a forward
807 // reference or not. At module finalization we add the empty
808 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000809 Entry =
810 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000811 llvm::GlobalValue::ExternalLinkage,
812 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000813 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000814 &CGM.getModule());
815 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
816 UsedGlobals.push_back(Entry);
817 // FIXME: Is this necessary? Why only for protocol?
818 Entry->setAlignment(4);
819 }
820
821 return Entry;
822}
823
824/*
825 struct _objc_protocol_extension {
826 uint32_t size;
827 struct objc_method_description_list *optional_instance_methods;
828 struct objc_method_description_list *optional_class_methods;
829 struct objc_property_list *instance_properties;
830 };
831*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000832llvm::Constant *
833CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
834 const ConstantVector &OptInstanceMethods,
835 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000836 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000837 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000838 std::vector<llvm::Constant*> Values(4);
839 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000840 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000841 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
842 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000843 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
844 OptInstanceMethods);
845 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000846 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
847 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000848 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
849 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000850 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
851 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000852 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000853
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000854 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000855 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
856 Values[3]->isNullValue())
857 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
858
859 llvm::Constant *Init =
860 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
861 llvm::GlobalVariable *GV =
862 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
863 llvm::GlobalValue::InternalLinkage,
864 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000865 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000866 &CGM.getModule());
867 // No special section, but goes in llvm.used
868 UsedGlobals.push_back(GV);
869
870 return GV;
871}
872
873/*
874 struct objc_protocol_list {
875 struct objc_protocol_list *next;
876 long count;
877 Protocol *list[];
878 };
879*/
Daniel Dunbardbc93372008-08-21 21:57:41 +0000880llvm::Constant *
881CGObjCMac::EmitProtocolList(const std::string &Name,
882 ObjCProtocolDecl::protocol_iterator begin,
883 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000884 std::vector<llvm::Constant*> ProtocolRefs;
885
Daniel Dunbardbc93372008-08-21 21:57:41 +0000886 for (; begin != end; ++begin)
887 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000888
889 // Just return null for empty protocol lists
890 if (ProtocolRefs.empty())
891 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
892
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000893 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000894 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
895
896 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000897 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000898 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
899 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
900 Values[2] =
901 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
902 ProtocolRefs.size()),
903 ProtocolRefs);
904
905 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
906 llvm::GlobalVariable *GV =
907 new llvm::GlobalVariable(Init->getType(), false,
908 llvm::GlobalValue::InternalLinkage,
909 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +0000910 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000911 &CGM.getModule());
912 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
913 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
914}
915
916/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000917 struct _objc_property {
918 const char * const name;
919 const char * const attributes;
920 };
921
922 struct _objc_property_list {
923 uint32_t entsize; // sizeof (struct _objc_property)
924 uint32_t prop_count;
925 struct _objc_property[prop_count];
926 };
927*/
928llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000929 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +0000930 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000931 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +0000932 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
933 E = OCD->prop_end(); I != E; ++I) {
934 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000935 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000936 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000937 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
938 Prop));
939 }
940
941 // Return null for empty list.
942 if (Properties.empty())
943 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
944
945 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000946 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000947 std::vector<llvm::Constant*> Values(3);
948 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
949 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
950 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
951 Properties.size());
952 Values[2] = llvm::ConstantArray::get(AT, Properties);
953 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
954
955 llvm::GlobalVariable *GV =
956 new llvm::GlobalVariable(Init->getType(), false,
957 llvm::GlobalValue::InternalLinkage,
958 Init,
959 Name,
960 &CGM.getModule());
961 // No special section on property lists?
962 UsedGlobals.push_back(GV);
963 return llvm::ConstantExpr::getBitCast(GV,
964 ObjCTypes.PropertyListPtrTy);
965
966}
967
968/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969 struct objc_method_description_list {
970 int count;
971 struct objc_method_description list[];
972 };
973*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974llvm::Constant *
975CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
976 std::vector<llvm::Constant*> Desc(2);
977 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
978 ObjCTypes.SelectorPtrTy);
979 Desc[1] = GetMethodVarType(MD);
980 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
981 Desc);
982}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000984llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
985 const char *Section,
986 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987 // Return null for empty list.
988 if (Methods.empty())
989 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
990
991 std::vector<llvm::Constant*> Values(2);
992 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
993 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
994 Methods.size());
995 Values[1] = llvm::ConstantArray::get(AT, Methods);
996 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
997
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998 llvm::GlobalVariable *GV =
999 new llvm::GlobalVariable(Init->getType(), false,
1000 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001001 Init, Name, &CGM.getModule());
1002 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001003 UsedGlobals.push_back(GV);
1004 return llvm::ConstantExpr::getBitCast(GV,
1005 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001006}
1007
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001008/*
1009 struct _objc_category {
1010 char *category_name;
1011 char *class_name;
1012 struct _objc_method_list *instance_methods;
1013 struct _objc_method_list *class_methods;
1014 struct _objc_protocol_list *protocols;
1015 uint32_t size; // <rdar://4585769>
1016 struct _objc_property_list *instance_properties;
1017 };
1018 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001019void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001020 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001021
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001022 // FIXME: This is poor design, the OCD should have a pointer to the
1023 // category decl. Additionally, note that Category can be null for
1024 // the @implementation w/o an @interface case. Sema should just
1025 // create one for us as it does for @implementation so everyone else
1026 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001027 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001028 const ObjCCategoryDecl *Category =
1029 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001030 std::string ExtName(Interface->getNameAsString() + "_" +
1031 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001032
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001033 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1034 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1035 e = OCD->instmeth_end(); i != e; ++i) {
1036 // Instance methods should always be defined.
1037 InstanceMethods.push_back(GetMethodConstant(*i));
1038 }
1039 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1040 e = OCD->classmeth_end(); i != e; ++i) {
1041 // Class methods should always be defined.
1042 ClassMethods.push_back(GetMethodConstant(*i));
1043 }
1044
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001045 std::vector<llvm::Constant*> Values(7);
1046 Values[0] = GetClassName(OCD->getIdentifier());
1047 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001048 Values[2] =
1049 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1050 ExtName,
1051 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001052 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001053 Values[3] =
1054 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1055 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001056 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001057 if (Category) {
1058 Values[4] =
1059 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1060 Category->protocol_begin(),
1061 Category->protocol_end());
1062 } else {
1063 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1064 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001065 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001066
1067 // If there is no category @interface then there can be no properties.
1068 if (Category) {
1069 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001070 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001071 } else {
1072 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1073 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001074
1075 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1076 Values);
1077
1078 llvm::GlobalVariable *GV =
1079 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1080 llvm::GlobalValue::InternalLinkage,
1081 Init,
1082 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1083 &CGM.getModule());
1084 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1085 UsedGlobals.push_back(GV);
1086 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001087}
1088
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001089// FIXME: Get from somewhere?
1090enum ClassFlags {
1091 eClassFlags_Factory = 0x00001,
1092 eClassFlags_Meta = 0x00002,
1093 // <rdr://5142207>
1094 eClassFlags_HasCXXStructors = 0x02000,
1095 eClassFlags_Hidden = 0x20000,
1096 eClassFlags_ABI2_Hidden = 0x00010,
1097 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1098};
1099
1100// <rdr://5142207&4705298&4843145>
1101static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1102 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1103 // FIXME: Support -fvisibility
1104 switch (attr->getVisibility()) {
1105 default:
1106 assert(0 && "Unknown visibility");
1107 return false;
1108 case VisibilityAttr::DefaultVisibility:
1109 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1110 return false;
1111 case VisibilityAttr::HiddenVisibility:
1112 return true;
1113 }
1114 } else {
1115 return false; // FIXME: Support -fvisibility
1116 }
1117}
1118
1119/*
1120 struct _objc_class {
1121 Class isa;
1122 Class super_class;
1123 const char *name;
1124 long version;
1125 long info;
1126 long instance_size;
1127 struct _objc_ivar_list *ivars;
1128 struct _objc_method_list *methods;
1129 struct _objc_cache *cache;
1130 struct _objc_protocol_list *protocols;
1131 // Objective-C 1.0 extensions (<rdr://4585769>)
1132 const char *ivar_layout;
1133 struct _objc_class_ext *ext;
1134 };
1135
1136 See EmitClassExtension();
1137 */
1138void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001139 DefinedSymbols.insert(ID->getIdentifier());
1140
Chris Lattner8ec03f52008-11-24 03:54:41 +00001141 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001142 // FIXME: Gross
1143 ObjCInterfaceDecl *Interface =
1144 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001145 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001146 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001147 Interface->protocol_begin(),
1148 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001149 const llvm::Type *InterfaceTy =
1150 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1151 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001152 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001153
1154 // FIXME: Set CXX-structors flag.
1155 if (IsClassHidden(ID->getClassInterface()))
1156 Flags |= eClassFlags_Hidden;
1157
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001158 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1159 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1160 e = ID->instmeth_end(); i != e; ++i) {
1161 // Instance methods should always be defined.
1162 InstanceMethods.push_back(GetMethodConstant(*i));
1163 }
1164 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1165 e = ID->classmeth_end(); i != e; ++i) {
1166 // Class methods should always be defined.
1167 ClassMethods.push_back(GetMethodConstant(*i));
1168 }
1169
1170 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1171 e = ID->propimpl_end(); i != e; ++i) {
1172 ObjCPropertyImplDecl *PID = *i;
1173
1174 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1175 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1176
1177 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1178 if (llvm::Constant *C = GetMethodConstant(MD))
1179 InstanceMethods.push_back(C);
1180 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1181 if (llvm::Constant *C = GetMethodConstant(MD))
1182 InstanceMethods.push_back(C);
1183 }
1184 }
1185
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001186 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001187 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001188 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001189 // Record a reference to the super class.
1190 LazySymbols.insert(Super->getIdentifier());
1191
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001192 Values[ 1] =
1193 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1194 ObjCTypes.ClassPtrTy);
1195 } else {
1196 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1197 }
1198 Values[ 2] = GetClassName(ID->getIdentifier());
1199 // Version is always 0.
1200 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1201 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1202 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1203 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001204 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001205 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001206 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001207 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001208 // cache is always NULL.
1209 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1210 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001211 // FIXME: Set ivar_layout
1212 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001213 Values[11] = EmitClassExtension(ID);
1214 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1215 Values);
1216
1217 llvm::GlobalVariable *GV =
1218 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1219 llvm::GlobalValue::InternalLinkage,
1220 Init,
1221 std::string("\01L_OBJC_CLASS_")+ClassName,
1222 &CGM.getModule());
1223 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1224 UsedGlobals.push_back(GV);
1225 // FIXME: Why?
1226 GV->setAlignment(32);
1227 DefinedClasses.push_back(GV);
1228}
1229
1230llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1231 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001232 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001233 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001234 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001235 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001236
1237 if (IsClassHidden(ID->getClassInterface()))
1238 Flags |= eClassFlags_Hidden;
1239
1240 std::vector<llvm::Constant*> Values(12);
1241 // The isa for the metaclass is the root of the hierarchy.
1242 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1243 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1244 Root = Super;
1245 Values[ 0] =
1246 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1247 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001248 // The super class for the metaclass is emitted as the name of the
1249 // super class. The runtime fixes this up to point to the
1250 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001251 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1252 Values[ 1] =
1253 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1254 ObjCTypes.ClassPtrTy);
1255 } else {
1256 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1257 }
1258 Values[ 2] = GetClassName(ID->getIdentifier());
1259 // Version is always 0.
1260 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1261 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1262 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1263 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001264 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001265 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001266 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001267 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001268 // cache is always NULL.
1269 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1270 Values[ 9] = Protocols;
1271 // ivar_layout for metaclass is always NULL.
1272 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1273 // The class extension is always unused for metaclasses.
1274 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1275 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1276 Values);
1277
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001278 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001279 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001280
1281 // Check for a forward reference.
1282 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1283 if (GV) {
1284 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1285 "Forward metaclass reference has incorrect type.");
1286 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1287 GV->setInitializer(Init);
1288 } else {
1289 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1290 llvm::GlobalValue::InternalLinkage,
1291 Init, Name,
1292 &CGM.getModule());
1293 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001294 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1295 UsedGlobals.push_back(GV);
1296 // FIXME: Why?
1297 GV->setAlignment(32);
1298
1299 return GV;
1300}
1301
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001302llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001303 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001304
1305 // FIXME: Should we look these up somewhere other than the
1306 // module. Its a bit silly since we only generate these while
1307 // processing an implementation, so exactly one pointer would work
1308 // if know when we entered/exitted an implementation block.
1309
1310 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001311 // Previously, metaclass with internal linkage may have been defined.
1312 // pass 'true' as 2nd argument so it is returned.
1313 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001314 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1315 "Forward metaclass reference has incorrect type.");
1316 return GV;
1317 } else {
1318 // Generate as an external reference to keep a consistent
1319 // module. This will be patched up when we emit the metaclass.
1320 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1321 llvm::GlobalValue::ExternalLinkage,
1322 0,
1323 Name,
1324 &CGM.getModule());
1325 }
1326}
1327
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001328/*
1329 struct objc_class_ext {
1330 uint32_t size;
1331 const char *weak_ivar_layout;
1332 struct _objc_property_list *properties;
1333 };
1334*/
1335llvm::Constant *
1336CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1337 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001338 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001339
1340 std::vector<llvm::Constant*> Values(3);
1341 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001342 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001343 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001344 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001345 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001346
1347 // Return null if no extension bits are used.
1348 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1349 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1350
1351 llvm::Constant *Init =
1352 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1353 llvm::GlobalVariable *GV =
1354 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1355 llvm::GlobalValue::InternalLinkage,
1356 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001357 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001358 &CGM.getModule());
1359 // No special section, but goes in llvm.used
1360 UsedGlobals.push_back(GV);
1361
1362 return GV;
1363}
1364
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001365/// countInheritedIvars - count number of ivars in class and its super class(s)
1366///
1367static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1368 int count = 0;
1369 if (!OI)
1370 return 0;
1371 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1372 if (SuperClass)
1373 count += countInheritedIvars(SuperClass);
1374 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1375 E = OI->ivar_end(); I != E; ++I)
1376 ++count;
1377 return count;
1378}
1379
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001380/*
1381 struct objc_ivar {
1382 char *ivar_name;
1383 char *ivar_type;
1384 int ivar_offset;
1385 };
1386
1387 struct objc_ivar_list {
1388 int ivar_count;
1389 struct objc_ivar list[count];
1390 };
1391 */
1392llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1393 bool ForClass,
1394 const llvm::Type *InterfaceTy) {
1395 std::vector<llvm::Constant*> Ivars, Ivar(3);
1396
1397 // When emitting the root class GCC emits ivar entries for the
1398 // actual class structure. It is not clear if we need to follow this
1399 // behavior; for now lets try and get away with not doing it. If so,
1400 // the cleanest solution would be to make up an ObjCInterfaceDecl
1401 // for the class.
1402 if (ForClass)
1403 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1404
1405 const llvm::StructLayout *Layout =
1406 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001407 ObjCInterfaceDecl *OID =
1408 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1409 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1410 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1411 RecordDecl::field_iterator ifield = RD->field_begin();
1412 while (countSuperClassIvars-- > 0)
1413 ++ifield;
1414 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1415 FieldDecl *Field = *ifield;
1416 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1417 getLLVMFieldNo(Field));
1418 if (Field->getIdentifier())
1419 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1420 else
1421 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001422 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001423 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001424 Ivar[1] = GetMethodVarType(TypeStr);
1425 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001426 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001427 }
1428
1429 // Return null for empty list.
1430 if (Ivars.empty())
1431 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1432
1433 std::vector<llvm::Constant*> Values(2);
1434 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1435 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1436 Ivars.size());
1437 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1438 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1439
1440 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1441 "\01L_OBJC_INSTANCE_VARIABLES_");
1442 llvm::GlobalVariable *GV =
1443 new llvm::GlobalVariable(Init->getType(), false,
1444 llvm::GlobalValue::InternalLinkage,
1445 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001446 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001447 &CGM.getModule());
1448 if (ForClass) {
1449 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1450 // FIXME: Why is this only here?
1451 GV->setAlignment(32);
1452 } else {
1453 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1454 }
1455 UsedGlobals.push_back(GV);
1456 return llvm::ConstantExpr::getBitCast(GV,
1457 ObjCTypes.IvarListPtrTy);
1458}
1459
1460/*
1461 struct objc_method {
1462 SEL method_name;
1463 char *method_types;
1464 void *method;
1465 };
1466
1467 struct objc_method_list {
1468 struct objc_method_list *obsolete;
1469 int count;
1470 struct objc_method methods_list[count];
1471 };
1472*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001473
1474/// GetMethodConstant - Return a struct objc_method constant for the
1475/// given method if it has been defined. The result is null if the
1476/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001477llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001478 // FIXME: Use DenseMap::lookup
1479 llvm::Function *Fn = MethodDefinitions[MD];
1480 if (!Fn)
1481 return 0;
1482
1483 std::vector<llvm::Constant*> Method(3);
1484 Method[0] =
1485 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1486 ObjCTypes.SelectorPtrTy);
1487 Method[1] = GetMethodVarType(MD);
1488 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1489 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1490}
1491
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001492llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1493 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001494 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495 // Return null for empty list.
1496 if (Methods.empty())
1497 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1498
1499 std::vector<llvm::Constant*> Values(3);
1500 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1501 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1502 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1503 Methods.size());
1504 Values[2] = llvm::ConstantArray::get(AT, Methods);
1505 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1506
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001507 llvm::GlobalVariable *GV =
1508 new llvm::GlobalVariable(Init->getType(), false,
1509 llvm::GlobalValue::InternalLinkage,
1510 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001511 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001512 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001513 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001514 UsedGlobals.push_back(GV);
1515 return llvm::ConstantExpr::getBitCast(GV,
1516 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001517}
1518
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001519llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD,
1520 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001521 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001522 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001523
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001524 const llvm::FunctionType *MethodTy =
1525 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001526 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001527 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001528 llvm::GlobalValue::InternalLinkage,
1529 Name,
1530 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001531 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001532
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001533 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001534}
1535
1536llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001537 // Abuse this interface function as a place to finalize.
1538 FinishModule();
1539
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001540 return NULL;
1541}
1542
Daniel Dunbar49f66022008-09-24 03:38:44 +00001543llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1544 return ObjCTypes.GetPropertyFn;
1545}
1546
1547llvm::Function *CGObjCMac::GetPropertySetFunction() {
1548 return ObjCTypes.SetPropertyFn;
1549}
1550
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001551llvm::Function *CGObjCMac::EnumerationMutationFunction()
1552{
1553 return ObjCTypes.EnumerationMutationFn;
1554}
1555
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001556/*
1557
1558Objective-C setjmp-longjmp (sjlj) Exception Handling
1559--
1560
1561The basic framework for a @try-catch-finally is as follows:
1562{
1563 objc_exception_data d;
1564 id _rethrow = null;
1565
1566 objc_exception_try_enter(&d);
1567 if (!setjmp(d.jmp_buf)) {
1568 ... try body ...
1569 } else {
1570 // exception path
1571 id _caught = objc_exception_extract(&d);
1572
1573 // enter new try scope for handlers
1574 if (!setjmp(d.jmp_buf)) {
1575 ... match exception and execute catch blocks ...
1576
1577 // fell off end, rethrow.
1578 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001579 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001580 } else {
1581 // exception in catch block
1582 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001583 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001584 }
1585 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001586 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001587
1588finally:
1589 // match either the initial try_enter or the catch try_enter,
1590 // depending on the path followed.
1591 objc_exception_try_exit(&d);
1592finally_no_exit:
1593 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001594 ... dispatch to finally destination ...
1595
1596finally_rethrow:
1597 objc_exception_throw(_rethrow);
1598
1599finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001600}
1601
1602This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001603uses _rethrow to determine if objc_exception_try_exit should be called
1604and if the object should be rethrown. This breaks in the face of
1605throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001606
1607We specialize this framework for a few particular circumstances:
1608
1609 - If there are no catch blocks, then we avoid emitting the second
1610 exception handling context.
1611
1612 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1613 e)) we avoid emitting the code to rethrow an uncaught exception.
1614
1615 - FIXME: If there is no @finally block we can do a few more
1616 simplifications.
1617
1618Rethrows and Jumps-Through-Finally
1619--
1620
1621Support for implicit rethrows and jumping through the finally block is
1622handled by storing the current exception-handling context in
1623ObjCEHStack.
1624
Daniel Dunbar898d5082008-09-30 01:06:03 +00001625In order to implement proper @finally semantics, we support one basic
1626mechanism for jumping through the finally block to an arbitrary
1627destination. Constructs which generate exits from a @try or @catch
1628block use this mechanism to implement the proper semantics by chaining
1629jumps, as necessary.
1630
1631This mechanism works like the one used for indirect goto: we
1632arbitrarily assign an ID to each destination and store the ID for the
1633destination in a variable prior to entering the finally block. At the
1634end of the finally block we simply create a switch to the proper
1635destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001636
1637Code gen for @synchronized(expr) stmt;
1638Effectively generating code for:
1639objc_sync_enter(expr);
1640@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001641*/
1642
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001643void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1644 const Stmt &S) {
1645 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001646 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001647 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1648 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1649 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1650 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001651 llvm::Value *DestCode =
1652 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1653
1654 // Generate jump code. Done here so we can directly add things to
1655 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001656 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001657 llvm::SwitchInst *FinallySwitch =
1658 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1659 FinallyEnd, 10, FinallyJump);
1660
1661 // Push an EH context entry, used for handling rethrows and jumps
1662 // through finally.
1663 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1664 FinallySwitch, DestCode);
1665 CGF.ObjCEHStack.push_back(&EHEntry);
1666
1667 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001668 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1669 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001670 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1671 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001672 if (!isTry) {
1673 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001674 llvm::Value *Arg = CGF.EmitScalarExpr(
1675 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1676 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1677 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001678 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001679
1680 // Enter a new try block and call setjmp.
1681 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1682 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1683 "jmpbufarray");
1684 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1685 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1686 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001687
Daniel Dunbar55e87422008-11-11 02:29:29 +00001688 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1689 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001690 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001691 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001692
1693 // Emit the @try block.
1694 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001695 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1696 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001697 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001698
1699 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001700 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001701
1702 // Retrieve the exception object. We may emit multiple blocks but
1703 // nothing can cross this so the value is already in SSA form.
1704 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1705 ExceptionData,
1706 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001707 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001708 if (!isTry)
1709 {
1710 CGF.Builder.CreateStore(Caught, RethrowPtr);
1711 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1712 }
1713 else if (const ObjCAtCatchStmt* CatchStmt =
1714 cast<ObjCAtTryStmt>(S).getCatchStmts())
1715 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001716 // Enter a new exception try block (in case a @catch block throws
1717 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001718 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001719
Anders Carlsson80f25672008-09-09 17:59:25 +00001720 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1721 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001722 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001723
Daniel Dunbar55e87422008-11-11 02:29:29 +00001724 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1725 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001726 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001727
1728 CGF.EmitBlock(CatchBlock);
1729
Daniel Dunbar55e40722008-09-27 07:03:52 +00001730 // Handle catch list. As a special case we check if everything is
1731 // matched and avoid generating code for falling off the end if
1732 // so.
1733 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001734 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001735 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001736
Anders Carlssondde0a942008-09-11 09:15:33 +00001737 const DeclStmt *CatchParam =
1738 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001739 const VarDecl *VD = 0;
1740 const PointerType *PT = 0;
1741
Anders Carlsson80f25672008-09-09 17:59:25 +00001742 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001743 if (!CatchParam) {
1744 AllMatched = true;
1745 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001746 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001747 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001748
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001749 // catch(id e) always matches.
1750 // FIXME: For the time being we also match id<X>; this should
1751 // be rejected by Sema instead.
1752 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1753 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001754 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001755 }
1756
Daniel Dunbar55e40722008-09-27 07:03:52 +00001757 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001758 if (CatchParam) {
1759 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001760 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001761 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001762 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001763
Anders Carlssondde0a942008-09-11 09:15:33 +00001764 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001765 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001766 break;
1767 }
1768
Daniel Dunbar129271a2008-09-27 07:36:24 +00001769 assert(PT && "Unexpected non-pointer type in @catch");
1770 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001771 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001772 assert(ObjCType && "Catch parameter must have Objective-C type!");
1773
1774 // Check if the @catch block matches the exception object.
1775 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1776
Anders Carlsson80f25672008-09-09 17:59:25 +00001777 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1778 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001779
Daniel Dunbar55e87422008-11-11 02:29:29 +00001780 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001781
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001782 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001783 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001784
1785 // Emit the @catch block.
1786 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001787 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001788 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001789
1790 llvm::Value *Tmp =
1791 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1792 "tmp");
1793 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001794
1795 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001796 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001797
1798 CGF.EmitBlock(NextCatchBlock);
1799 }
1800
Daniel Dunbar55e40722008-09-27 07:03:52 +00001801 if (!AllMatched) {
1802 // None of the handlers caught the exception, so store it to be
1803 // rethrown at the end of the @finally block.
1804 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001805 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001806 }
1807
1808 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001809 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001810 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1811 ExceptionData),
1812 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001813 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001814 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001815 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001816 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001817 }
1818
Daniel Dunbar898d5082008-09-30 01:06:03 +00001819 // Pop the exception-handling stack entry. It is important to do
1820 // this now, because the code in the @finally block is not in this
1821 // context.
1822 CGF.ObjCEHStack.pop_back();
1823
Anders Carlsson80f25672008-09-09 17:59:25 +00001824 // Emit the @finally block.
1825 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001826 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001827
1828 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001829 if (isTry) {
1830 if (const ObjCAtFinallyStmt* FinallyStmt =
1831 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1832 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1833 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001834 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001835 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001836 // For @synchronized, call objc_sync_enter(sync.expr)
1837 llvm::Value *Arg = CGF.EmitScalarExpr(
1838 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1839 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1840 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1841 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001842
Daniel Dunbar898d5082008-09-30 01:06:03 +00001843 CGF.EmitBlock(FinallyJump);
1844
1845 CGF.EmitBlock(FinallyRethrow);
1846 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1847 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001848 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001849
1850 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001851}
1852
1853void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001854 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001855 llvm::Value *ExceptionAsObject;
1856
1857 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1858 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1859 ExceptionAsObject =
1860 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1861 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001862 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001863 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001864 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001865 }
1866
1867 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001868 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001869
1870 // Clear the insertion point to indicate we are in unreachable code.
1871 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001872}
1873
Daniel Dunbar898d5082008-09-30 01:06:03 +00001874void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1875 llvm::BasicBlock *Dst,
1876 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001877 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001878 return;
1879
1880 // Find the destination code for this block. We always use 0 for the
1881 // fallthrough block (default destination).
1882 llvm::SwitchInst *SI = E->FinallySwitch;
1883 llvm::ConstantInt *ID;
1884 if (Dst == SI->getDefaultDest()) {
1885 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1886 } else {
1887 ID = SI->findCaseDest(Dst);
1888 if (!ID) {
1889 // No code found, get a new unique one by just using the number
1890 // of switch successors.
1891 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1892 SI->addCase(ID, Dst);
1893 }
1894 }
1895
1896 // Set the destination code and branch.
1897 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001898 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001899}
1900
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001901/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001902/// object: objc_read_weak (id *src)
1903///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001904llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001905 llvm::Value *AddrWeakObj)
1906{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001907 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001908 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001909 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001910 return read_weak;
1911}
1912
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001913/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
1914/// objc_assign_weak (id src, id *dst)
1915///
1916void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1917 llvm::Value *src, llvm::Value *dst)
1918{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001919 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1920 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001921 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
1922 src, dst, "weakassign");
1923 return;
1924}
1925
Fariborz Jahanian58626502008-11-19 00:59:10 +00001926/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
1927/// objc_assign_global (id src, id *dst)
1928///
1929void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1930 llvm::Value *src, llvm::Value *dst)
1931{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001932 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1933 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001934 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
1935 src, dst, "globalassign");
1936 return;
1937}
1938
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001939/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
1940/// objc_assign_ivar (id src, id *dst)
1941///
1942void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1943 llvm::Value *src, llvm::Value *dst)
1944{
1945 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1946 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
1947 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
1948 src, dst, "assignivar");
1949 return;
1950}
1951
Fariborz Jahanian58626502008-11-19 00:59:10 +00001952/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
1953/// objc_assign_strongCast (id src, id *dst)
1954///
1955void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1956 llvm::Value *src, llvm::Value *dst)
1957{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001958 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1959 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001960 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
1961 src, dst, "weakassign");
1962 return;
1963}
1964
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001965/* *** Private Interface *** */
1966
1967/// EmitImageInfo - Emit the image info marker used to encode some module
1968/// level information.
1969///
1970/// See: <rdr://4810609&4810587&4810587>
1971/// struct IMAGE_INFO {
1972/// unsigned version;
1973/// unsigned flags;
1974/// };
1975enum ImageInfoFlags {
1976 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1977 eImageInfo_GarbageCollected = (1 << 1),
1978 eImageInfo_GCOnly = (1 << 2)
1979};
1980
1981void CGObjCMac::EmitImageInfo() {
1982 unsigned version = 0; // Version is unused?
1983 unsigned flags = 0;
1984
1985 // FIXME: Fix and continue?
1986 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1987 flags |= eImageInfo_GarbageCollected;
1988 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1989 flags |= eImageInfo_GCOnly;
1990
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001991 // Emitted as int[2];
1992 llvm::Constant *values[2] = {
1993 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1994 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1995 };
1996 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001997 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001998 new llvm::GlobalVariable(AT, true,
1999 llvm::GlobalValue::InternalLinkage,
2000 llvm::ConstantArray::get(AT, values, 2),
2001 "\01L_OBJC_IMAGE_INFO",
2002 &CGM.getModule());
2003
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002004 if (ObjCABI == 1) {
2005 GV->setSection("__OBJC, __image_info,regular");
2006 } else {
2007 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2008 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002009
2010 UsedGlobals.push_back(GV);
2011}
2012
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002013
2014// struct objc_module {
2015// unsigned long version;
2016// unsigned long size;
2017// const char *name;
2018// Symtab symtab;
2019// };
2020
2021// FIXME: Get from somewhere
2022static const int ModuleVersion = 7;
2023
2024void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002025 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002026
2027 std::vector<llvm::Constant*> Values(4);
2028 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2029 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002030 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002031 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002032 Values[3] = EmitModuleSymbols();
2033
2034 llvm::GlobalVariable *GV =
2035 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2036 llvm::GlobalValue::InternalLinkage,
2037 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2038 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002039 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002040 &CGM.getModule());
2041 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2042 UsedGlobals.push_back(GV);
2043}
2044
2045llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002046 unsigned NumClasses = DefinedClasses.size();
2047 unsigned NumCategories = DefinedCategories.size();
2048
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002049 // Return null if no symbols were defined.
2050 if (!NumClasses && !NumCategories)
2051 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2052
2053 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002054 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2055 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2056 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2057 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2058
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002059 // The runtime expects exactly the list of defined classes followed
2060 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002061 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002062 for (unsigned i=0; i<NumClasses; i++)
2063 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2064 ObjCTypes.Int8PtrTy);
2065 for (unsigned i=0; i<NumCategories; i++)
2066 Symbols[NumClasses + i] =
2067 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2068 ObjCTypes.Int8PtrTy);
2069
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002070 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002071 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002072 NumClasses + NumCategories),
2073 Symbols);
2074
2075 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2076
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002077 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002078 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002079 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002080 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002081 "\01L_OBJC_SYMBOLS",
2082 &CGM.getModule());
2083 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2084 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002085 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2086}
2087
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002088llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002089 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002090 LazySymbols.insert(ID->getIdentifier());
2091
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002092 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2093
2094 if (!Entry) {
2095 llvm::Constant *Casted =
2096 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2097 ObjCTypes.ClassPtrTy);
2098 Entry =
2099 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2100 llvm::GlobalValue::InternalLinkage,
2101 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2102 &CGM.getModule());
2103 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2104 UsedGlobals.push_back(Entry);
2105 }
2106
2107 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002108}
2109
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002110llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002111 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2112
2113 if (!Entry) {
2114 llvm::Constant *Casted =
2115 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2116 ObjCTypes.SelectorPtrTy);
2117 Entry =
2118 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2119 llvm::GlobalValue::InternalLinkage,
2120 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2121 &CGM.getModule());
2122 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2123 UsedGlobals.push_back(Entry);
2124 }
2125
2126 return Builder.CreateLoad(Entry, false, "tmp");
2127}
2128
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002129llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
2130 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002131
2132 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002133 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002134 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002135 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002136 llvm::GlobalValue::InternalLinkage,
2137 C, "\01L_OBJC_CLASS_NAME_",
2138 &CGM.getModule());
2139 Entry->setSection("__TEXT,__cstring,cstring_literals");
2140 UsedGlobals.push_back(Entry);
2141 }
2142
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002143 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002144}
2145
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002146llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002147 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2148
2149 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002150 // FIXME: Avoid std::string copying.
2151 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002152 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002153 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002154 llvm::GlobalValue::InternalLinkage,
2155 C, "\01L_OBJC_METH_VAR_NAME_",
2156 &CGM.getModule());
2157 Entry->setSection("__TEXT,__cstring,cstring_literals");
2158 UsedGlobals.push_back(Entry);
2159 }
2160
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002161 return getConstantGEP(Entry, 0, 0);
2162}
2163
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002164// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002165llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002166 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2167}
2168
2169// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002170llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002171 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2172}
2173
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002174llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002175 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002176
2177 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002178 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002179 Entry =
2180 new llvm::GlobalVariable(C->getType(), false,
2181 llvm::GlobalValue::InternalLinkage,
2182 C, "\01L_OBJC_METH_VAR_TYPE_",
2183 &CGM.getModule());
2184 Entry->setSection("__TEXT,__cstring,cstring_literals");
2185 UsedGlobals.push_back(Entry);
2186 }
2187
2188 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002189}
2190
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002191// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002192llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002194 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2195 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002196 return GetMethodVarType(TypeStr);
2197}
2198
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002199// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002200llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002201 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2202
2203 if (!Entry) {
2204 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2205 Entry =
2206 new llvm::GlobalVariable(C->getType(), false,
2207 llvm::GlobalValue::InternalLinkage,
2208 C, "\01L_OBJC_PROP_NAME_ATTR_",
2209 &CGM.getModule());
2210 Entry->setSection("__TEXT,__cstring,cstring_literals");
2211 UsedGlobals.push_back(Entry);
2212 }
2213
2214 return getConstantGEP(Entry, 0, 0);
2215}
2216
2217// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002218// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002219llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002220 const Decl *Container) {
2221 std::string TypeStr;
2222 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002223 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2224}
2225
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002226void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2227 const ObjCContainerDecl *CD,
2228 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002229 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002230 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002231 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002232 assert (CD && "Missing container decl in GetNameForMethod");
2233 NameOut += CD->getNameAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +00002234 NameOut += ' ';
2235 NameOut += D->getSelector().getAsString();
2236 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002237}
2238
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002239void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002240 EmitModuleInfo();
2241
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002242 // Emit the dummy bodies for any protocols which were referenced but
2243 // never defined.
2244 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2245 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2246 if (i->second->hasInitializer())
2247 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002248
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002249 std::vector<llvm::Constant*> Values(5);
2250 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2251 Values[1] = GetClassName(i->first);
2252 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2253 Values[3] = Values[4] =
2254 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2255 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2256 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2257 Values));
2258 }
2259
2260 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002261 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002262 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002264 }
2265
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002266 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002267 llvm::GlobalValue *GV =
2268 new llvm::GlobalVariable(AT, false,
2269 llvm::GlobalValue::AppendingLinkage,
2270 llvm::ConstantArray::get(AT, Used),
2271 "llvm.used",
2272 &CGM.getModule());
2273
2274 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002275
2276 // Add assembler directives to add lazy undefined symbol references
2277 // for classes which are referenced but not defined. This is
2278 // important for correct linker interaction.
2279
2280 // FIXME: Uh, this isn't particularly portable.
2281 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002282
2283 if (!CGM.getModule().getModuleInlineAsm().empty())
2284 s << "\n";
2285
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002286 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2287 e = LazySymbols.end(); i != e; ++i) {
2288 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2289 }
2290 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2291 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002292 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002293 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2294 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002295
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002296 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002297}
2298
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002299CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002300 : CGObjCCommonMac(cgm),
2301 ObjCTypes(cgm)
2302{
2303 ObjCABI = 2;
2304}
2305
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002306/* *** */
2307
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002308ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2309: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002310{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002311 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2312 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002313
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002314 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002315 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002316 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2318
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002319 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002320 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002321 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002322
2323 // FIXME: It would be nice to unify this with the opaque type, so
2324 // that the IR comes out a bit cleaner.
2325 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2326 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002327
2328 // I'm not sure I like this. The implicit coordination is a bit
2329 // gross. We should solve this in a reasonable fashion because this
2330 // is a pretty common task (match some runtime data structure with
2331 // an LLVM data structure).
2332
2333 // FIXME: This is leaked.
2334 // FIXME: Merge with rewriter code?
2335
2336 // struct _objc_super {
2337 // id self;
2338 // Class cls;
2339 // }
2340 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2341 SourceLocation(),
2342 &Ctx.Idents.get("_objc_super"));
2343 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2344 Ctx.getObjCIdType(), 0, false));
2345 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2346 Ctx.getObjCClassType(), 0, false));
2347 RD->completeDefinition(Ctx);
2348
2349 SuperCTy = Ctx.getTagDeclType(RD);
2350 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2351
2352 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002353 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2354
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002355 // struct _prop_t {
2356 // char *name;
2357 // char *attributes;
2358 // }
2359 PropertyTy = llvm::StructType::get(Int8PtrTy,
2360 Int8PtrTy,
2361 NULL);
2362 CGM.getModule().addTypeName("struct._prop_t",
2363 PropertyTy);
2364
2365 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002366 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002367 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002368 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002369 // }
2370 PropertyListTy = llvm::StructType::get(IntTy,
2371 IntTy,
2372 llvm::ArrayType::get(PropertyTy, 0),
2373 NULL);
2374 CGM.getModule().addTypeName("struct._prop_list_t",
2375 PropertyListTy);
2376 // struct _prop_list_t *
2377 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2378
2379 // struct _objc_method {
2380 // SEL _cmd;
2381 // char *method_type;
2382 // char *_imp;
2383 // }
2384 MethodTy = llvm::StructType::get(SelectorPtrTy,
2385 Int8PtrTy,
2386 Int8PtrTy,
2387 NULL);
2388 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002389
2390 // struct _objc_cache *
2391 CacheTy = llvm::OpaqueType::get();
2392 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2393 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002394
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002395 // Property manipulation functions.
2396
2397 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2398 std::vector<const llvm::Type*> Params;
2399 Params.push_back(ObjectPtrTy);
2400 Params.push_back(SelectorPtrTy);
2401 Params.push_back(LongTy);
2402 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2403 GetPropertyFn =
2404 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2405 Params,
2406 false),
2407 "objc_getProperty");
2408
2409 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2410 Params.clear();
2411 Params.push_back(ObjectPtrTy);
2412 Params.push_back(SelectorPtrTy);
2413 Params.push_back(LongTy);
2414 Params.push_back(ObjectPtrTy);
2415 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2416 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2417 SetPropertyFn =
2418 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2419 Params,
2420 false),
2421 "objc_setProperty");
2422 // Enumeration mutation.
2423
2424 Params.clear();
2425 Params.push_back(ObjectPtrTy);
2426 EnumerationMutationFn =
2427 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2428 Params,
2429 false),
2430 "objc_enumerationMutation");
2431
2432 // gc's API
2433 // id objc_read_weak (id *)
2434 Params.clear();
2435 Params.push_back(PtrObjectPtrTy);
2436 GcReadWeakFn =
2437 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2438 Params,
2439 false),
2440 "objc_read_weak");
2441 // id objc_assign_weak (id, id *)
2442 Params.clear();
2443 Params.push_back(ObjectPtrTy);
2444 Params.push_back(PtrObjectPtrTy);
2445 GcAssignWeakFn =
2446 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2447 Params,
2448 false),
2449 "objc_assign_weak");
2450 GcAssignGlobalFn =
2451 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2452 Params,
2453 false),
2454 "objc_assign_global");
2455 GcAssignIvarFn =
2456 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2457 Params,
2458 false),
2459 "objc_assign_ivar");
2460 GcAssignStrongCastFn =
2461 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2462 Params,
2463 false),
2464 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002465}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002466
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002467ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2468 : ObjCCommonTypesHelper(cgm)
2469{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002470 // struct _objc_method_description {
2471 // SEL name;
2472 // char *types;
2473 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002474 MethodDescriptionTy =
2475 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002476 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002477 NULL);
2478 CGM.getModule().addTypeName("struct._objc_method_description",
2479 MethodDescriptionTy);
2480
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002481 // struct _objc_method_description_list {
2482 // int count;
2483 // struct _objc_method_description[1];
2484 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002485 MethodDescriptionListTy =
2486 llvm::StructType::get(IntTy,
2487 llvm::ArrayType::get(MethodDescriptionTy, 0),
2488 NULL);
2489 CGM.getModule().addTypeName("struct._objc_method_description_list",
2490 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002491
2492 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002493 MethodDescriptionListPtrTy =
2494 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2495
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002496 // Protocol description structures
2497
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002498 // struct _objc_protocol_extension {
2499 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2500 // struct _objc_method_description_list *optional_instance_methods;
2501 // struct _objc_method_description_list *optional_class_methods;
2502 // struct _objc_property_list *instance_properties;
2503 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002504 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002505 llvm::StructType::get(IntTy,
2506 MethodDescriptionListPtrTy,
2507 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002508 PropertyListPtrTy,
2509 NULL);
2510 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2511 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002512
2513 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002514 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2515
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002516 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002517
2518 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2519 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2520
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002521 const llvm::Type *T =
2522 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2523 LongTy,
2524 llvm::ArrayType::get(ProtocolTyHolder, 0),
2525 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002526 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2527
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002528 // struct _objc_protocol {
2529 // struct _objc_protocol_extension *isa;
2530 // char *protocol_name;
2531 // struct _objc_protocol **_objc_protocol_list;
2532 // struct _objc_method_description_list *instance_methods;
2533 // struct _objc_method_description_list *class_methods;
2534 // }
2535 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002536 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002537 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2538 MethodDescriptionListPtrTy,
2539 MethodDescriptionListPtrTy,
2540 NULL);
2541 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2542
2543 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2544 CGM.getModule().addTypeName("struct._objc_protocol_list",
2545 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002546 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002547 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2548
2549 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002550 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002551 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002552
2553 // Class description structures
2554
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002555 // struct _objc_ivar {
2556 // char *ivar_name;
2557 // char *ivar_type;
2558 // int ivar_offset;
2559 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002560 IvarTy = llvm::StructType::get(Int8PtrTy,
2561 Int8PtrTy,
2562 IntTy,
2563 NULL);
2564 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2565
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002566 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002567 IvarListTy = llvm::OpaqueType::get();
2568 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2569 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2570
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002571 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002572 MethodListTy = llvm::OpaqueType::get();
2573 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2574 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2575
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002576 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002577 ClassExtensionTy =
2578 llvm::StructType::get(IntTy,
2579 Int8PtrTy,
2580 PropertyListPtrTy,
2581 NULL);
2582 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2583 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2584
2585 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2586
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002587 // struct _objc_class {
2588 // Class isa;
2589 // Class super_class;
2590 // char *name;
2591 // long version;
2592 // long info;
2593 // long instance_size;
2594 // struct _objc_ivar_list *ivars;
2595 // struct _objc_method_list *methods;
2596 // struct _objc_cache *cache;
2597 // struct _objc_protocol_list *protocols;
2598 // char *ivar_layout;
2599 // struct _objc_class_ext *ext;
2600 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002601 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2602 llvm::PointerType::getUnqual(ClassTyHolder),
2603 Int8PtrTy,
2604 LongTy,
2605 LongTy,
2606 LongTy,
2607 IvarListPtrTy,
2608 MethodListPtrTy,
2609 CachePtrTy,
2610 ProtocolListPtrTy,
2611 Int8PtrTy,
2612 ClassExtensionPtrTy,
2613 NULL);
2614 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2615
2616 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2617 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2618 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2619
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002620 // struct _objc_category {
2621 // char *category_name;
2622 // char *class_name;
2623 // struct _objc_method_list *instance_method;
2624 // struct _objc_method_list *class_method;
2625 // uint32_t size; // sizeof(struct _objc_category)
2626 // struct _objc_property_list *instance_properties;// category's @property
2627 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002628 CategoryTy = llvm::StructType::get(Int8PtrTy,
2629 Int8PtrTy,
2630 MethodListPtrTy,
2631 MethodListPtrTy,
2632 ProtocolListPtrTy,
2633 IntTy,
2634 PropertyListPtrTy,
2635 NULL);
2636 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2637
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002638 // Global metadata structures
2639
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002640 // struct _objc_symtab {
2641 // long sel_ref_cnt;
2642 // SEL *refs;
2643 // short cls_def_cnt;
2644 // short cat_def_cnt;
2645 // char *defs[cls_def_cnt + cat_def_cnt];
2646 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002647 SymtabTy = llvm::StructType::get(LongTy,
2648 SelectorPtrTy,
2649 ShortTy,
2650 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002651 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002652 NULL);
2653 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2654 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2655
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002656 // struct _objc_module {
2657 // long version;
2658 // long size; // sizeof(struct _objc_module)
2659 // char *name;
2660 // struct _objc_symtab* symtab;
2661 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002662 ModuleTy =
2663 llvm::StructType::get(LongTy,
2664 LongTy,
2665 Int8PtrTy,
2666 SymtabPtrTy,
2667 NULL);
2668 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002669
Daniel Dunbar49f66022008-09-24 03:38:44 +00002670 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002671
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002672 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002673 std::vector<const llvm::Type*> Params;
2674 Params.push_back(ObjectPtrTy);
2675 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002676 MessageSendFn =
2677 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2678 Params,
2679 true),
2680 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002681
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002682 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002683 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002684 Params.push_back(ObjectPtrTy);
2685 Params.push_back(SelectorPtrTy);
2686 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002687 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2688 Params,
2689 true),
2690 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002691
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002692 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002693 Params.clear();
2694 Params.push_back(ObjectPtrTy);
2695 Params.push_back(SelectorPtrTy);
2696 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002697 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002698 MessageSendFpretFn =
2699 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2700 Params,
2701 true),
2702 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002703
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002704 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002705 Params.clear();
2706 Params.push_back(SuperPtrTy);
2707 Params.push_back(SelectorPtrTy);
2708 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002709 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2710 Params,
2711 true),
2712 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002713
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002714 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2715 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002716 Params.clear();
2717 Params.push_back(Int8PtrTy);
2718 Params.push_back(SuperPtrTy);
2719 Params.push_back(SelectorPtrTy);
2720 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002721 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2722 Params,
2723 true),
2724 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002725
2726 // There is no objc_msgSendSuper_fpret? How can that work?
2727 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002728
Anders Carlsson124526b2008-09-09 10:10:21 +00002729 // FIXME: This is the size of the setjmp buffer and should be
2730 // target specific. 18 is what's used on 32-bit X86.
2731 uint64_t SetJmpBufferSize = 18;
2732
2733 // Exceptions
2734 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002735 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002736
2737 ExceptionDataTy =
2738 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2739 SetJmpBufferSize),
2740 StackPtrTy, NULL);
2741 CGM.getModule().addTypeName("struct._objc_exception_data",
2742 ExceptionDataTy);
2743
2744 Params.clear();
2745 Params.push_back(ObjectPtrTy);
2746 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002747 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2748 Params,
2749 false),
2750 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002751
2752 Params.clear();
2753 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2754 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002755 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2756 Params,
2757 false),
2758 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002759 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002760 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2761 Params,
2762 false),
2763 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002764 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002765 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2766 Params,
2767 false),
2768 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002769
2770 Params.clear();
2771 Params.push_back(ClassPtrTy);
2772 Params.push_back(ObjectPtrTy);
2773 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002774 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2775 Params,
2776 false),
2777 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002778
2779 // synchronized APIs
2780 // void objc_sync_enter (id)
2781 Params.clear();
2782 Params.push_back(ObjectPtrTy);
2783 SyncEnterFn =
2784 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2785 Params,
2786 false),
2787 "objc_sync_enter");
2788 // void objc_sync_exit (id)
2789 SyncExitFn =
2790 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2791 Params,
2792 false),
2793 "objc_sync_exit");
2794
Anders Carlsson124526b2008-09-09 10:10:21 +00002795
2796 Params.clear();
2797 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2798 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002799 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2800 Params,
2801 false),
2802 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002803
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002804}
2805
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002806ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002807: ObjCCommonTypesHelper(cgm)
2808{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002809 // struct _method_list_t {
2810 // uint32_t entsize; // sizeof(struct _objc_method)
2811 // uint32_t method_count;
2812 // struct _objc_method method_list[method_count];
2813 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002814 MethodListnfABITy = llvm::StructType::get(IntTy,
2815 IntTy,
2816 llvm::ArrayType::get(MethodTy, 0),
2817 NULL);
2818 CGM.getModule().addTypeName("struct.__method_list_t",
2819 MethodListnfABITy);
2820 // struct method_list_t *
2821 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002822
2823 // struct _protocol_t {
2824 // id isa; // NULL
2825 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002826 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002827 // const struct method_list_t * const instance_methods;
2828 // const struct method_list_t * const class_methods;
2829 // const struct method_list_t *optionalInstanceMethods;
2830 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002831 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002832 // const uint32_t size; // sizeof(struct _protocol_t)
2833 // const uint32_t flags; // = 0
2834 // }
2835
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002836 // Holder for struct _protocol_list_t *
2837 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2838
2839 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2840 Int8PtrTy,
2841 llvm::PointerType::getUnqual(
2842 ProtocolListTyHolder),
2843 MethodListnfABIPtrTy,
2844 MethodListnfABIPtrTy,
2845 MethodListnfABIPtrTy,
2846 MethodListnfABIPtrTy,
2847 PropertyListPtrTy,
2848 IntTy,
2849 IntTy,
2850 NULL);
2851 CGM.getModule().addTypeName("struct._protocol_t",
2852 ProtocolnfABITy);
2853
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002854 // struct _objc_protocol_list {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002855 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002856 // struct _protocol_t[protocol_count];
2857 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002858 ProtocolListnfABITy = llvm::StructType::get(LongTy,
2859 llvm::ArrayType::get(
2860 ProtocolnfABITy, 0),
2861 NULL);
2862 CGM.getModule().addTypeName("struct._objc_protocol_list",
2863 ProtocolListnfABITy);
2864
2865 // FIXME! Is this doing the right thing?
2866 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
2867 ProtocolListnfABITy);
2868
2869 // struct _objc_protocol_list*
2870 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002871
2872 // struct _ivar_t {
2873 // unsigned long int *offset; // pointer to ivar offset location
2874 // char *name;
2875 // char *type;
2876 // uint32_t alignment;
2877 // uint32_t size;
2878 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002879 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
2880 Int8PtrTy,
2881 Int8PtrTy,
2882 IntTy,
2883 IntTy,
2884 NULL);
2885 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
2886
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002887 // struct _ivar_list_t {
2888 // uint32 entsize; // sizeof(struct _ivar_t)
2889 // uint32 count;
2890 // struct _iver_t list[count];
2891 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002892 IvarListnfABIPtrTy = llvm::StructType::get(IntTy,
2893 IntTy,
2894 llvm::ArrayType::get(
2895 IvarnfABITy, 0),
2896 NULL);
2897 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABIPtrTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002898
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002899 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002900 // uint32_t const flags;
2901 // uint32_t const instanceStart;
2902 // uint32_t const instanceSize;
2903 // uint32_t const reserved; // only when building for 64bit targets
2904 // const uint8_t * const ivarLayout;
2905 // const char *const name;
2906 // const struct _method_list_t * const baseMethods;
2907 // const struct _objc_protocol_list *const baseProtocols;
2908 // const struct _ivar_list_t *const ivars;
2909 // const uint8_t * const weakIvarLayout;
2910 // const struct _prop_list_t * const properties;
2911 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002912
2913 // FIXME. Add 'reserved' field in 64bit abi mode!
2914 ClassRonfABITy = llvm::StructType::get(IntTy,
2915 IntTy,
2916 IntTy,
2917 Int8PtrTy,
2918 Int8PtrTy,
2919 MethodListnfABIPtrTy,
2920 ProtocolListnfABIPtrTy,
2921 IvarListnfABIPtrTy,
2922 Int8PtrTy,
2923 PropertyListPtrTy,
2924 NULL);
2925 CGM.getModule().addTypeName("struct._class_ro_t",
2926 ClassRonfABITy);
2927
2928 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
2929 std::vector<const llvm::Type*> Params;
2930 Params.push_back(ObjectPtrTy);
2931 Params.push_back(SelectorPtrTy);
2932 ImpnfABITy = llvm::PointerType::getUnqual(
2933 llvm::FunctionType::get(ObjectPtrTy, Params, false));
2934
2935 // struct _class_t {
2936 // struct _class_t *isa;
2937 // struct _class_t * const superclass;
2938 // void *cache;
2939 // IMP *vtable;
2940 // struct class_ro_t *ro;
2941 // }
2942
2943 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2944 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2945 llvm::PointerType::getUnqual(ClassTyHolder),
2946 CachePtrTy,
2947 llvm::PointerType::getUnqual(ImpnfABITy),
2948 llvm::PointerType::getUnqual(
2949 ClassRonfABITy),
2950 NULL);
2951 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
2952
2953 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
2954 ClassnfABITy);
2955
2956 // struct _category_t {
2957 // const char * const name;
2958 // struct _class_t *const cls;
2959 // const struct _method_list_t * const instance_methods;
2960 // const struct _method_list_t * const class_methods;
2961 // const struct _protocol_list_t * const protocols;
2962 // const struct _prop_list_t * const properties;
2963 // }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002964}
2965
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002966/* *** */
2967
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002968CodeGen::CGObjCRuntime *
2969CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002970 return new CGObjCMac(CGM);
2971}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002972
2973CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002974CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002975 return 0;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002976 // return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002977}