blob: 0a2c3e16a1a7a378dc46dae186dae890eb71e746 [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 Jahaniandb286862009-01-22 00:37:21 +000068 llvm::Function *GetPropertyFn, *SetPropertyFn;
69
70 llvm::Function *EnumerationMutationFn;
71
72 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
73 llvm::Function *GcReadWeakFn;
74
75 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
76 llvm::Function *GcAssignWeakFn;
77
78 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
79 llvm::Function *GcAssignGlobalFn;
80
81 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
82 llvm::Function *GcAssignIvarFn;
83
84 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
85 llvm::Function *GcAssignStrongCastFn;
86
Fariborz Jahanianee0af742009-01-21 22:04:16 +000087 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
88 ~ObjCCommonTypesHelper(){}
89};
Daniel Dunbare8b470d2008-08-23 04:28:29 +000090
Fariborz Jahanianee0af742009-01-21 22:04:16 +000091/// ObjCTypesHelper - Helper class that encapsulates lazy
92/// construction of varies types used during ObjC generation.
93class ObjCTypesHelper : public ObjCCommonTypesHelper {
94private:
95
96 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
97 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
98 *MessageSendSuperFpretFn;
99
100public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000101 /// SymtabTy - LLVM type for struct objc_symtab.
102 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000103 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
104 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000105 /// ModuleTy - LLVM type for struct objc_module.
106 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000107
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000108 /// ProtocolTy - LLVM type for struct objc_protocol.
109 const llvm::StructType *ProtocolTy;
110 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
111 const llvm::Type *ProtocolPtrTy;
112 /// ProtocolExtensionTy - LLVM type for struct
113 /// objc_protocol_extension.
114 const llvm::StructType *ProtocolExtensionTy;
115 /// ProtocolExtensionTy - LLVM type for struct
116 /// objc_protocol_extension *.
117 const llvm::Type *ProtocolExtensionPtrTy;
118 /// MethodDescriptionTy - LLVM type for struct
119 /// objc_method_description.
120 const llvm::StructType *MethodDescriptionTy;
121 /// MethodDescriptionListTy - LLVM type for struct
122 /// objc_method_description_list.
123 const llvm::StructType *MethodDescriptionListTy;
124 /// MethodDescriptionListPtrTy - LLVM type for struct
125 /// objc_method_description_list *.
126 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000127 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
128 /// in GCC parlance).
129 const llvm::StructType *PropertyTy;
130 /// PropertyListTy - LLVM type for struct objc_property_list
131 /// (_prop_list_t in GCC parlance).
132 const llvm::StructType *PropertyListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000133 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
134 const llvm::Type *PropertyListPtrTy;
135 /// ProtocolListTy - LLVM type for struct objc_property_list.
136 const llvm::Type *ProtocolListTy;
137 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
138 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000139 /// CategoryTy - LLVM type for struct objc_category.
140 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000141 /// ClassTy - LLVM type for struct objc_class.
142 const llvm::StructType *ClassTy;
143 /// ClassPtrTy - LLVM type for struct objc_class *.
144 const llvm::Type *ClassPtrTy;
145 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
146 const llvm::StructType *ClassExtensionTy;
147 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
148 const llvm::Type *ClassExtensionPtrTy;
149 /// CacheTy - LLVM type for struct objc_cache.
150 const llvm::Type *CacheTy;
151 /// CachePtrTy - LLVM type for struct objc_cache *.
152 const llvm::Type *CachePtrTy;
153 // IvarTy - LLVM type for struct objc_ivar.
154 const llvm::StructType *IvarTy;
155 /// IvarListTy - LLVM type for struct objc_ivar_list.
156 const llvm::Type *IvarListTy;
157 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
158 const llvm::Type *IvarListPtrTy;
159 // MethodTy - LLVM type for struct objc_method.
160 const llvm::StructType *MethodTy;
161 /// MethodListTy - LLVM type for struct objc_method_list.
162 const llvm::Type *MethodListTy;
163 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
164 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000165
166 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
167 const llvm::Type *ExceptionDataTy;
168
169 /// ExceptionThrowFn - LLVM objc_exception_throw function.
170 llvm::Function *ExceptionThrowFn;
171
172 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
173 llvm::Function *ExceptionTryEnterFn;
174
175 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
176 llvm::Function *ExceptionTryExitFn;
177
178 /// ExceptionExtractFn - LLVM objc_exception_extract function.
179 llvm::Function *ExceptionExtractFn;
180
181 /// ExceptionMatchFn - LLVM objc_exception_match function.
182 llvm::Function *ExceptionMatchFn;
183
184 /// SetJmpFn - LLVM _setjmp function.
185 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000186
187 /// SyncEnterFn - LLVM object_sync_enter function.
188 llvm::Function *SyncEnterFn;
189
190 /// SyncExitFn - LLVM object_sync_exit function.
191 llvm::Function *SyncExitFn;
192
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000193public:
194 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000195 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000196
197
198 llvm::Function *getSendFn(bool IsSuper) {
199 return IsSuper ? MessageSendSuperFn : MessageSendFn;
200 }
201
202 llvm::Function *getSendStretFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
204 }
205
206 llvm::Function *getSendFpretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
208 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000209};
210
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000211/// ObjCModernTypesHelper - will have all types needed by objective-c's
212/// modern abi
213class ObjCModernTypesHelper : public ObjCCommonTypesHelper {
214public:
215 ObjCModernTypesHelper(CodeGen::CodeGenModule &cgm);
216 ~ObjCModernTypesHelper(){}
217};
218
219class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
220protected:
221 CodeGen::CodeGenModule &CGM;
222 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000223 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000224
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000225 /// LazySymbols - Symbols to generate a lazy reference for. See
226 /// DefinedSymbols and FinishModule().
227 std::set<IdentifierInfo*> LazySymbols;
228
229 /// DefinedSymbols - External symbols which are defined by this
230 /// module. The symbols in this list and LazySymbols are used to add
231 /// special linker symbols which ensure that Objective-C modules are
232 /// linked properly.
233 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000234
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000235 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000236 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000237
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000238 /// MethodVarNames - uniqued method variable names.
239 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000240
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000241 /// MethodVarTypes - uniqued method type signatures. We have to use
242 /// a StringMap here because have no other unique reference.
243 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000244
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000245 /// MethodDefinitions - map of methods which have been defined in
246 /// this translation unit.
247 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000248
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000249 /// PropertyNames - uniqued method variable names.
250 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000251
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000252 /// ClassReferences - uniqued class references.
253 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000254
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000255 /// SelectorReferences - uniqued selector references.
256 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000257
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000258 /// Protocols - Protocols for which an objc_protocol structure has
259 /// been emitted. Forward declarations are handled by creating an
260 /// empty structure whose initializer is filled in when/if defined.
261 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000262
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000263 /// DefinedProtocols - Protocols which have actually been
264 /// defined. We should not need this, see FIXME in GenerateProtocol.
265 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000266
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000267 /// DefinedClasses - List of defined classes.
268 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000269
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000270 /// DefinedCategories - List of defined categories.
271 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000272
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000273 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000274 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000275 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000276
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000277 /// GetNameForMethod - Return a name for the given method.
278 /// \param[out] NameOut - The return value.
279 void GetNameForMethod(const ObjCMethodDecl *OMD,
280 const ObjCContainerDecl *CD,
281 std::string &NameOut);
282
283 /// GetMethodVarName - Return a unique constant for the given
284 /// selector's name. The return value has type char *.
285 llvm::Constant *GetMethodVarName(Selector Sel);
286 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
287 llvm::Constant *GetMethodVarName(const std::string &Name);
288
289 /// GetMethodVarType - Return a unique constant for the given
290 /// selector's name. The return value has type char *.
291
292 // FIXME: This is a horrible name.
293 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
294 llvm::Constant *GetMethodVarType(const std::string &Name);
295
296 /// GetPropertyName - Return a unique constant for the given
297 /// name. The return value has type char *.
298 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
299
300 // FIXME: This can be dropped once string functions are unified.
301 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
302 const Decl *Container);
303
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000304public:
305 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
306 { }
307};
308
309class CGObjCMac : public CGObjCCommonMac {
310private:
311 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000312 /// EmitImageInfo - Emit the image info marker used to encode some module
313 /// level information.
314 void EmitImageInfo();
315
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000316 /// EmitModuleInfo - Another marker encoding module level
317 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 void EmitModuleInfo();
319
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000320 /// EmitModuleSymols - Emit module symbols, the list of defined
321 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000322 llvm::Constant *EmitModuleSymbols();
323
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000324 /// FinishModule - Write out global data structures at the end of
325 /// processing a translation unit.
326 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000327
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000328 /// EmitClassExtension - Generate the class extension structure used
329 /// to store the weak ivar layout and properties. The return value
330 /// has type ClassExtensionPtrTy.
331 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
332
333 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
334 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000335 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000336 const ObjCInterfaceDecl *ID);
337
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000338 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000339 QualType ResultType,
340 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000341 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000342 QualType Arg0Ty,
343 bool IsSuper,
344 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000345
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000346 /// EmitIvarList - Emit the ivar list for the given
347 /// implementation. If ForClass is true the list of class ivars
348 /// (i.e. metaclass ivars) is emitted, otherwise the list of
349 /// interface ivars will be emitted. The return value has type
350 /// IvarListPtrTy.
351 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
352 bool ForClass,
353 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000354
355 /// EmitMetaClass - Emit a forward reference to the class structure
356 /// for the metaclass of the given interface. The return value has
357 /// type ClassPtrTy.
358 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
359
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000360 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000361 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000362 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
363 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000364 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000365 const ConstantVector &Methods);
366
367 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
368
369 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000370
371 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000372 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000373 llvm::Constant *EmitMethodList(const std::string &Name,
374 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000375 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000376
377 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000378 /// method declarations.
379 /// - TypeName: The name for the type containing the methods.
380 /// - IsProtocol: True iff these methods are for a protocol.
381 /// - ClassMethds: True iff these are class methods.
382 /// - Required: When true, only "required" methods are
383 /// listed. Similarly, when false only "optional" methods are
384 /// listed. For classes this should always be true.
385 /// - begin, end: The method list to output.
386 ///
387 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000388 llvm::Constant *EmitMethodDescList(const std::string &Name,
389 const char *Section,
390 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000391
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000392 /// EmitPropertyList - Emit the given property list. The return
393 /// value has type PropertyListPtrTy.
394 llvm::Constant *EmitPropertyList(const std::string &Name,
Steve Naroff93983f82009-01-11 12:47:58 +0000395 const Decl *Container,
396 const ObjCContainerDecl *OCD);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000397
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000398 /// GetOrEmitProtocol - Get the protocol object for the given
399 /// declaration, emitting it if necessary. The return value has type
400 /// ProtocolPtrTy.
401 llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
402
403 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
404 /// object for the given declaration, emitting it if needed. These
405 /// forward references will be filled in with empty bodies if no
406 /// definition is seen. The return value has type ProtocolPtrTy.
407 llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
408
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000409 /// EmitProtocolExtension - Generate the protocol extension
410 /// structure used to store optional instance and class methods, and
411 /// protocol properties. The return value has type
412 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000413 llvm::Constant *
414 EmitProtocolExtension(const ObjCProtocolDecl *PD,
415 const ConstantVector &OptInstanceMethods,
416 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000417
418 /// EmitProtocolList - Generate the list of referenced
419 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000420 llvm::Constant *EmitProtocolList(const std::string &Name,
421 ObjCProtocolDecl::protocol_iterator begin,
422 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000423
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000424 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
425 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000426 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000427
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000428 /// GetProtocolRef - Return a reference to the internal protocol
429 /// description, creating an empty one if it has not been
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000430 /// defined. The return value has type ProtocolPtrTy.
431 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000432
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000433 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000435 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000436
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000437public:
438 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000439 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000440
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000441 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000442 QualType ResultType,
443 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000444 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000445 bool IsClassMessage,
446 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000447
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000448 virtual CodeGen::RValue
449 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000450 QualType ResultType,
451 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000452 const ObjCInterfaceDecl *Class,
453 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000454 bool IsClassMessage,
455 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000456
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000457 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000458 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000459
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000460 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000461
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000462 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
463 const ObjCContainerDecl *CD=0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000464
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000465 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000466
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000467 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000468
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000469 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000470 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000471
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000472 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000473
474 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000475 virtual llvm::Function *GetPropertyGetFunction();
476 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000477 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000478
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000479 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
480 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000481 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
482 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000483 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000484 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000485 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
486 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000487 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
488 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000489 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
490 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000491 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
492 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000493};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000494
495class CGObjCModernMac : public CGObjCCommonMac {
496private:
497 ObjCModernTypesHelper ObjCTypes;
498public:
499 CGObjCModernMac(CodeGen::CodeGenModule &cgm);
500};
501
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000502} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000503
504/* *** Helper Functions *** */
505
506/// getConstantGEP() - Help routine to construct simple GEPs.
507static llvm::Constant *getConstantGEP(llvm::Constant *C,
508 unsigned idx0,
509 unsigned idx1) {
510 llvm::Value *Idxs[] = {
511 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
512 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
513 };
514 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
515}
516
517/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000518
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000519CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
520 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000521{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000522 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000523 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000524}
525
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000526/// GetClass - Return a reference to the class for the given interface
527/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000528llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 const ObjCInterfaceDecl *ID) {
530 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000531}
532
533/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000534llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000535 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000536}
537
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000538/// Generate a constant CFString object.
539/*
540 struct __builtin_CFString {
541 const int *isa; // point to __CFConstantStringClassReference
542 int flags;
543 const char *str;
544 long length;
545 };
546*/
547
548llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000549 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000550}
551
552/// Generates a message send where the super is the receiver. This is
553/// a message send to self with special delivery semantics indicating
554/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000555CodeGen::RValue
556CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000557 QualType ResultType,
558 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000559 const ObjCInterfaceDecl *Class,
560 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000561 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000562 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000563 // Create and init a super structure; this is a (receiver, class)
564 // pair we will pass to objc_msgSendSuper.
565 llvm::Value *ObjCSuper =
566 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
567 llvm::Value *ReceiverAsObject =
568 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
569 CGF.Builder.CreateStore(ReceiverAsObject,
570 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000571
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000572 // If this is a class message the metaclass is passed as the target.
573 llvm::Value *Target;
574 if (IsClassMessage) {
575 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
576 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
577 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
578 Target = Super;
579 } else {
580 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
581 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000582 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
583 // and ObjCTypes types.
584 const llvm::Type *ClassTy =
585 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000586 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000587 CGF.Builder.CreateStore(Target,
588 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
589
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000590 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000591 ObjCSuper, ObjCTypes.SuperPtrCTy,
592 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000593}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000594
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000595/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000596CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000597 QualType ResultType,
598 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000599 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000600 bool IsClassMessage,
601 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000602 llvm::Value *Arg0 =
603 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000604 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000605 Arg0, CGF.getContext().getObjCIdType(),
606 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000607}
608
609CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000610 QualType ResultType,
611 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000612 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000613 QualType Arg0Ty,
614 bool IsSuper,
615 const CallArgList &CallArgs) {
616 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000617 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
618 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
619 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000620 CGF.getContext().getObjCSelType()));
621 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000622
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000623 const llvm::FunctionType *FTy =
624 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
625 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000626
627 llvm::Constant *Fn;
628 if (CGM.ReturnTypeUsesSret(ResultType)) {
629 Fn = ObjCTypes.getSendStretFn(IsSuper);
630 } else if (ResultType->isFloatingType()) {
631 // FIXME: Sadly, this is wrong. This actually depends on the
632 // architecture. This happens to be right for x86-32 though.
633 Fn = ObjCTypes.getSendFpretFn(IsSuper);
634 } else {
635 Fn = ObjCTypes.getSendFn(IsSuper);
636 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000637 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000638 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000639}
640
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000641llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000642 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000643 // FIXME: I don't understand why gcc generates this, or where it is
644 // resolved. Investigate. Its also wasteful to look this up over and
645 // over.
646 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
647
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000648 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
649 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000650}
651
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000652void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
653 // FIXME: We shouldn't need this, the protocol decl should contain
654 // enough information to tell us whether this was a declaration or a
655 // definition.
656 DefinedProtocols.insert(PD->getIdentifier());
657
658 // If we have generated a forward reference to this protocol, emit
659 // it now. Otherwise do nothing, the protocol objects are lazily
660 // emitted.
661 if (Protocols.count(PD->getIdentifier()))
662 GetOrEmitProtocol(PD);
663}
664
665llvm::Constant *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
666 if (DefinedProtocols.count(PD->getIdentifier()))
667 return GetOrEmitProtocol(PD);
668 return GetOrEmitProtocolRef(PD);
669}
670
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000671/*
672 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
673 struct _objc_protocol {
674 struct _objc_protocol_extension *isa;
675 char *protocol_name;
676 struct _objc_protocol_list *protocol_list;
677 struct _objc__method_prototype_list *instance_methods;
678 struct _objc__method_prototype_list *class_methods
679 };
680
681 See EmitProtocolExtension().
682*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000683llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
684 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
685
686 // Early exit if a defining object has already been generated.
687 if (Entry && Entry->hasInitializer())
688 return Entry;
689
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000690 // FIXME: I don't understand why gcc generates this, or where it is
691 // resolved. Investigate. Its also wasteful to look this up over and
692 // over.
693 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
694
Chris Lattner8ec03f52008-11-24 03:54:41 +0000695 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000696
697 // Construct method lists.
698 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
699 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
700 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
701 e = PD->instmeth_end(); i != e; ++i) {
702 ObjCMethodDecl *MD = *i;
703 llvm::Constant *C = GetMethodDescriptionConstant(MD);
704 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
705 OptInstanceMethods.push_back(C);
706 } else {
707 InstanceMethods.push_back(C);
708 }
709 }
710
711 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
712 e = PD->classmeth_end(); i != e; ++i) {
713 ObjCMethodDecl *MD = *i;
714 llvm::Constant *C = GetMethodDescriptionConstant(MD);
715 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
716 OptClassMethods.push_back(C);
717 } else {
718 ClassMethods.push_back(C);
719 }
720 }
721
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000722 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000723 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000724 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000725 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000726 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000727 PD->protocol_begin(),
728 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000729 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000730 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
731 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000732 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
733 InstanceMethods);
734 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000735 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
736 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000737 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
738 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000739 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
740 Values);
741
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000742 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000743 // Already created, fix the linkage and update the initializer.
744 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000745 Entry->setInitializer(Init);
746 } else {
747 Entry =
748 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
749 llvm::GlobalValue::InternalLinkage,
750 Init,
751 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
752 &CGM.getModule());
753 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
754 UsedGlobals.push_back(Entry);
755 // FIXME: Is this necessary? Why only for protocol?
756 Entry->setAlignment(4);
757 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000758
759 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000760}
761
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000762llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000763 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
764
765 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000766 // We use the initializer as a marker of whether this is a forward
767 // reference or not. At module finalization we add the empty
768 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000769 Entry =
770 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000771 llvm::GlobalValue::ExternalLinkage,
772 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000773 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000774 &CGM.getModule());
775 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
776 UsedGlobals.push_back(Entry);
777 // FIXME: Is this necessary? Why only for protocol?
778 Entry->setAlignment(4);
779 }
780
781 return Entry;
782}
783
784/*
785 struct _objc_protocol_extension {
786 uint32_t size;
787 struct objc_method_description_list *optional_instance_methods;
788 struct objc_method_description_list *optional_class_methods;
789 struct objc_property_list *instance_properties;
790 };
791*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000792llvm::Constant *
793CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
794 const ConstantVector &OptInstanceMethods,
795 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000796 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000797 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000798 std::vector<llvm::Constant*> Values(4);
799 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000800 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000801 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
802 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000803 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
804 OptInstanceMethods);
805 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000806 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
807 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000808 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
809 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000810 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
811 PD->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +0000812 0, PD);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000813
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000814 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000815 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
816 Values[3]->isNullValue())
817 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
818
819 llvm::Constant *Init =
820 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
821 llvm::GlobalVariable *GV =
822 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
823 llvm::GlobalValue::InternalLinkage,
824 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000825 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000826 &CGM.getModule());
827 // No special section, but goes in llvm.used
828 UsedGlobals.push_back(GV);
829
830 return GV;
831}
832
833/*
834 struct objc_protocol_list {
835 struct objc_protocol_list *next;
836 long count;
837 Protocol *list[];
838 };
839*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000840llvm::Constant *
841CGObjCMac::EmitProtocolList(const std::string &Name,
842 ObjCProtocolDecl::protocol_iterator begin,
843 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000844 std::vector<llvm::Constant*> ProtocolRefs;
845
Daniel Dunbardbc933702008-08-21 21:57:41 +0000846 for (; begin != end; ++begin)
847 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000848
849 // Just return null for empty protocol lists
850 if (ProtocolRefs.empty())
851 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
852
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000853 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000854 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
855
856 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000857 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000858 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
859 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
860 Values[2] =
861 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
862 ProtocolRefs.size()),
863 ProtocolRefs);
864
865 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
866 llvm::GlobalVariable *GV =
867 new llvm::GlobalVariable(Init->getType(), false,
868 llvm::GlobalValue::InternalLinkage,
869 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000870 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000871 &CGM.getModule());
872 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
873 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
874}
875
876/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000877 struct _objc_property {
878 const char * const name;
879 const char * const attributes;
880 };
881
882 struct _objc_property_list {
883 uint32_t entsize; // sizeof (struct _objc_property)
884 uint32_t prop_count;
885 struct _objc_property[prop_count];
886 };
887*/
888llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000889 const Decl *Container,
Steve Naroff93983f82009-01-11 12:47:58 +0000890 const ObjCContainerDecl *OCD) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000891 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +0000892 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
893 E = OCD->prop_end(); I != E; ++I) {
894 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000895 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000896 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000897 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
898 Prop));
899 }
900
901 // Return null for empty list.
902 if (Properties.empty())
903 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
904
905 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000906 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000907 std::vector<llvm::Constant*> Values(3);
908 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
909 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
910 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
911 Properties.size());
912 Values[2] = llvm::ConstantArray::get(AT, Properties);
913 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
914
915 llvm::GlobalVariable *GV =
916 new llvm::GlobalVariable(Init->getType(), false,
917 llvm::GlobalValue::InternalLinkage,
918 Init,
919 Name,
920 &CGM.getModule());
921 // No special section on property lists?
922 UsedGlobals.push_back(GV);
923 return llvm::ConstantExpr::getBitCast(GV,
924 ObjCTypes.PropertyListPtrTy);
925
926}
927
928/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000929 struct objc_method_description_list {
930 int count;
931 struct objc_method_description list[];
932 };
933*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000934llvm::Constant *
935CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
936 std::vector<llvm::Constant*> Desc(2);
937 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
938 ObjCTypes.SelectorPtrTy);
939 Desc[1] = GetMethodVarType(MD);
940 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
941 Desc);
942}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000943
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000944llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
945 const char *Section,
946 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000947 // Return null for empty list.
948 if (Methods.empty())
949 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
950
951 std::vector<llvm::Constant*> Values(2);
952 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
953 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
954 Methods.size());
955 Values[1] = llvm::ConstantArray::get(AT, Methods);
956 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
957
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000958 llvm::GlobalVariable *GV =
959 new llvm::GlobalVariable(Init->getType(), false,
960 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000961 Init, Name, &CGM.getModule());
962 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000963 UsedGlobals.push_back(GV);
964 return llvm::ConstantExpr::getBitCast(GV,
965 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000966}
967
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000968/*
969 struct _objc_category {
970 char *category_name;
971 char *class_name;
972 struct _objc_method_list *instance_methods;
973 struct _objc_method_list *class_methods;
974 struct _objc_protocol_list *protocols;
975 uint32_t size; // <rdar://4585769>
976 struct _objc_property_list *instance_properties;
977 };
978 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000979void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000980 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000981
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000982 // FIXME: This is poor design, the OCD should have a pointer to the
983 // category decl. Additionally, note that Category can be null for
984 // the @implementation w/o an @interface case. Sema should just
985 // create one for us as it does for @implementation so everyone else
986 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000987 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000988 const ObjCCategoryDecl *Category =
989 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000990 std::string ExtName(Interface->getNameAsString() + "_" +
991 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000992
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000993 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
994 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
995 e = OCD->instmeth_end(); i != e; ++i) {
996 // Instance methods should always be defined.
997 InstanceMethods.push_back(GetMethodConstant(*i));
998 }
999 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1000 e = OCD->classmeth_end(); i != e; ++i) {
1001 // Class methods should always be defined.
1002 ClassMethods.push_back(GetMethodConstant(*i));
1003 }
1004
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001005 std::vector<llvm::Constant*> Values(7);
1006 Values[0] = GetClassName(OCD->getIdentifier());
1007 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001008 Values[2] =
1009 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1010 ExtName,
1011 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001012 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001013 Values[3] =
1014 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1015 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001016 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001017 if (Category) {
1018 Values[4] =
1019 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1020 Category->protocol_begin(),
1021 Category->protocol_end());
1022 } else {
1023 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1024 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001025 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001026
1027 // If there is no category @interface then there can be no properties.
1028 if (Category) {
1029 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Steve Naroff93983f82009-01-11 12:47:58 +00001030 OCD, Category);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001031 } else {
1032 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1033 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001034
1035 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1036 Values);
1037
1038 llvm::GlobalVariable *GV =
1039 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1040 llvm::GlobalValue::InternalLinkage,
1041 Init,
1042 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1043 &CGM.getModule());
1044 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1045 UsedGlobals.push_back(GV);
1046 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001047}
1048
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001049// FIXME: Get from somewhere?
1050enum ClassFlags {
1051 eClassFlags_Factory = 0x00001,
1052 eClassFlags_Meta = 0x00002,
1053 // <rdr://5142207>
1054 eClassFlags_HasCXXStructors = 0x02000,
1055 eClassFlags_Hidden = 0x20000,
1056 eClassFlags_ABI2_Hidden = 0x00010,
1057 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1058};
1059
1060// <rdr://5142207&4705298&4843145>
1061static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1062 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1063 // FIXME: Support -fvisibility
1064 switch (attr->getVisibility()) {
1065 default:
1066 assert(0 && "Unknown visibility");
1067 return false;
1068 case VisibilityAttr::DefaultVisibility:
1069 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1070 return false;
1071 case VisibilityAttr::HiddenVisibility:
1072 return true;
1073 }
1074 } else {
1075 return false; // FIXME: Support -fvisibility
1076 }
1077}
1078
1079/*
1080 struct _objc_class {
1081 Class isa;
1082 Class super_class;
1083 const char *name;
1084 long version;
1085 long info;
1086 long instance_size;
1087 struct _objc_ivar_list *ivars;
1088 struct _objc_method_list *methods;
1089 struct _objc_cache *cache;
1090 struct _objc_protocol_list *protocols;
1091 // Objective-C 1.0 extensions (<rdr://4585769>)
1092 const char *ivar_layout;
1093 struct _objc_class_ext *ext;
1094 };
1095
1096 See EmitClassExtension();
1097 */
1098void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001099 DefinedSymbols.insert(ID->getIdentifier());
1100
Chris Lattner8ec03f52008-11-24 03:54:41 +00001101 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001102 // FIXME: Gross
1103 ObjCInterfaceDecl *Interface =
1104 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001105 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001106 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001107 Interface->protocol_begin(),
1108 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001109 const llvm::Type *InterfaceTy =
1110 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1111 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001112 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113
1114 // FIXME: Set CXX-structors flag.
1115 if (IsClassHidden(ID->getClassInterface()))
1116 Flags |= eClassFlags_Hidden;
1117
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001118 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1119 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1120 e = ID->instmeth_end(); i != e; ++i) {
1121 // Instance methods should always be defined.
1122 InstanceMethods.push_back(GetMethodConstant(*i));
1123 }
1124 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1125 e = ID->classmeth_end(); i != e; ++i) {
1126 // Class methods should always be defined.
1127 ClassMethods.push_back(GetMethodConstant(*i));
1128 }
1129
1130 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1131 e = ID->propimpl_end(); i != e; ++i) {
1132 ObjCPropertyImplDecl *PID = *i;
1133
1134 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1135 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1136
1137 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1138 if (llvm::Constant *C = GetMethodConstant(MD))
1139 InstanceMethods.push_back(C);
1140 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1141 if (llvm::Constant *C = GetMethodConstant(MD))
1142 InstanceMethods.push_back(C);
1143 }
1144 }
1145
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001146 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001147 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001148 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001149 // Record a reference to the super class.
1150 LazySymbols.insert(Super->getIdentifier());
1151
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001152 Values[ 1] =
1153 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1154 ObjCTypes.ClassPtrTy);
1155 } else {
1156 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1157 }
1158 Values[ 2] = GetClassName(ID->getIdentifier());
1159 // Version is always 0.
1160 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1161 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1162 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1163 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001164 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001165 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001166 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001167 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001168 // cache is always NULL.
1169 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1170 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001171 // FIXME: Set ivar_layout
1172 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001173 Values[11] = EmitClassExtension(ID);
1174 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1175 Values);
1176
1177 llvm::GlobalVariable *GV =
1178 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1179 llvm::GlobalValue::InternalLinkage,
1180 Init,
1181 std::string("\01L_OBJC_CLASS_")+ClassName,
1182 &CGM.getModule());
1183 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1184 UsedGlobals.push_back(GV);
1185 // FIXME: Why?
1186 GV->setAlignment(32);
1187 DefinedClasses.push_back(GV);
1188}
1189
1190llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1191 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001192 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001193 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001194 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001195 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001196
1197 if (IsClassHidden(ID->getClassInterface()))
1198 Flags |= eClassFlags_Hidden;
1199
1200 std::vector<llvm::Constant*> Values(12);
1201 // The isa for the metaclass is the root of the hierarchy.
1202 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1203 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1204 Root = Super;
1205 Values[ 0] =
1206 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1207 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001208 // The super class for the metaclass is emitted as the name of the
1209 // super class. The runtime fixes this up to point to the
1210 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001211 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1212 Values[ 1] =
1213 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1214 ObjCTypes.ClassPtrTy);
1215 } else {
1216 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1217 }
1218 Values[ 2] = GetClassName(ID->getIdentifier());
1219 // Version is always 0.
1220 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1221 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1222 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1223 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001224 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001225 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001226 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001227 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001228 // cache is always NULL.
1229 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1230 Values[ 9] = Protocols;
1231 // ivar_layout for metaclass is always NULL.
1232 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1233 // The class extension is always unused for metaclasses.
1234 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1235 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1236 Values);
1237
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001238 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001239 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001240
1241 // Check for a forward reference.
1242 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1243 if (GV) {
1244 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1245 "Forward metaclass reference has incorrect type.");
1246 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1247 GV->setInitializer(Init);
1248 } else {
1249 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1250 llvm::GlobalValue::InternalLinkage,
1251 Init, Name,
1252 &CGM.getModule());
1253 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001254 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1255 UsedGlobals.push_back(GV);
1256 // FIXME: Why?
1257 GV->setAlignment(32);
1258
1259 return GV;
1260}
1261
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001262llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001263 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001264
1265 // FIXME: Should we look these up somewhere other than the
1266 // module. Its a bit silly since we only generate these while
1267 // processing an implementation, so exactly one pointer would work
1268 // if know when we entered/exitted an implementation block.
1269
1270 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001271 // Previously, metaclass with internal linkage may have been defined.
1272 // pass 'true' as 2nd argument so it is returned.
1273 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001274 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1275 "Forward metaclass reference has incorrect type.");
1276 return GV;
1277 } else {
1278 // Generate as an external reference to keep a consistent
1279 // module. This will be patched up when we emit the metaclass.
1280 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1281 llvm::GlobalValue::ExternalLinkage,
1282 0,
1283 Name,
1284 &CGM.getModule());
1285 }
1286}
1287
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001288/*
1289 struct objc_class_ext {
1290 uint32_t size;
1291 const char *weak_ivar_layout;
1292 struct _objc_property_list *properties;
1293 };
1294*/
1295llvm::Constant *
1296CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1297 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001298 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001299
1300 std::vector<llvm::Constant*> Values(3);
1301 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001302 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001303 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001304 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Steve Naroff93983f82009-01-11 12:47:58 +00001305 ID, ID->getClassInterface());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001306
1307 // Return null if no extension bits are used.
1308 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1309 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1310
1311 llvm::Constant *Init =
1312 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1313 llvm::GlobalVariable *GV =
1314 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1315 llvm::GlobalValue::InternalLinkage,
1316 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001317 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001318 &CGM.getModule());
1319 // No special section, but goes in llvm.used
1320 UsedGlobals.push_back(GV);
1321
1322 return GV;
1323}
1324
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001325/// countInheritedIvars - count number of ivars in class and its super class(s)
1326///
1327static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1328 int count = 0;
1329 if (!OI)
1330 return 0;
1331 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1332 if (SuperClass)
1333 count += countInheritedIvars(SuperClass);
1334 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1335 E = OI->ivar_end(); I != E; ++I)
1336 ++count;
1337 return count;
1338}
1339
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001340/*
1341 struct objc_ivar {
1342 char *ivar_name;
1343 char *ivar_type;
1344 int ivar_offset;
1345 };
1346
1347 struct objc_ivar_list {
1348 int ivar_count;
1349 struct objc_ivar list[count];
1350 };
1351 */
1352llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1353 bool ForClass,
1354 const llvm::Type *InterfaceTy) {
1355 std::vector<llvm::Constant*> Ivars, Ivar(3);
1356
1357 // When emitting the root class GCC emits ivar entries for the
1358 // actual class structure. It is not clear if we need to follow this
1359 // behavior; for now lets try and get away with not doing it. If so,
1360 // the cleanest solution would be to make up an ObjCInterfaceDecl
1361 // for the class.
1362 if (ForClass)
1363 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1364
1365 const llvm::StructLayout *Layout =
1366 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001367 ObjCInterfaceDecl *OID =
1368 const_cast<ObjCInterfaceDecl *>(ID->getClassInterface());
1369 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
1370 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
1371 RecordDecl::field_iterator ifield = RD->field_begin();
1372 while (countSuperClassIvars-- > 0)
1373 ++ifield;
1374 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1375 FieldDecl *Field = *ifield;
1376 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1377 getLLVMFieldNo(Field));
1378 if (Field->getIdentifier())
1379 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1380 else
1381 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001382 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001383 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001384 Ivar[1] = GetMethodVarType(TypeStr);
1385 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001386 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001387 }
1388
1389 // Return null for empty list.
1390 if (Ivars.empty())
1391 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1392
1393 std::vector<llvm::Constant*> Values(2);
1394 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1395 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1396 Ivars.size());
1397 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1398 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1399
1400 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1401 "\01L_OBJC_INSTANCE_VARIABLES_");
1402 llvm::GlobalVariable *GV =
1403 new llvm::GlobalVariable(Init->getType(), false,
1404 llvm::GlobalValue::InternalLinkage,
1405 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001406 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001407 &CGM.getModule());
1408 if (ForClass) {
1409 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1410 // FIXME: Why is this only here?
1411 GV->setAlignment(32);
1412 } else {
1413 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1414 }
1415 UsedGlobals.push_back(GV);
1416 return llvm::ConstantExpr::getBitCast(GV,
1417 ObjCTypes.IvarListPtrTy);
1418}
1419
1420/*
1421 struct objc_method {
1422 SEL method_name;
1423 char *method_types;
1424 void *method;
1425 };
1426
1427 struct objc_method_list {
1428 struct objc_method_list *obsolete;
1429 int count;
1430 struct objc_method methods_list[count];
1431 };
1432*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001433
1434/// GetMethodConstant - Return a struct objc_method constant for the
1435/// given method if it has been defined. The result is null if the
1436/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001437llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001438 // FIXME: Use DenseMap::lookup
1439 llvm::Function *Fn = MethodDefinitions[MD];
1440 if (!Fn)
1441 return 0;
1442
1443 std::vector<llvm::Constant*> Method(3);
1444 Method[0] =
1445 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1446 ObjCTypes.SelectorPtrTy);
1447 Method[1] = GetMethodVarType(MD);
1448 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1449 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1450}
1451
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001452llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1453 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001454 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001455 // Return null for empty list.
1456 if (Methods.empty())
1457 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1458
1459 std::vector<llvm::Constant*> Values(3);
1460 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1461 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1462 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1463 Methods.size());
1464 Values[2] = llvm::ConstantArray::get(AT, Methods);
1465 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1466
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001467 llvm::GlobalVariable *GV =
1468 new llvm::GlobalVariable(Init->getType(), false,
1469 llvm::GlobalValue::InternalLinkage,
1470 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001471 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001472 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001473 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 UsedGlobals.push_back(GV);
1475 return llvm::ConstantExpr::getBitCast(GV,
1476 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001477}
1478
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001479llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD,
1480 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001481 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001482 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001483
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001484 const llvm::FunctionType *MethodTy =
1485 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001486 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001487 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001488 llvm::GlobalValue::InternalLinkage,
1489 Name,
1490 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001491 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001492
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001493 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494}
1495
1496llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001497 // Abuse this interface function as a place to finalize.
1498 FinishModule();
1499
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001500 return NULL;
1501}
1502
Daniel Dunbar49f66022008-09-24 03:38:44 +00001503llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1504 return ObjCTypes.GetPropertyFn;
1505}
1506
1507llvm::Function *CGObjCMac::GetPropertySetFunction() {
1508 return ObjCTypes.SetPropertyFn;
1509}
1510
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001511llvm::Function *CGObjCMac::EnumerationMutationFunction()
1512{
1513 return ObjCTypes.EnumerationMutationFn;
1514}
1515
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001516/*
1517
1518Objective-C setjmp-longjmp (sjlj) Exception Handling
1519--
1520
1521The basic framework for a @try-catch-finally is as follows:
1522{
1523 objc_exception_data d;
1524 id _rethrow = null;
1525
1526 objc_exception_try_enter(&d);
1527 if (!setjmp(d.jmp_buf)) {
1528 ... try body ...
1529 } else {
1530 // exception path
1531 id _caught = objc_exception_extract(&d);
1532
1533 // enter new try scope for handlers
1534 if (!setjmp(d.jmp_buf)) {
1535 ... match exception and execute catch blocks ...
1536
1537 // fell off end, rethrow.
1538 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001539 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001540 } else {
1541 // exception in catch block
1542 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001543 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001544 }
1545 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001546 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001547
1548finally:
1549 // match either the initial try_enter or the catch try_enter,
1550 // depending on the path followed.
1551 objc_exception_try_exit(&d);
1552finally_no_exit:
1553 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001554 ... dispatch to finally destination ...
1555
1556finally_rethrow:
1557 objc_exception_throw(_rethrow);
1558
1559finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001560}
1561
1562This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001563uses _rethrow to determine if objc_exception_try_exit should be called
1564and if the object should be rethrown. This breaks in the face of
1565throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001566
1567We specialize this framework for a few particular circumstances:
1568
1569 - If there are no catch blocks, then we avoid emitting the second
1570 exception handling context.
1571
1572 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1573 e)) we avoid emitting the code to rethrow an uncaught exception.
1574
1575 - FIXME: If there is no @finally block we can do a few more
1576 simplifications.
1577
1578Rethrows and Jumps-Through-Finally
1579--
1580
1581Support for implicit rethrows and jumping through the finally block is
1582handled by storing the current exception-handling context in
1583ObjCEHStack.
1584
Daniel Dunbar898d5082008-09-30 01:06:03 +00001585In order to implement proper @finally semantics, we support one basic
1586mechanism for jumping through the finally block to an arbitrary
1587destination. Constructs which generate exits from a @try or @catch
1588block use this mechanism to implement the proper semantics by chaining
1589jumps, as necessary.
1590
1591This mechanism works like the one used for indirect goto: we
1592arbitrarily assign an ID to each destination and store the ID for the
1593destination in a variable prior to entering the finally block. At the
1594end of the finally block we simply create a switch to the proper
1595destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001596
1597Code gen for @synchronized(expr) stmt;
1598Effectively generating code for:
1599objc_sync_enter(expr);
1600@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001601*/
1602
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001603void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1604 const Stmt &S) {
1605 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001606 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001607 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1608 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1609 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1610 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001611 llvm::Value *DestCode =
1612 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1613
1614 // Generate jump code. Done here so we can directly add things to
1615 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001616 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001617 llvm::SwitchInst *FinallySwitch =
1618 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1619 FinallyEnd, 10, FinallyJump);
1620
1621 // Push an EH context entry, used for handling rethrows and jumps
1622 // through finally.
1623 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1624 FinallySwitch, DestCode);
1625 CGF.ObjCEHStack.push_back(&EHEntry);
1626
1627 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001628 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1629 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001630 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1631 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001632 if (!isTry) {
1633 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001634 llvm::Value *Arg = CGF.EmitScalarExpr(
1635 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1636 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1637 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001638 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001639
1640 // Enter a new try block and call setjmp.
1641 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1642 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1643 "jmpbufarray");
1644 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1645 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1646 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001647
Daniel Dunbar55e87422008-11-11 02:29:29 +00001648 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1649 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001650 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001651 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001652
1653 // Emit the @try block.
1654 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001655 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1656 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001657 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001658
1659 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001660 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001661
1662 // Retrieve the exception object. We may emit multiple blocks but
1663 // nothing can cross this so the value is already in SSA form.
1664 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1665 ExceptionData,
1666 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001667 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001668 if (!isTry)
1669 {
1670 CGF.Builder.CreateStore(Caught, RethrowPtr);
1671 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1672 }
1673 else if (const ObjCAtCatchStmt* CatchStmt =
1674 cast<ObjCAtTryStmt>(S).getCatchStmts())
1675 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001676 // Enter a new exception try block (in case a @catch block throws
1677 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001678 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001679
Anders Carlsson80f25672008-09-09 17:59:25 +00001680 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1681 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001682 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001683
Daniel Dunbar55e87422008-11-11 02:29:29 +00001684 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1685 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001686 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001687
1688 CGF.EmitBlock(CatchBlock);
1689
Daniel Dunbar55e40722008-09-27 07:03:52 +00001690 // Handle catch list. As a special case we check if everything is
1691 // matched and avoid generating code for falling off the end if
1692 // so.
1693 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001694 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001695 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001696
Anders Carlssondde0a942008-09-11 09:15:33 +00001697 const DeclStmt *CatchParam =
1698 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001699 const VarDecl *VD = 0;
1700 const PointerType *PT = 0;
1701
Anders Carlsson80f25672008-09-09 17:59:25 +00001702 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001703 if (!CatchParam) {
1704 AllMatched = true;
1705 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001706 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001707 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001708
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001709 // catch(id e) always matches.
1710 // FIXME: For the time being we also match id<X>; this should
1711 // be rejected by Sema instead.
1712 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1713 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001714 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001715 }
1716
Daniel Dunbar55e40722008-09-27 07:03:52 +00001717 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001718 if (CatchParam) {
1719 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001720 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001721 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001722 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001723
Anders Carlssondde0a942008-09-11 09:15:33 +00001724 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001725 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001726 break;
1727 }
1728
Daniel Dunbar129271a2008-09-27 07:36:24 +00001729 assert(PT && "Unexpected non-pointer type in @catch");
1730 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001731 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001732 assert(ObjCType && "Catch parameter must have Objective-C type!");
1733
1734 // Check if the @catch block matches the exception object.
1735 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1736
Anders Carlsson80f25672008-09-09 17:59:25 +00001737 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1738 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001739
Daniel Dunbar55e87422008-11-11 02:29:29 +00001740 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001741
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001742 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001743 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001744
1745 // Emit the @catch block.
1746 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001747 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001748 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001749
1750 llvm::Value *Tmp =
1751 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1752 "tmp");
1753 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001754
1755 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001756 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001757
1758 CGF.EmitBlock(NextCatchBlock);
1759 }
1760
Daniel Dunbar55e40722008-09-27 07:03:52 +00001761 if (!AllMatched) {
1762 // None of the handlers caught the exception, so store it to be
1763 // rethrown at the end of the @finally block.
1764 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001765 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001766 }
1767
1768 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001769 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001770 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1771 ExceptionData),
1772 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001773 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001774 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001775 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001776 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001777 }
1778
Daniel Dunbar898d5082008-09-30 01:06:03 +00001779 // Pop the exception-handling stack entry. It is important to do
1780 // this now, because the code in the @finally block is not in this
1781 // context.
1782 CGF.ObjCEHStack.pop_back();
1783
Anders Carlsson80f25672008-09-09 17:59:25 +00001784 // Emit the @finally block.
1785 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001786 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001787
1788 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001789 if (isTry) {
1790 if (const ObjCAtFinallyStmt* FinallyStmt =
1791 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1792 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1793 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001794 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001795 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001796 // For @synchronized, call objc_sync_enter(sync.expr)
1797 llvm::Value *Arg = CGF.EmitScalarExpr(
1798 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1799 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1800 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1801 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001802
Daniel Dunbar898d5082008-09-30 01:06:03 +00001803 CGF.EmitBlock(FinallyJump);
1804
1805 CGF.EmitBlock(FinallyRethrow);
1806 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1807 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001808 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001809
1810 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001811}
1812
1813void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001814 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001815 llvm::Value *ExceptionAsObject;
1816
1817 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1818 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1819 ExceptionAsObject =
1820 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1821 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001822 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001823 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001824 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001825 }
1826
1827 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00001828 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00001829
1830 // Clear the insertion point to indicate we are in unreachable code.
1831 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001832}
1833
Daniel Dunbar898d5082008-09-30 01:06:03 +00001834void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
1835 llvm::BasicBlock *Dst,
1836 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001837 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00001838 return;
1839
1840 // Find the destination code for this block. We always use 0 for the
1841 // fallthrough block (default destination).
1842 llvm::SwitchInst *SI = E->FinallySwitch;
1843 llvm::ConstantInt *ID;
1844 if (Dst == SI->getDefaultDest()) {
1845 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
1846 } else {
1847 ID = SI->findCaseDest(Dst);
1848 if (!ID) {
1849 // No code found, get a new unique one by just using the number
1850 // of switch successors.
1851 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
1852 SI->addCase(ID, Dst);
1853 }
1854 }
1855
1856 // Set the destination code and branch.
1857 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001858 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001859}
1860
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001861/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001862/// object: objc_read_weak (id *src)
1863///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001864llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001865 llvm::Value *AddrWeakObj)
1866{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001867 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001868 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001869 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001870 return read_weak;
1871}
1872
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001873/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
1874/// objc_assign_weak (id src, id *dst)
1875///
1876void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1877 llvm::Value *src, llvm::Value *dst)
1878{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001879 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1880 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001881 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
1882 src, dst, "weakassign");
1883 return;
1884}
1885
Fariborz Jahanian58626502008-11-19 00:59:10 +00001886/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
1887/// objc_assign_global (id src, id *dst)
1888///
1889void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1890 llvm::Value *src, llvm::Value *dst)
1891{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001892 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1893 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001894 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
1895 src, dst, "globalassign");
1896 return;
1897}
1898
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001899/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
1900/// objc_assign_ivar (id src, id *dst)
1901///
1902void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1903 llvm::Value *src, llvm::Value *dst)
1904{
1905 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1906 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
1907 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
1908 src, dst, "assignivar");
1909 return;
1910}
1911
Fariborz Jahanian58626502008-11-19 00:59:10 +00001912/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
1913/// objc_assign_strongCast (id src, id *dst)
1914///
1915void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1916 llvm::Value *src, llvm::Value *dst)
1917{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00001918 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
1919 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001920 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
1921 src, dst, "weakassign");
1922 return;
1923}
1924
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001925/* *** Private Interface *** */
1926
1927/// EmitImageInfo - Emit the image info marker used to encode some module
1928/// level information.
1929///
1930/// See: <rdr://4810609&4810587&4810587>
1931/// struct IMAGE_INFO {
1932/// unsigned version;
1933/// unsigned flags;
1934/// };
1935enum ImageInfoFlags {
1936 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1937 eImageInfo_GarbageCollected = (1 << 1),
1938 eImageInfo_GCOnly = (1 << 2)
1939};
1940
1941void CGObjCMac::EmitImageInfo() {
1942 unsigned version = 0; // Version is unused?
1943 unsigned flags = 0;
1944
1945 // FIXME: Fix and continue?
1946 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1947 flags |= eImageInfo_GarbageCollected;
1948 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1949 flags |= eImageInfo_GCOnly;
1950
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001951 // Emitted as int[2];
1952 llvm::Constant *values[2] = {
1953 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1954 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1955 };
1956 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001957 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001958 new llvm::GlobalVariable(AT, true,
1959 llvm::GlobalValue::InternalLinkage,
1960 llvm::ConstantArray::get(AT, values, 2),
1961 "\01L_OBJC_IMAGE_INFO",
1962 &CGM.getModule());
1963
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001964 if (ObjCABI == 1) {
1965 GV->setSection("__OBJC, __image_info,regular");
1966 } else {
1967 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1968 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001969
1970 UsedGlobals.push_back(GV);
1971}
1972
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001973
1974// struct objc_module {
1975// unsigned long version;
1976// unsigned long size;
1977// const char *name;
1978// Symtab symtab;
1979// };
1980
1981// FIXME: Get from somewhere
1982static const int ModuleVersion = 7;
1983
1984void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001985 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001986
1987 std::vector<llvm::Constant*> Values(4);
1988 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1989 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001990 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001991 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001992 Values[3] = EmitModuleSymbols();
1993
1994 llvm::GlobalVariable *GV =
1995 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1996 llvm::GlobalValue::InternalLinkage,
1997 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1998 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001999 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002000 &CGM.getModule());
2001 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2002 UsedGlobals.push_back(GV);
2003}
2004
2005llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002006 unsigned NumClasses = DefinedClasses.size();
2007 unsigned NumCategories = DefinedCategories.size();
2008
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002009 // Return null if no symbols were defined.
2010 if (!NumClasses && !NumCategories)
2011 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2012
2013 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002014 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2015 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2016 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2017 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2018
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002019 // The runtime expects exactly the list of defined classes followed
2020 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002021 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002022 for (unsigned i=0; i<NumClasses; i++)
2023 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2024 ObjCTypes.Int8PtrTy);
2025 for (unsigned i=0; i<NumCategories; i++)
2026 Symbols[NumClasses + i] =
2027 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2028 ObjCTypes.Int8PtrTy);
2029
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002030 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002031 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002032 NumClasses + NumCategories),
2033 Symbols);
2034
2035 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2036
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002037 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002038 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002039 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002040 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002041 "\01L_OBJC_SYMBOLS",
2042 &CGM.getModule());
2043 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2044 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2046}
2047
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002048llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002049 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002050 LazySymbols.insert(ID->getIdentifier());
2051
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002052 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2053
2054 if (!Entry) {
2055 llvm::Constant *Casted =
2056 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2057 ObjCTypes.ClassPtrTy);
2058 Entry =
2059 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2060 llvm::GlobalValue::InternalLinkage,
2061 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2062 &CGM.getModule());
2063 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2064 UsedGlobals.push_back(Entry);
2065 }
2066
2067 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002068}
2069
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002070llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002071 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2072
2073 if (!Entry) {
2074 llvm::Constant *Casted =
2075 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2076 ObjCTypes.SelectorPtrTy);
2077 Entry =
2078 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2079 llvm::GlobalValue::InternalLinkage,
2080 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2081 &CGM.getModule());
2082 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2083 UsedGlobals.push_back(Entry);
2084 }
2085
2086 return Builder.CreateLoad(Entry, false, "tmp");
2087}
2088
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002089llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
2090 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002091
2092 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002093 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002094 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002095 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002096 llvm::GlobalValue::InternalLinkage,
2097 C, "\01L_OBJC_CLASS_NAME_",
2098 &CGM.getModule());
2099 Entry->setSection("__TEXT,__cstring,cstring_literals");
2100 UsedGlobals.push_back(Entry);
2101 }
2102
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002103 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002104}
2105
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002106llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002107 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2108
2109 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002110 // FIXME: Avoid std::string copying.
2111 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002112 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002113 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002114 llvm::GlobalValue::InternalLinkage,
2115 C, "\01L_OBJC_METH_VAR_NAME_",
2116 &CGM.getModule());
2117 Entry->setSection("__TEXT,__cstring,cstring_literals");
2118 UsedGlobals.push_back(Entry);
2119 }
2120
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002121 return getConstantGEP(Entry, 0, 0);
2122}
2123
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002124// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002125llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002126 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2127}
2128
2129// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002130llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002131 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2132}
2133
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002134llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002135 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002136
2137 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002138 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002139 Entry =
2140 new llvm::GlobalVariable(C->getType(), false,
2141 llvm::GlobalValue::InternalLinkage,
2142 C, "\01L_OBJC_METH_VAR_TYPE_",
2143 &CGM.getModule());
2144 Entry->setSection("__TEXT,__cstring,cstring_literals");
2145 UsedGlobals.push_back(Entry);
2146 }
2147
2148 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002149}
2150
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002151// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002152llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002153 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002154 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2155 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002156 return GetMethodVarType(TypeStr);
2157}
2158
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002159// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002160llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002161 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2162
2163 if (!Entry) {
2164 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2165 Entry =
2166 new llvm::GlobalVariable(C->getType(), false,
2167 llvm::GlobalValue::InternalLinkage,
2168 C, "\01L_OBJC_PROP_NAME_ATTR_",
2169 &CGM.getModule());
2170 Entry->setSection("__TEXT,__cstring,cstring_literals");
2171 UsedGlobals.push_back(Entry);
2172 }
2173
2174 return getConstantGEP(Entry, 0, 0);
2175}
2176
2177// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002178// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002179llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002180 const Decl *Container) {
2181 std::string TypeStr;
2182 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002183 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2184}
2185
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002186void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2187 const ObjCContainerDecl *CD,
2188 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002189 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002190 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002191 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002192 assert (CD && "Missing container decl in GetNameForMethod");
2193 NameOut += CD->getNameAsString();
Chris Lattner077bf5e2008-11-24 03:33:13 +00002194 NameOut += ' ';
2195 NameOut += D->getSelector().getAsString();
2196 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002197}
2198
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002199void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002200 EmitModuleInfo();
2201
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002202 // Emit the dummy bodies for any protocols which were referenced but
2203 // never defined.
2204 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2205 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2206 if (i->second->hasInitializer())
2207 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002208
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002209 std::vector<llvm::Constant*> Values(5);
2210 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2211 Values[1] = GetClassName(i->first);
2212 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2213 Values[3] = Values[4] =
2214 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2215 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2216 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2217 Values));
2218 }
2219
2220 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002221 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002222 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002224 }
2225
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002226 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002227 llvm::GlobalValue *GV =
2228 new llvm::GlobalVariable(AT, false,
2229 llvm::GlobalValue::AppendingLinkage,
2230 llvm::ConstantArray::get(AT, Used),
2231 "llvm.used",
2232 &CGM.getModule());
2233
2234 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002235
2236 // Add assembler directives to add lazy undefined symbol references
2237 // for classes which are referenced but not defined. This is
2238 // important for correct linker interaction.
2239
2240 // FIXME: Uh, this isn't particularly portable.
2241 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002242
2243 if (!CGM.getModule().getModuleInlineAsm().empty())
2244 s << "\n";
2245
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002246 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2247 e = LazySymbols.end(); i != e; ++i) {
2248 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2249 }
2250 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2251 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002252 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002253 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2254 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002255
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002256 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002257}
2258
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002259CGObjCModernMac::CGObjCModernMac(CodeGen::CodeGenModule &cgm)
2260 : CGObjCCommonMac(cgm),
2261 ObjCTypes(cgm)
2262{
2263 ObjCABI = 2;
2264}
2265
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002266/* *** */
2267
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002268ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2269: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002270{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002271 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2272 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002273
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002274 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002275 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002276 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2278
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002279 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002280 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002281 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002282
2283 // FIXME: It would be nice to unify this with the opaque type, so
2284 // that the IR comes out a bit cleaner.
2285 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2286 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002287
2288 // I'm not sure I like this. The implicit coordination is a bit
2289 // gross. We should solve this in a reasonable fashion because this
2290 // is a pretty common task (match some runtime data structure with
2291 // an LLVM data structure).
2292
2293 // FIXME: This is leaked.
2294 // FIXME: Merge with rewriter code?
2295
2296 // struct _objc_super {
2297 // id self;
2298 // Class cls;
2299 // }
2300 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2301 SourceLocation(),
2302 &Ctx.Idents.get("_objc_super"));
2303 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2304 Ctx.getObjCIdType(), 0, false));
2305 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2306 Ctx.getObjCClassType(), 0, false));
2307 RD->completeDefinition(Ctx);
2308
2309 SuperCTy = Ctx.getTagDeclType(RD);
2310 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2311
2312 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002313 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2314
2315 // Property manipulation functions.
2316
2317 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2318 std::vector<const llvm::Type*> Params;
2319 Params.push_back(ObjectPtrTy);
2320 Params.push_back(SelectorPtrTy);
2321 Params.push_back(LongTy);
2322 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2323 GetPropertyFn =
2324 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2325 Params,
2326 false),
2327 "objc_getProperty");
2328
2329 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2330 Params.clear();
2331 Params.push_back(ObjectPtrTy);
2332 Params.push_back(SelectorPtrTy);
2333 Params.push_back(LongTy);
2334 Params.push_back(ObjectPtrTy);
2335 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2336 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2337 SetPropertyFn =
2338 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2339 Params,
2340 false),
2341 "objc_setProperty");
2342 // Enumeration mutation.
2343
2344 Params.clear();
2345 Params.push_back(ObjectPtrTy);
2346 EnumerationMutationFn =
2347 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2348 Params,
2349 false),
2350 "objc_enumerationMutation");
2351
2352 // gc's API
2353 // id objc_read_weak (id *)
2354 Params.clear();
2355 Params.push_back(PtrObjectPtrTy);
2356 GcReadWeakFn =
2357 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2358 Params,
2359 false),
2360 "objc_read_weak");
2361 // id objc_assign_weak (id, id *)
2362 Params.clear();
2363 Params.push_back(ObjectPtrTy);
2364 Params.push_back(PtrObjectPtrTy);
2365 GcAssignWeakFn =
2366 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2367 Params,
2368 false),
2369 "objc_assign_weak");
2370 GcAssignGlobalFn =
2371 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2372 Params,
2373 false),
2374 "objc_assign_global");
2375 GcAssignIvarFn =
2376 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2377 Params,
2378 false),
2379 "objc_assign_ivar");
2380 GcAssignStrongCastFn =
2381 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2382 Params,
2383 false),
2384 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002385}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002386
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002387ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2388 : ObjCCommonTypesHelper(cgm)
2389{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002390 // struct _objc_method_description {
2391 // SEL name;
2392 // char *types;
2393 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002394 MethodDescriptionTy =
2395 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002397 NULL);
2398 CGM.getModule().addTypeName("struct._objc_method_description",
2399 MethodDescriptionTy);
2400
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002401 // struct _objc_method_description_list {
2402 // int count;
2403 // struct _objc_method_description[1];
2404 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002405 MethodDescriptionListTy =
2406 llvm::StructType::get(IntTy,
2407 llvm::ArrayType::get(MethodDescriptionTy, 0),
2408 NULL);
2409 CGM.getModule().addTypeName("struct._objc_method_description_list",
2410 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002411
2412 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002413 MethodDescriptionListPtrTy =
2414 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2415
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002416 // struct _objc_property {
2417 // char *name;
2418 // char *attributes;
2419 // }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002420 PropertyTy = llvm::StructType::get(Int8PtrTy,
2421 Int8PtrTy,
2422 NULL);
2423 CGM.getModule().addTypeName("struct._objc_property",
2424 PropertyTy);
2425
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002426 // struct _objc_property_list {
2427 // uint32_t entsize; // sizeof(struct _objc_property)
2428 // uint32_t count_of_properties;
2429 // struct _objc_property prop_list[count_of_properties];
2430 // }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002431 PropertyListTy = llvm::StructType::get(IntTy,
2432 IntTy,
2433 llvm::ArrayType::get(PropertyTy, 0),
2434 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002435 CGM.getModule().addTypeName("struct._objc_property_list",
2436 PropertyListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002437 // struct _objc_property_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002438 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2439
2440 // Protocol description structures
2441
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002442 // struct _objc_protocol_extension {
2443 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2444 // struct _objc_method_description_list *optional_instance_methods;
2445 // struct _objc_method_description_list *optional_class_methods;
2446 // struct _objc_property_list *instance_properties;
2447 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002448 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002449 llvm::StructType::get(IntTy,
2450 MethodDescriptionListPtrTy,
2451 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002452 PropertyListPtrTy,
2453 NULL);
2454 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2455 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002456
2457 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002458 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2459
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002460 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002461
2462 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2463 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2464
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002465 const llvm::Type *T =
2466 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2467 LongTy,
2468 llvm::ArrayType::get(ProtocolTyHolder, 0),
2469 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002470 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2471
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002472 // struct _objc_protocol {
2473 // struct _objc_protocol_extension *isa;
2474 // char *protocol_name;
2475 // struct _objc_protocol **_objc_protocol_list;
2476 // struct _objc_method_description_list *instance_methods;
2477 // struct _objc_method_description_list *class_methods;
2478 // }
2479 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002480 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002481 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2482 MethodDescriptionListPtrTy,
2483 MethodDescriptionListPtrTy,
2484 NULL);
2485 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2486
2487 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2488 CGM.getModule().addTypeName("struct._objc_protocol_list",
2489 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002490 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002491 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2492
2493 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002494 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002495 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002496
2497 // Class description structures
2498
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002499 // struct _objc_ivar {
2500 // char *ivar_name;
2501 // char *ivar_type;
2502 // int ivar_offset;
2503 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002504 IvarTy = llvm::StructType::get(Int8PtrTy,
2505 Int8PtrTy,
2506 IntTy,
2507 NULL);
2508 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2509
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002510 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002511 IvarListTy = llvm::OpaqueType::get();
2512 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2513 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2514
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002515 // struct _objc_method {
2516 // SEL _cmd;
2517 // char *method_type;
2518 // char *_imp;
2519 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002520 MethodTy = llvm::StructType::get(SelectorPtrTy,
2521 Int8PtrTy,
2522 Int8PtrTy,
2523 NULL);
2524 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
2525
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002526 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002527 MethodListTy = llvm::OpaqueType::get();
2528 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2529 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2530
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002531 // struct _objc_cache *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002532 CacheTy = llvm::OpaqueType::get();
2533 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2534 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
2535
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002536 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002537 ClassExtensionTy =
2538 llvm::StructType::get(IntTy,
2539 Int8PtrTy,
2540 PropertyListPtrTy,
2541 NULL);
2542 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2543 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2544
2545 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2546
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002547 // struct _objc_class {
2548 // Class isa;
2549 // Class super_class;
2550 // char *name;
2551 // long version;
2552 // long info;
2553 // long instance_size;
2554 // struct _objc_ivar_list *ivars;
2555 // struct _objc_method_list *methods;
2556 // struct _objc_cache *cache;
2557 // struct _objc_protocol_list *protocols;
2558 // char *ivar_layout;
2559 // struct _objc_class_ext *ext;
2560 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002561 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2562 llvm::PointerType::getUnqual(ClassTyHolder),
2563 Int8PtrTy,
2564 LongTy,
2565 LongTy,
2566 LongTy,
2567 IvarListPtrTy,
2568 MethodListPtrTy,
2569 CachePtrTy,
2570 ProtocolListPtrTy,
2571 Int8PtrTy,
2572 ClassExtensionPtrTy,
2573 NULL);
2574 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2575
2576 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2577 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2578 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2579
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002580 // struct _objc_category {
2581 // char *category_name;
2582 // char *class_name;
2583 // struct _objc_method_list *instance_method;
2584 // struct _objc_method_list *class_method;
2585 // uint32_t size; // sizeof(struct _objc_category)
2586 // struct _objc_property_list *instance_properties;// category's @property
2587 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002588 CategoryTy = llvm::StructType::get(Int8PtrTy,
2589 Int8PtrTy,
2590 MethodListPtrTy,
2591 MethodListPtrTy,
2592 ProtocolListPtrTy,
2593 IntTy,
2594 PropertyListPtrTy,
2595 NULL);
2596 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2597
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002598 // Global metadata structures
2599
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002600 // struct _objc_symtab {
2601 // long sel_ref_cnt;
2602 // SEL *refs;
2603 // short cls_def_cnt;
2604 // short cat_def_cnt;
2605 // char *defs[cls_def_cnt + cat_def_cnt];
2606 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002607 SymtabTy = llvm::StructType::get(LongTy,
2608 SelectorPtrTy,
2609 ShortTy,
2610 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002611 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002612 NULL);
2613 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2614 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2615
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002616 // struct _objc_module {
2617 // long version;
2618 // long size; // sizeof(struct _objc_module)
2619 // char *name;
2620 // struct _objc_symtab* symtab;
2621 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002622 ModuleTy =
2623 llvm::StructType::get(LongTy,
2624 LongTy,
2625 Int8PtrTy,
2626 SymtabPtrTy,
2627 NULL);
2628 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002629
Daniel Dunbar49f66022008-09-24 03:38:44 +00002630 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002631
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002632 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002633 std::vector<const llvm::Type*> Params;
2634 Params.push_back(ObjectPtrTy);
2635 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002636 MessageSendFn =
2637 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2638 Params,
2639 true),
2640 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002641
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002642 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002643 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002644 Params.push_back(ObjectPtrTy);
2645 Params.push_back(SelectorPtrTy);
2646 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002647 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2648 Params,
2649 true),
2650 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002651
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002652 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002653 Params.clear();
2654 Params.push_back(ObjectPtrTy);
2655 Params.push_back(SelectorPtrTy);
2656 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002657 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002658 MessageSendFpretFn =
2659 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2660 Params,
2661 true),
2662 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002663
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002664 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002665 Params.clear();
2666 Params.push_back(SuperPtrTy);
2667 Params.push_back(SelectorPtrTy);
2668 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002669 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2670 Params,
2671 true),
2672 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002673
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002674 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2675 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002676 Params.clear();
2677 Params.push_back(Int8PtrTy);
2678 Params.push_back(SuperPtrTy);
2679 Params.push_back(SelectorPtrTy);
2680 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002681 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2682 Params,
2683 true),
2684 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002685
2686 // There is no objc_msgSendSuper_fpret? How can that work?
2687 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002688
Anders Carlsson124526b2008-09-09 10:10:21 +00002689 // FIXME: This is the size of the setjmp buffer and should be
2690 // target specific. 18 is what's used on 32-bit X86.
2691 uint64_t SetJmpBufferSize = 18;
2692
2693 // Exceptions
2694 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002695 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002696
2697 ExceptionDataTy =
2698 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2699 SetJmpBufferSize),
2700 StackPtrTy, NULL);
2701 CGM.getModule().addTypeName("struct._objc_exception_data",
2702 ExceptionDataTy);
2703
2704 Params.clear();
2705 Params.push_back(ObjectPtrTy);
2706 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002707 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2708 Params,
2709 false),
2710 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002711
2712 Params.clear();
2713 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2714 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002715 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2716 Params,
2717 false),
2718 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002719 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002720 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2721 Params,
2722 false),
2723 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002724 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002725 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2726 Params,
2727 false),
2728 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002729
2730 Params.clear();
2731 Params.push_back(ClassPtrTy);
2732 Params.push_back(ObjectPtrTy);
2733 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002734 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2735 Params,
2736 false),
2737 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002738
2739 // synchronized APIs
2740 // void objc_sync_enter (id)
2741 Params.clear();
2742 Params.push_back(ObjectPtrTy);
2743 SyncEnterFn =
2744 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2745 Params,
2746 false),
2747 "objc_sync_enter");
2748 // void objc_sync_exit (id)
2749 SyncExitFn =
2750 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2751 Params,
2752 false),
2753 "objc_sync_exit");
2754
Anders Carlsson124526b2008-09-09 10:10:21 +00002755
2756 Params.clear();
2757 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2758 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002759 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2760 Params,
2761 false),
2762 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002763
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002764}
2765
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002766ObjCModernTypesHelper::ObjCModernTypesHelper(CodeGen::CodeGenModule &cgm)
2767: ObjCCommonTypesHelper(cgm)
2768{
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002769}
2770
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002771/* *** */
2772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002773CodeGen::CGObjCRuntime *
2774CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002775 return new CGObjCMac(CGM);
2776}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002777
2778CodeGen::CGObjCRuntime *
2779CodeGen::CreateMacModernObjCRuntime(CodeGen::CodeGenModule &CGM) {
2780 return 0;
2781 // return new CGObjCModernMac(CGM);
2782}