blob: 26a11c90be5f7e736e51fef0def484cb7b13e121 [file] [log] [blame]
Daniel Dunbar8c85fac2008-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 Dunbar1be1df32008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbar75de89f2009-02-24 07:47:38 +000023#include "llvm/Intrinsics.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000024#include "llvm/Module.h"
Daniel Dunbar35b777f2008-10-29 22:36:39 +000025#include "llvm/ADT/DenseSet.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000027#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000028
29using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000030using namespace CodeGen;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000031
32namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000033
Daniel Dunbarfe131f02008-08-27 02:31:56 +000034 typedef std::vector<llvm::Constant*> ConstantVector;
35
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000036 // FIXME: We should find a nicer way to make the labels for
37 // metadata, string concatenation is lame.
38
Fariborz Jahanian48543f52009-01-21 22:04:16 +000039class ObjCCommonTypesHelper {
40protected:
41 CodeGen::CodeGenModule &CGM;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000042
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000043public:
Daniel Dunbarb050fa62008-08-21 04:36:09 +000044 const llvm::Type *ShortTy, *IntTy, *LongTy;
45 const llvm::Type *Int8PtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000046
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000047 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
48 const llvm::Type *ObjectPtrTy;
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +000049
50 /// PtrObjectPtrTy - LLVM type for id *
51 const llvm::Type *PtrObjectPtrTy;
52
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000053 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000054 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000055 /// ProtocolPtrTy - LLVM type for external protocol handles
56 /// (typeof(Protocol))
57 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000058
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000059 // SuperCTy - clang type for struct objc_super.
60 QualType SuperCTy;
61 // SuperPtrCTy - clang type for struct objc_super *.
62 QualType SuperPtrCTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000063
Daniel Dunbar15245e52008-08-23 04:28:29 +000064 /// SuperTy - LLVM type for struct objc_super.
65 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +000066 /// SuperPtrTy - LLVM type for struct objc_super *.
67 const llvm::Type *SuperPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000068
Fariborz Jahaniand0374812009-01-22 23:02:58 +000069 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
70 /// in GCC parlance).
71 const llvm::StructType *PropertyTy;
72
73 /// PropertyListTy - LLVM type for struct objc_property_list
74 /// (_prop_list_t in GCC parlance).
75 const llvm::StructType *PropertyListTy;
76 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
77 const llvm::Type *PropertyListPtrTy;
78
79 // MethodTy - LLVM type for struct objc_method.
80 const llvm::StructType *MethodTy;
81
Fariborz Jahanian781f2732009-01-23 01:46:23 +000082 /// CacheTy - LLVM type for struct objc_cache.
83 const llvm::Type *CacheTy;
84 /// CachePtrTy - LLVM type for struct objc_cache *.
85 const llvm::Type *CachePtrTy;
86
Fariborz Jahanian4b161702009-01-22 00:37:21 +000087 llvm::Function *GetPropertyFn, *SetPropertyFn;
88
89 llvm::Function *EnumerationMutationFn;
90
91 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
92 llvm::Function *GcReadWeakFn;
93
94 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
95 llvm::Function *GcAssignWeakFn;
96
97 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
98 llvm::Function *GcAssignGlobalFn;
99
100 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
101 llvm::Function *GcAssignIvarFn;
102
103 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
104 llvm::Function *GcAssignStrongCastFn;
Anders Carlsson1cf75362009-02-16 22:59:18 +0000105
106 /// ExceptionThrowFn - LLVM objc_exception_throw function.
107 llvm::Function *ExceptionThrowFn;
108
Daniel Dunbar34416d62009-02-24 01:43:46 +0000109 /// SyncEnterFn - LLVM object_sync_enter function.
110 llvm::Function *SyncEnterFn;
111
112 /// SyncExitFn - LLVM object_sync_exit function.
113 llvm::Function *SyncExitFn;
114
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000115 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
116 ~ObjCCommonTypesHelper(){}
117};
Daniel Dunbar15245e52008-08-23 04:28:29 +0000118
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000119/// ObjCTypesHelper - Helper class that encapsulates lazy
120/// construction of varies types used during ObjC generation.
121class ObjCTypesHelper : public ObjCCommonTypesHelper {
122private:
123
124 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
125 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
126 *MessageSendSuperFpretFn;
127
128public:
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000129 /// SymtabTy - LLVM type for struct objc_symtab.
130 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000131 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
132 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000133 /// ModuleTy - LLVM type for struct objc_module.
134 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000135
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000136 /// ProtocolTy - LLVM type for struct objc_protocol.
137 const llvm::StructType *ProtocolTy;
138 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
139 const llvm::Type *ProtocolPtrTy;
140 /// ProtocolExtensionTy - LLVM type for struct
141 /// objc_protocol_extension.
142 const llvm::StructType *ProtocolExtensionTy;
143 /// ProtocolExtensionTy - LLVM type for struct
144 /// objc_protocol_extension *.
145 const llvm::Type *ProtocolExtensionPtrTy;
146 /// MethodDescriptionTy - LLVM type for struct
147 /// objc_method_description.
148 const llvm::StructType *MethodDescriptionTy;
149 /// MethodDescriptionListTy - LLVM type for struct
150 /// objc_method_description_list.
151 const llvm::StructType *MethodDescriptionListTy;
152 /// MethodDescriptionListPtrTy - LLVM type for struct
153 /// objc_method_description_list *.
154 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000155 /// ProtocolListTy - LLVM type for struct objc_property_list.
156 const llvm::Type *ProtocolListTy;
157 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
158 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000159 /// CategoryTy - LLVM type for struct objc_category.
160 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000161 /// ClassTy - LLVM type for struct objc_class.
162 const llvm::StructType *ClassTy;
163 /// ClassPtrTy - LLVM type for struct objc_class *.
164 const llvm::Type *ClassPtrTy;
165 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
166 const llvm::StructType *ClassExtensionTy;
167 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
168 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000169 // IvarTy - LLVM type for struct objc_ivar.
170 const llvm::StructType *IvarTy;
171 /// IvarListTy - LLVM type for struct objc_ivar_list.
172 const llvm::Type *IvarListTy;
173 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
174 const llvm::Type *IvarListPtrTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000175 /// MethodListTy - LLVM type for struct objc_method_list.
176 const llvm::Type *MethodListTy;
177 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
178 const llvm::Type *MethodListPtrTy;
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000179
180 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
181 const llvm::Type *ExceptionDataTy;
182
Anders Carlsson9acb0a42008-09-09 10:10:21 +0000183 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
184 llvm::Function *ExceptionTryEnterFn;
185
186 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
187 llvm::Function *ExceptionTryExitFn;
188
189 /// ExceptionExtractFn - LLVM objc_exception_extract function.
190 llvm::Function *ExceptionExtractFn;
191
192 /// ExceptionMatchFn - LLVM objc_exception_match function.
193 llvm::Function *ExceptionMatchFn;
194
195 /// SetJmpFn - LLVM _setjmp function.
196 llvm::Function *SetJmpFn;
Chris Lattnerdd978702008-11-15 21:26:17 +0000197
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000198public:
199 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000200 ~ObjCTypesHelper() {}
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000201
202
203 llvm::Function *getSendFn(bool IsSuper) {
204 return IsSuper ? MessageSendSuperFn : MessageSendFn;
205 }
206
207 llvm::Function *getSendStretFn(bool IsSuper) {
208 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
209 }
210
211 llvm::Function *getSendFpretFn(bool IsSuper) {
212 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
213 }
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000214};
215
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000216/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000217/// modern abi
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000218class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000219public:
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000220 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
221 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
222 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
223 *MessageSendSuper2StretFixupFn;
224
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000225 // MethodListnfABITy - LLVM for struct _method_list_t
226 const llvm::StructType *MethodListnfABITy;
227
228 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
229 const llvm::Type *MethodListnfABIPtrTy;
230
231 // ProtocolnfABITy = LLVM for struct _protocol_t
232 const llvm::StructType *ProtocolnfABITy;
233
Daniel Dunbar1f42bb02009-02-15 07:36:20 +0000234 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
235 const llvm::Type *ProtocolnfABIPtrTy;
236
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000237 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
238 const llvm::StructType *ProtocolListnfABITy;
239
240 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
241 const llvm::Type *ProtocolListnfABIPtrTy;
242
243 // ClassnfABITy - LLVM for struct _class_t
244 const llvm::StructType *ClassnfABITy;
245
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000246 // ClassnfABIPtrTy - LLVM for struct _class_t*
247 const llvm::Type *ClassnfABIPtrTy;
248
Fariborz Jahanian781f2732009-01-23 01:46:23 +0000249 // IvarnfABITy - LLVM for struct _ivar_t
250 const llvm::StructType *IvarnfABITy;
251
252 // IvarListnfABITy - LLVM for struct _ivar_list_t
253 const llvm::StructType *IvarListnfABITy;
254
255 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
256 const llvm::Type *IvarListnfABIPtrTy;
257
258 // ClassRonfABITy - LLVM for struct _class_ro_t
259 const llvm::StructType *ClassRonfABITy;
260
261 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
262 const llvm::Type *ImpnfABITy;
263
264 // CategorynfABITy - LLVM for struct _category_t
265 const llvm::StructType *CategorynfABITy;
266
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000267 // New types for nonfragile abi messaging.
268
269 // MessageRefTy - LLVM for:
270 // struct _message_ref_t {
271 // IMP messenger;
272 // SEL name;
273 // };
274 const llvm::StructType *MessageRefTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000275 // MessageRefCTy - clang type for struct _message_ref_t
276 QualType MessageRefCTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000277
278 // MessageRefPtrTy - LLVM for struct _message_ref_t*
279 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000280 // MessageRefCPtrTy - clang type for struct _message_ref_t*
281 QualType MessageRefCPtrTy;
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000282
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +0000283 // MessengerTy - Type of the messenger (shown as IMP above)
284 const llvm::FunctionType *MessengerTy;
285
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +0000286 // SuperMessageRefTy - LLVM for:
287 // struct _super_message_ref_t {
288 // SUPER_IMP messenger;
289 // SEL name;
290 // };
291 const llvm::StructType *SuperMessageRefTy;
292
293 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
294 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000295
296 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
297 /// exception personality function.
298 llvm::Value *EHPersonalityPtr;
299
300 llvm::Function *UnwindResumeOrRethrowFn;
301
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000302 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
303 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000304};
305
306class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
307protected:
308 CodeGen::CodeGenModule &CGM;
309 // FIXME! May not be needing this after all.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000310 unsigned ObjCABI;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000311
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000312 /// LazySymbols - Symbols to generate a lazy reference for. See
313 /// DefinedSymbols and FinishModule().
314 std::set<IdentifierInfo*> LazySymbols;
315
316 /// DefinedSymbols - External symbols which are defined by this
317 /// module. The symbols in this list and LazySymbols are used to add
318 /// special linker symbols which ensure that Objective-C modules are
319 /// linked properly.
320 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000321
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000322 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000323 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000324
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000325 /// MethodVarNames - uniqued method variable names.
326 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000327
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000328 /// MethodVarTypes - uniqued method type signatures. We have to use
329 /// a StringMap here because have no other unique reference.
330 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000331
Daniel Dunbar12996f52008-08-26 21:51:14 +0000332 /// MethodDefinitions - map of methods which have been defined in
333 /// this translation unit.
334 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000335
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000336 /// PropertyNames - uniqued method variable names.
337 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000338
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000339 /// ClassReferences - uniqued class references.
340 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000341
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000342 /// SelectorReferences - uniqued selector references.
343 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000344
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000345 /// Protocols - Protocols for which an objc_protocol structure has
346 /// been emitted. Forward declarations are handled by creating an
347 /// empty structure whose initializer is filled in when/if defined.
348 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000349
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000350 /// DefinedProtocols - Protocols which have actually been
351 /// defined. We should not need this, see FIXME in GenerateProtocol.
352 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000353
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000354 /// DefinedClasses - List of defined classes.
355 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000356
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000357 /// DefinedCategories - List of defined categories.
358 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000359
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000360 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000361 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000362 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000363
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +0000364 /// GetNameForMethod - Return a name for the given method.
365 /// \param[out] NameOut - The return value.
366 void GetNameForMethod(const ObjCMethodDecl *OMD,
367 const ObjCContainerDecl *CD,
368 std::string &NameOut);
369
370 /// GetMethodVarName - Return a unique constant for the given
371 /// selector's name. The return value has type char *.
372 llvm::Constant *GetMethodVarName(Selector Sel);
373 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
374 llvm::Constant *GetMethodVarName(const std::string &Name);
375
376 /// GetMethodVarType - Return a unique constant for the given
377 /// selector's name. The return value has type char *.
378
379 // FIXME: This is a horrible name.
380 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
381 llvm::Constant *GetMethodVarType(const std::string &Name);
382
383 /// GetPropertyName - Return a unique constant for the given
384 /// name. The return value has type char *.
385 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
386
387 // FIXME: This can be dropped once string functions are unified.
388 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
389 const Decl *Container);
390
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000391 /// GetClassName - Return a unique constant for the given selector's
392 /// name. The return value has type char *.
393 llvm::Constant *GetClassName(IdentifierInfo *Ident);
394
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +0000395 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
396 RecordDecl::field_iterator &FIV,
397 RecordDecl::field_iterator &PIV);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +0000398 /// EmitPropertyList - Emit the given property list. The return
399 /// value has type PropertyListPtrTy.
400 llvm::Constant *EmitPropertyList(const std::string &Name,
401 const Decl *Container,
402 const ObjCContainerDecl *OCD,
403 const ObjCCommonTypesHelper &ObjCTypes);
404
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000405 /// GetProtocolRef - Return a reference to the internal protocol
406 /// description, creating an empty one if it has not been
407 /// defined. The return value has type ProtocolPtrTy.
408 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
409
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000410public:
411 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
412 { }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000413
414 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000415
416 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
417 const ObjCContainerDecl *CD=0);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000418
419 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
420
421 /// GetOrEmitProtocol - Get the protocol object for the given
422 /// declaration, emitting it if necessary. The return value has type
423 /// ProtocolPtrTy.
424 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
425
426 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
427 /// object for the given declaration, emitting it if needed. These
428 /// forward references will be filled in with empty bodies if no
429 /// definition is seen. The return value has type ProtocolPtrTy.
430 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000431};
432
433class CGObjCMac : public CGObjCCommonMac {
434private:
435 ObjCTypesHelper ObjCTypes;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000436 /// EmitImageInfo - Emit the image info marker used to encode some module
437 /// level information.
438 void EmitImageInfo();
439
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000440 /// EmitModuleInfo - Another marker encoding module level
441 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000442 void EmitModuleInfo();
443
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000444 /// EmitModuleSymols - Emit module symbols, the list of defined
445 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000446 llvm::Constant *EmitModuleSymbols();
447
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000448 /// FinishModule - Write out global data structures at the end of
449 /// processing a translation unit.
450 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000451
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000452 /// EmitClassExtension - Generate the class extension structure used
453 /// to store the weak ivar layout and properties. The return value
454 /// has type ClassExtensionPtrTy.
455 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
456
457 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
458 /// for the given class.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000459 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000460 const ObjCInterfaceDecl *ID);
461
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000462 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000463 QualType ResultType,
464 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000465 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000466 QualType Arg0Ty,
467 bool IsSuper,
468 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000469
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000470 /// EmitIvarList - Emit the ivar list for the given
471 /// implementation. If ForClass is true the list of class ivars
472 /// (i.e. metaclass ivars) is emitted, otherwise the list of
473 /// interface ivars will be emitted. The return value has type
474 /// IvarListPtrTy.
475 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +0000476 bool ForClass);
477
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000478 /// EmitMetaClass - Emit a forward reference to the class structure
479 /// for the metaclass of the given interface. The return value has
480 /// type ClassPtrTy.
481 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
482
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000483 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000484 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000485 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
486 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +0000487 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000488 const ConstantVector &Methods);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000489
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000490 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000491
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000492 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000493
494 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000495 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000496 llvm::Constant *EmitMethodList(const std::string &Name,
497 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000498 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000499
500 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000501 /// method declarations.
502 /// - TypeName: The name for the type containing the methods.
503 /// - IsProtocol: True iff these methods are for a protocol.
504 /// - ClassMethds: True iff these are class methods.
505 /// - Required: When true, only "required" methods are
506 /// listed. Similarly, when false only "optional" methods are
507 /// listed. For classes this should always be true.
508 /// - begin, end: The method list to output.
509 ///
510 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000511 llvm::Constant *EmitMethodDescList(const std::string &Name,
512 const char *Section,
513 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000514
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000515 /// GetOrEmitProtocol - Get the protocol object for the given
516 /// declaration, emitting it if necessary. The return value has type
517 /// ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000518 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000519
520 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
521 /// object for the given declaration, emitting it if needed. These
522 /// forward references will be filled in with empty bodies if no
523 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000524 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000525
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000526 /// EmitProtocolExtension - Generate the protocol extension
527 /// structure used to store optional instance and class methods, and
528 /// protocol properties. The return value has type
529 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000530 llvm::Constant *
531 EmitProtocolExtension(const ObjCProtocolDecl *PD,
532 const ConstantVector &OptInstanceMethods,
533 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000534
535 /// EmitProtocolList - Generate the list of referenced
536 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000537 llvm::Constant *EmitProtocolList(const std::string &Name,
538 ObjCProtocolDecl::protocol_iterator begin,
539 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000540
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000541 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
542 /// for the given selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000543 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000544
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000545 public:
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000546 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000547
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000548 virtual llvm::Function *ModuleInitFunction();
549
Daniel Dunbara04840b2008-08-23 03:46:30 +0000550 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000551 QualType ResultType,
552 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000553 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000554 bool IsClassMessage,
555 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000556
Daniel Dunbara04840b2008-08-23 03:46:30 +0000557 virtual CodeGen::RValue
558 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000559 QualType ResultType,
560 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000561 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000562 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000563 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000564 bool IsClassMessage,
565 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +0000566
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000567 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000568 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000569
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000570 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000571
Daniel Dunbarac93e472008-08-15 22:20:32 +0000572 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000573
Daniel Dunbarac93e472008-08-15 22:20:32 +0000574 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000575
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000576 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000577 const ObjCProtocolDecl *PD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000578
Daniel Dunbarf7103722008-09-24 03:38:44 +0000579 virtual llvm::Function *GetPropertyGetFunction();
580 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000581 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000582
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000583 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
584 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000585 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
586 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000587 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000588 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000589 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
590 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000591 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
592 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000593 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
594 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000595 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
596 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +0000597
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000598 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
599 QualType ObjectTy,
600 llvm::Value *BaseValue,
601 const ObjCIvarDecl *Ivar,
602 const FieldDecl *Field,
603 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000604 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
605 ObjCInterfaceDecl *Interface,
606 const ObjCIvarDecl *Ivar);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000607};
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000608
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000609class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000610private:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000611 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000612 llvm::GlobalVariable* ObjCEmptyCacheVar;
613 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanianf3a44012009-02-06 20:09:23 +0000614 /// MetaClassReferences - uniqued meta class references.
615 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000616
617 /// FinishNonFragileABIModule - Write out global data structures at the end of
618 /// processing a translation unit.
619 void FinishNonFragileABIModule();
620
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000621 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
622 unsigned InstanceStart,
623 unsigned InstanceSize,
624 const ObjCImplementationDecl *ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +0000625 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
626 llvm::Constant *IsAGV,
627 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +0000628 llvm::Constant *ClassRoGV,
629 bool HiddenVisibility);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000630
631 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
632
Fariborz Jahanian151747b2009-01-30 00:46:37 +0000633 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
634
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000635 /// EmitMethodList - Emit the method list for the given
636 /// implementation. The return value has type MethodListnfABITy.
637 llvm::Constant *EmitMethodList(const std::string &Name,
638 const char *Section,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +0000639 const ConstantVector &Methods);
640 /// EmitIvarList - Emit the ivar list for the given
641 /// implementation. If ForClass is true the list of class ivars
642 /// (i.e. metaclass ivars) is emitted, otherwise the list of
643 /// interface ivars will be emitted. The return value has type
644 /// IvarListnfABIPtrTy.
645 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000646
Fariborz Jahaniancc00f922009-02-10 20:21:06 +0000647 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +0000648 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +0000649 unsigned long int offset);
650
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000651 /// GetOrEmitProtocol - Get the protocol object for the given
652 /// declaration, emitting it if necessary. The return value has type
653 /// ProtocolPtrTy.
654 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
655
656 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
657 /// object for the given declaration, emitting it if needed. These
658 /// forward references will be filled in with empty bodies if no
659 /// definition is seen. The return value has type ProtocolPtrTy.
660 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
661
662 /// EmitProtocolList - Generate the list of referenced
663 /// protocols. The return value has type ProtocolListPtrTy.
664 llvm::Constant *EmitProtocolList(const std::string &Name,
665 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian7e881162009-02-04 00:22:57 +0000666 ObjCProtocolDecl::protocol_iterator end);
667
668 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
669 QualType ResultType,
670 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +0000671 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +0000672 QualType Arg0Ty,
673 bool IsSuper,
674 const CallArgList &CallArgs);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000675
Fariborz Jahanian917c0402009-02-05 20:41:40 +0000676 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
677 /// for the given class.
678 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +0000679 const ObjCInterfaceDecl *ID,
680 bool IsSuper = false);
681
682 /// EmitMetaClassRef - Return a Value * of the address of _class_t
683 /// meta-data
684 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
685 const ObjCInterfaceDecl *ID);
686
Fariborz Jahaniancc00f922009-02-10 20:21:06 +0000687 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
688 /// the given ivar.
689 ///
690 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahaniana09a5142009-02-12 18:51:23 +0000691 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +0000692 const ObjCIvarDecl *Ivar);
Fariborz Jahanian917c0402009-02-05 20:41:40 +0000693
Fariborz Jahanianebb82c62009-02-11 20:51:17 +0000694 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
695 /// for the given selector.
696 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
697
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000698public:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000699 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000700 // FIXME. All stubs for now!
701 virtual llvm::Function *ModuleInitFunction();
702
703 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
704 QualType ResultType,
705 Selector Sel,
706 llvm::Value *Receiver,
707 bool IsClassMessage,
Fariborz Jahanian7e881162009-02-04 00:22:57 +0000708 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000709
710 virtual CodeGen::RValue
711 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
712 QualType ResultType,
713 Selector Sel,
714 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000715 bool isCategoryImpl,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000716 llvm::Value *Receiver,
717 bool IsClassMessage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +0000718 const CallArgList &CallArgs);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000719
720 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian917c0402009-02-05 20:41:40 +0000721 const ObjCInterfaceDecl *ID);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000722
723 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanianebb82c62009-02-11 20:51:17 +0000724 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000725
Fariborz Jahanianfe49a092009-01-26 18:32:24 +0000726 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000727
728 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000729 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +0000730 const ObjCProtocolDecl *PD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000731
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000732 virtual llvm::Function *GetPropertyGetFunction(){
733 return ObjCTypes.GetPropertyFn;
734 }
735 virtual llvm::Function *GetPropertySetFunction(){
736 return ObjCTypes.SetPropertyFn;
737 }
Daniel Dunbar978d2be2009-02-16 18:48:45 +0000738 virtual llvm::Function *EnumerationMutationFunction() {
739 return ObjCTypes.EnumerationMutationFn;
740 }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000741
742 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar75de89f2009-02-24 07:47:38 +0000743 const Stmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000744 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson1cf75362009-02-16 22:59:18 +0000745 const ObjCAtThrowStmt &S);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000746 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000747 llvm::Value *AddrWeakObj);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000748 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000749 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000750 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000751 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000752 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000753 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000754 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian5eefb432009-02-16 22:52:32 +0000755 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000756 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
757 QualType ObjectTy,
758 llvm::Value *BaseValue,
759 const ObjCIvarDecl *Ivar,
760 const FieldDecl *Field,
761 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000762 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
763 ObjCInterfaceDecl *Interface,
764 const ObjCIvarDecl *Ivar);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000765};
766
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000767} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000768
769/* *** Helper Functions *** */
770
771/// getConstantGEP() - Help routine to construct simple GEPs.
772static llvm::Constant *getConstantGEP(llvm::Constant *C,
773 unsigned idx0,
774 unsigned idx1) {
775 llvm::Value *Idxs[] = {
776 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
777 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
778 };
779 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
780}
781
782/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000783
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000784CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
785 ObjCTypes(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000786{
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000787 ObjCABI = 1;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000788 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000789}
790
Daniel Dunbar434627a2008-08-16 00:25:02 +0000791/// GetClass - Return a reference to the class for the given interface
792/// decl.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000793llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000794 const ObjCInterfaceDecl *ID) {
795 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000796}
797
798/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000799llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000800 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000801}
802
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000803/// Generate a constant CFString object.
804/*
805 struct __builtin_CFString {
806 const int *isa; // point to __CFConstantStringClassReference
807 int flags;
808 const char *str;
809 long length;
810 };
811*/
812
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000813llvm::Constant *CGObjCCommonMac::GenerateConstantString(
814 const std::string &String) {
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000815 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000816}
817
818/// Generates a message send where the super is the receiver. This is
819/// a message send to self with special delivery semantics indicating
820/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000821CodeGen::RValue
822CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000823 QualType ResultType,
824 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000825 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000826 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000827 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000828 bool IsClassMessage,
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000829 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +0000830 // Create and init a super structure; this is a (receiver, class)
831 // pair we will pass to objc_msgSendSuper.
832 llvm::Value *ObjCSuper =
833 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
834 llvm::Value *ReceiverAsObject =
835 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
836 CGF.Builder.CreateStore(ReceiverAsObject,
837 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +0000838
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000839 // If this is a class message the metaclass is passed as the target.
840 llvm::Value *Target;
841 if (IsClassMessage) {
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000842 if (isCategoryImpl) {
843 // Message sent to 'super' in a class method defined in a category
844 // implementation requires an odd treatment.
845 // If we are in a class method, we must retrieve the
846 // _metaclass_ for the current class, pointed at by
847 // the class's "isa" pointer. The following assumes that
848 // isa" is the first ivar in a class (which it must be).
849 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
850 Target = CGF.Builder.CreateStructGEP(Target, 0);
851 Target = CGF.Builder.CreateLoad(Target);
852 }
853 else {
854 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
855 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
856 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
857 Target = Super;
858 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000859 } else {
860 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
861 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000862 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
863 // and ObjCTypes types.
864 const llvm::Type *ClassTy =
865 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000866 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000867 CGF.Builder.CreateStore(Target,
868 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
869
Daniel Dunbardd851282008-08-30 05:35:15 +0000870 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000871 ObjCSuper, ObjCTypes.SuperPtrCTy,
872 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000873}
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000874
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000875/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000876CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000877 QualType ResultType,
878 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000879 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000880 bool IsClassMessage,
881 const CallArgList &CallArgs) {
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000882 llvm::Value *Arg0 =
883 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbardd851282008-08-30 05:35:15 +0000884 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000885 Arg0, CGF.getContext().getObjCIdType(),
886 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000887}
888
889CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000890 QualType ResultType,
891 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000892 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000893 QualType Arg0Ty,
894 bool IsSuper,
895 const CallArgList &CallArgs) {
896 CallArgList ActualArgs;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000897 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
898 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
899 Sel)),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000900 CGF.getContext().getObjCSelType()));
901 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000902
Daniel Dunbar34bda882009-02-02 23:23:47 +0000903 CodeGenTypes &Types = CGM.getTypes();
904 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
905 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000906
907 llvm::Constant *Fn;
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000908 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000909 Fn = ObjCTypes.getSendStretFn(IsSuper);
910 } else if (ResultType->isFloatingType()) {
911 // FIXME: Sadly, this is wrong. This actually depends on the
912 // architecture. This happens to be right for x86-32 though.
913 Fn = ObjCTypes.getSendFpretFn(IsSuper);
914 } else {
915 Fn = ObjCTypes.getSendFn(IsSuper);
916 }
Daniel Dunbara9976a22008-09-10 07:00:50 +0000917 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar6ee022b2009-02-02 22:03:45 +0000918 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000919}
920
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000921llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000922 const ObjCProtocolDecl *PD) {
Daniel Dunbarb3518152008-09-04 04:33:15 +0000923 // FIXME: I don't understand why gcc generates this, or where it is
924 // resolved. Investigate. Its also wasteful to look this up over and
925 // over.
926 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
927
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000928 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
929 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000930}
931
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000932void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000933 // FIXME: We shouldn't need this, the protocol decl should contain
934 // enough information to tell us whether this was a declaration or a
935 // definition.
936 DefinedProtocols.insert(PD->getIdentifier());
937
938 // If we have generated a forward reference to this protocol, emit
939 // it now. Otherwise do nothing, the protocol objects are lazily
940 // emitted.
941 if (Protocols.count(PD->getIdentifier()))
942 GetOrEmitProtocol(PD);
943}
944
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000945llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000946 if (DefinedProtocols.count(PD->getIdentifier()))
947 return GetOrEmitProtocol(PD);
948 return GetOrEmitProtocolRef(PD);
949}
950
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000951/*
952 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
953 struct _objc_protocol {
954 struct _objc_protocol_extension *isa;
955 char *protocol_name;
956 struct _objc_protocol_list *protocol_list;
957 struct _objc__method_prototype_list *instance_methods;
958 struct _objc__method_prototype_list *class_methods
959 };
960
961 See EmitProtocolExtension().
962*/
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000963llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
964 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
965
966 // Early exit if a defining object has already been generated.
967 if (Entry && Entry->hasInitializer())
968 return Entry;
969
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000970 // FIXME: I don't understand why gcc generates this, or where it is
971 // resolved. Investigate. Its also wasteful to look this up over and
972 // over.
973 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
974
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000975 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000976
977 // Construct method lists.
978 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
979 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
980 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
981 e = PD->instmeth_end(); i != e; ++i) {
982 ObjCMethodDecl *MD = *i;
983 llvm::Constant *C = GetMethodDescriptionConstant(MD);
984 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
985 OptInstanceMethods.push_back(C);
986 } else {
987 InstanceMethods.push_back(C);
988 }
989 }
990
991 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
992 e = PD->classmeth_end(); i != e; ++i) {
993 ObjCMethodDecl *MD = *i;
994 llvm::Constant *C = GetMethodDescriptionConstant(MD);
995 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
996 OptClassMethods.push_back(C);
997 } else {
998 ClassMethods.push_back(C);
999 }
1000 }
1001
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001002 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001003 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001004 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001005 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001006 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001007 PD->protocol_begin(),
1008 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001009 Values[3] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001010 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1011 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001012 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1013 InstanceMethods);
1014 Values[4] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001015 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1016 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001017 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1018 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001019 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1020 Values);
1021
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001022 if (Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001023 // Already created, fix the linkage and update the initializer.
1024 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001025 Entry->setInitializer(Init);
1026 } else {
1027 Entry =
1028 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1029 llvm::GlobalValue::InternalLinkage,
1030 Init,
1031 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1032 &CGM.getModule());
1033 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1034 UsedGlobals.push_back(Entry);
1035 // FIXME: Is this necessary? Why only for protocol?
1036 Entry->setAlignment(4);
1037 }
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001038
1039 return Entry;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001040}
1041
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001042llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001043 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1044
1045 if (!Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001046 // We use the initializer as a marker of whether this is a forward
1047 // reference or not. At module finalization we add the empty
1048 // contents for protocols which were referenced but never defined.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001049 Entry =
1050 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001051 llvm::GlobalValue::ExternalLinkage,
1052 0,
Chris Lattner271d4c22008-11-24 05:29:24 +00001053 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001054 &CGM.getModule());
1055 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1056 UsedGlobals.push_back(Entry);
1057 // FIXME: Is this necessary? Why only for protocol?
1058 Entry->setAlignment(4);
1059 }
1060
1061 return Entry;
1062}
1063
1064/*
1065 struct _objc_protocol_extension {
1066 uint32_t size;
1067 struct objc_method_description_list *optional_instance_methods;
1068 struct objc_method_description_list *optional_class_methods;
1069 struct objc_property_list *instance_properties;
1070 };
1071*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001072llvm::Constant *
1073CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1074 const ConstantVector &OptInstanceMethods,
1075 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001076 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001077 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001078 std::vector<llvm::Constant*> Values(4);
1079 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001080 Values[1] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001081 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1082 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001083 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1084 OptInstanceMethods);
1085 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001086 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1087 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001088 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1089 OptClassMethods);
Chris Lattner271d4c22008-11-24 05:29:24 +00001090 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1091 PD->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001092 0, PD, ObjCTypes);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001093
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001094 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001095 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1096 Values[3]->isNullValue())
1097 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1098
1099 llvm::Constant *Init =
1100 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1101 llvm::GlobalVariable *GV =
1102 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1103 llvm::GlobalValue::InternalLinkage,
1104 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001105 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001106 &CGM.getModule());
1107 // No special section, but goes in llvm.used
1108 UsedGlobals.push_back(GV);
1109
1110 return GV;
1111}
1112
1113/*
1114 struct objc_protocol_list {
1115 struct objc_protocol_list *next;
1116 long count;
1117 Protocol *list[];
1118 };
1119*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001120llvm::Constant *
1121CGObjCMac::EmitProtocolList(const std::string &Name,
1122 ObjCProtocolDecl::protocol_iterator begin,
1123 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001124 std::vector<llvm::Constant*> ProtocolRefs;
1125
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001126 for (; begin != end; ++begin)
1127 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001128
1129 // Just return null for empty protocol lists
1130 if (ProtocolRefs.empty())
1131 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1132
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001133 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001134 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1135
1136 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001137 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001138 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1139 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1140 Values[2] =
1141 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1142 ProtocolRefs.size()),
1143 ProtocolRefs);
1144
1145 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1146 llvm::GlobalVariable *GV =
1147 new llvm::GlobalVariable(Init->getType(), false,
1148 llvm::GlobalValue::InternalLinkage,
1149 Init,
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001150 Name,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001151 &CGM.getModule());
1152 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1153 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1154}
1155
1156/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001157 struct _objc_property {
1158 const char * const name;
1159 const char * const attributes;
1160 };
1161
1162 struct _objc_property_list {
1163 uint32_t entsize; // sizeof (struct _objc_property)
1164 uint32_t prop_count;
1165 struct _objc_property[prop_count];
1166 };
1167*/
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001168llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1169 const Decl *Container,
1170 const ObjCContainerDecl *OCD,
1171 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001172 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroffdcf1e842009-01-11 12:47:58 +00001173 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1174 E = OCD->prop_end(); I != E; ++I) {
1175 const ObjCPropertyDecl *PD = *I;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001176 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001177 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001178 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1179 Prop));
1180 }
1181
1182 // Return null for empty list.
1183 if (Properties.empty())
1184 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1185
1186 unsigned PropertySize =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001187 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001188 std::vector<llvm::Constant*> Values(3);
1189 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1190 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1191 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1192 Properties.size());
1193 Values[2] = llvm::ConstantArray::get(AT, Properties);
1194 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1195
1196 llvm::GlobalVariable *GV =
1197 new llvm::GlobalVariable(Init->getType(), false,
1198 llvm::GlobalValue::InternalLinkage,
1199 Init,
1200 Name,
1201 &CGM.getModule());
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001202 if (ObjCABI == 2)
1203 GV->setSection("__DATA, __objc_const");
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001204 // No special section on property lists?
1205 UsedGlobals.push_back(GV);
1206 return llvm::ConstantExpr::getBitCast(GV,
1207 ObjCTypes.PropertyListPtrTy);
1208
1209}
1210
1211/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001212 struct objc_method_description_list {
1213 int count;
1214 struct objc_method_description list[];
1215 };
1216*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001217llvm::Constant *
1218CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1219 std::vector<llvm::Constant*> Desc(2);
1220 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1221 ObjCTypes.SelectorPtrTy);
1222 Desc[1] = GetMethodVarType(MD);
1223 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1224 Desc);
1225}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001226
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001227llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1228 const char *Section,
1229 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001230 // Return null for empty list.
1231 if (Methods.empty())
1232 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1233
1234 std::vector<llvm::Constant*> Values(2);
1235 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1236 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1237 Methods.size());
1238 Values[1] = llvm::ConstantArray::get(AT, Methods);
1239 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1240
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001241 llvm::GlobalVariable *GV =
1242 new llvm::GlobalVariable(Init->getType(), false,
1243 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001244 Init, Name, &CGM.getModule());
1245 GV->setSection(Section);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001246 UsedGlobals.push_back(GV);
1247 return llvm::ConstantExpr::getBitCast(GV,
1248 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001249}
1250
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001251/*
1252 struct _objc_category {
1253 char *category_name;
1254 char *class_name;
1255 struct _objc_method_list *instance_methods;
1256 struct _objc_method_list *class_methods;
1257 struct _objc_protocol_list *protocols;
1258 uint32_t size; // <rdar://4585769>
1259 struct _objc_property_list *instance_properties;
1260 };
1261 */
Daniel Dunbarac93e472008-08-15 22:20:32 +00001262void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbard8439f22009-01-12 21:08:18 +00001263 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001264
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001265 // FIXME: This is poor design, the OCD should have a pointer to the
1266 // category decl. Additionally, note that Category can be null for
1267 // the @implementation w/o an @interface case. Sema should just
1268 // create one for us as it does for @implementation so everyone else
1269 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001270 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001271 const ObjCCategoryDecl *Category =
1272 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattner271d4c22008-11-24 05:29:24 +00001273 std::string ExtName(Interface->getNameAsString() + "_" +
1274 OCD->getNameAsString());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001275
Daniel Dunbar12996f52008-08-26 21:51:14 +00001276 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1277 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1278 e = OCD->instmeth_end(); i != e; ++i) {
1279 // Instance methods should always be defined.
1280 InstanceMethods.push_back(GetMethodConstant(*i));
1281 }
1282 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1283 e = OCD->classmeth_end(); i != e; ++i) {
1284 // Class methods should always be defined.
1285 ClassMethods.push_back(GetMethodConstant(*i));
1286 }
1287
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001288 std::vector<llvm::Constant*> Values(7);
1289 Values[0] = GetClassName(OCD->getIdentifier());
1290 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001291 Values[2] =
1292 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1293 ExtName,
1294 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001295 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001296 Values[3] =
1297 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1298 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001299 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001300 if (Category) {
1301 Values[4] =
1302 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1303 Category->protocol_begin(),
1304 Category->protocol_end());
1305 } else {
1306 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1307 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001308 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001309
1310 // If there is no category @interface then there can be no properties.
1311 if (Category) {
1312 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001313 OCD, Category, ObjCTypes);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001314 } else {
1315 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1316 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001317
1318 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1319 Values);
1320
1321 llvm::GlobalVariable *GV =
1322 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1323 llvm::GlobalValue::InternalLinkage,
1324 Init,
1325 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1326 &CGM.getModule());
1327 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1328 UsedGlobals.push_back(GV);
1329 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001330}
1331
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001332// FIXME: Get from somewhere?
1333enum ClassFlags {
1334 eClassFlags_Factory = 0x00001,
1335 eClassFlags_Meta = 0x00002,
1336 // <rdr://5142207>
1337 eClassFlags_HasCXXStructors = 0x02000,
1338 eClassFlags_Hidden = 0x20000,
1339 eClassFlags_ABI2_Hidden = 0x00010,
1340 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1341};
1342
1343// <rdr://5142207&4705298&4843145>
1344static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1345 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1346 // FIXME: Support -fvisibility
1347 switch (attr->getVisibility()) {
1348 default:
1349 assert(0 && "Unknown visibility");
1350 return false;
1351 case VisibilityAttr::DefaultVisibility:
1352 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1353 return false;
1354 case VisibilityAttr::HiddenVisibility:
1355 return true;
1356 }
1357 } else {
1358 return false; // FIXME: Support -fvisibility
1359 }
1360}
1361
1362/*
1363 struct _objc_class {
1364 Class isa;
1365 Class super_class;
1366 const char *name;
1367 long version;
1368 long info;
1369 long instance_size;
1370 struct _objc_ivar_list *ivars;
1371 struct _objc_method_list *methods;
1372 struct _objc_cache *cache;
1373 struct _objc_protocol_list *protocols;
1374 // Objective-C 1.0 extensions (<rdr://4585769>)
1375 const char *ivar_layout;
1376 struct _objc_class_ext *ext;
1377 };
1378
1379 See EmitClassExtension();
1380 */
1381void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001382 DefinedSymbols.insert(ID->getIdentifier());
1383
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001384 std::string ClassName = ID->getNameAsString();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001385 // FIXME: Gross
1386 ObjCInterfaceDecl *Interface =
1387 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001388 llvm::Constant *Protocols =
Chris Lattner271d4c22008-11-24 05:29:24 +00001389 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001390 Interface->protocol_begin(),
1391 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001392 const llvm::Type *InterfaceTy =
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00001393 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001394 unsigned Flags = eClassFlags_Factory;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001395 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001396
1397 // FIXME: Set CXX-structors flag.
1398 if (IsClassHidden(ID->getClassInterface()))
1399 Flags |= eClassFlags_Hidden;
1400
Daniel Dunbar12996f52008-08-26 21:51:14 +00001401 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1402 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1403 e = ID->instmeth_end(); i != e; ++i) {
1404 // Instance methods should always be defined.
1405 InstanceMethods.push_back(GetMethodConstant(*i));
1406 }
1407 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1408 e = ID->classmeth_end(); i != e; ++i) {
1409 // Class methods should always be defined.
1410 ClassMethods.push_back(GetMethodConstant(*i));
1411 }
1412
1413 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1414 e = ID->propimpl_end(); i != e; ++i) {
1415 ObjCPropertyImplDecl *PID = *i;
1416
1417 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1418 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1419
1420 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1421 if (llvm::Constant *C = GetMethodConstant(MD))
1422 InstanceMethods.push_back(C);
1423 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1424 if (llvm::Constant *C = GetMethodConstant(MD))
1425 InstanceMethods.push_back(C);
1426 }
1427 }
1428
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001429 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar12996f52008-08-26 21:51:14 +00001430 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001431 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001432 // Record a reference to the super class.
1433 LazySymbols.insert(Super->getIdentifier());
1434
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001435 Values[ 1] =
1436 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1437 ObjCTypes.ClassPtrTy);
1438 } else {
1439 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1440 }
1441 Values[ 2] = GetClassName(ID->getIdentifier());
1442 // Version is always 0.
1443 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1444 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1445 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001446 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001447 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001448 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001449 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001450 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001451 // cache is always NULL.
1452 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1453 Values[ 9] = Protocols;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001454 // FIXME: Set ivar_layout
1455 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001456 Values[11] = EmitClassExtension(ID);
1457 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1458 Values);
1459
1460 llvm::GlobalVariable *GV =
1461 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1462 llvm::GlobalValue::InternalLinkage,
1463 Init,
1464 std::string("\01L_OBJC_CLASS_")+ClassName,
1465 &CGM.getModule());
1466 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1467 UsedGlobals.push_back(GV);
1468 // FIXME: Why?
1469 GV->setAlignment(32);
1470 DefinedClasses.push_back(GV);
1471}
1472
1473llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1474 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +00001475 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001476 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001477 unsigned Flags = eClassFlags_Meta;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001478 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001479
1480 if (IsClassHidden(ID->getClassInterface()))
1481 Flags |= eClassFlags_Hidden;
1482
1483 std::vector<llvm::Constant*> Values(12);
1484 // The isa for the metaclass is the root of the hierarchy.
1485 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1486 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1487 Root = Super;
1488 Values[ 0] =
1489 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1490 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001491 // The super class for the metaclass is emitted as the name of the
1492 // super class. The runtime fixes this up to point to the
1493 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001494 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1495 Values[ 1] =
1496 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1497 ObjCTypes.ClassPtrTy);
1498 } else {
1499 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1500 }
1501 Values[ 2] = GetClassName(ID->getIdentifier());
1502 // Version is always 0.
1503 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1504 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1505 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001506 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001507 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001508 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001509 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001510 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001511 // cache is always NULL.
1512 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1513 Values[ 9] = Protocols;
1514 // ivar_layout for metaclass is always NULL.
1515 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1516 // The class extension is always unused for metaclasses.
1517 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1518 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1519 Values);
1520
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001521 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001522 Name += ID->getNameAsCString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001523
1524 // Check for a forward reference.
1525 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1526 if (GV) {
1527 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1528 "Forward metaclass reference has incorrect type.");
1529 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1530 GV->setInitializer(Init);
1531 } else {
1532 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1533 llvm::GlobalValue::InternalLinkage,
1534 Init, Name,
1535 &CGM.getModule());
1536 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001537 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1538 UsedGlobals.push_back(GV);
1539 // FIXME: Why?
1540 GV->setAlignment(32);
1541
1542 return GV;
1543}
1544
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001545llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001546 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001547
1548 // FIXME: Should we look these up somewhere other than the
1549 // module. Its a bit silly since we only generate these while
1550 // processing an implementation, so exactly one pointer would work
1551 // if know when we entered/exitted an implementation block.
1552
1553 // Check for an existing forward reference.
Fariborz Jahanian5fe09f72009-01-07 20:11:22 +00001554 // Previously, metaclass with internal linkage may have been defined.
1555 // pass 'true' as 2nd argument so it is returned.
1556 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001557 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1558 "Forward metaclass reference has incorrect type.");
1559 return GV;
1560 } else {
1561 // Generate as an external reference to keep a consistent
1562 // module. This will be patched up when we emit the metaclass.
1563 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1564 llvm::GlobalValue::ExternalLinkage,
1565 0,
1566 Name,
1567 &CGM.getModule());
1568 }
1569}
1570
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001571/*
1572 struct objc_class_ext {
1573 uint32_t size;
1574 const char *weak_ivar_layout;
1575 struct _objc_property_list *properties;
1576 };
1577*/
1578llvm::Constant *
1579CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1580 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001581 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001582
1583 std::vector<llvm::Constant*> Values(3);
1584 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001585 // FIXME: Output weak_ivar_layout string.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001586 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattner271d4c22008-11-24 05:29:24 +00001587 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001588 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001589
1590 // Return null if no extension bits are used.
1591 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1592 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1593
1594 llvm::Constant *Init =
1595 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1596 llvm::GlobalVariable *GV =
1597 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1598 llvm::GlobalValue::InternalLinkage,
1599 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001600 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001601 &CGM.getModule());
1602 // No special section, but goes in llvm.used
1603 UsedGlobals.push_back(GV);
1604
1605 return GV;
1606}
1607
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001608/// countInheritedIvars - count number of ivars in class and its super class(s)
1609///
1610static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1611 int count = 0;
1612 if (!OI)
1613 return 0;
1614 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1615 if (SuperClass)
1616 count += countInheritedIvars(SuperClass);
1617 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1618 E = OI->ivar_end(); I != E; ++I)
1619 ++count;
1620 return count;
1621}
1622
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00001623/// getInterfaceDeclForIvar - Get the interface declaration node where
1624/// this ivar is declared in.
1625/// FIXME. Ideally, this info should be in the ivar node. But currently
1626/// it is not and prevailing wisdom is that ASTs should not have more
1627/// info than is absolutely needed, even though this info reflects the
1628/// source language.
1629///
1630static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1631 const ObjCInterfaceDecl *OI,
1632 const ObjCIvarDecl *IVD) {
1633 if (!OI)
1634 return 0;
1635 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1636 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1637 E = OI->ivar_end(); I != E; ++I)
1638 if ((*I)->getIdentifier() == IVD->getIdentifier())
1639 return OI;
1640 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1641}
1642
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001643/*
1644 struct objc_ivar {
1645 char *ivar_name;
1646 char *ivar_type;
1647 int ivar_offset;
1648 };
1649
1650 struct objc_ivar_list {
1651 int ivar_count;
1652 struct objc_ivar list[count];
1653 };
1654 */
1655llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001656 bool ForClass) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001657 std::vector<llvm::Constant*> Ivars, Ivar(3);
1658
1659 // When emitting the root class GCC emits ivar entries for the
1660 // actual class structure. It is not clear if we need to follow this
1661 // behavior; for now lets try and get away with not doing it. If so,
1662 // the cleanest solution would be to make up an ObjCInterfaceDecl
1663 // for the class.
1664 if (ForClass)
1665 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001666
1667 ObjCInterfaceDecl *OID =
1668 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1669 const llvm::Type *InterfaceTy =
1670 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001671 const llvm::StructLayout *Layout =
1672 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001673
1674 RecordDecl::field_iterator ifield, pfield;
1675 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001676 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1677 FieldDecl *Field = *ifield;
1678 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1679 getLLVMFieldNo(Field));
1680 if (Field->getIdentifier())
1681 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1682 else
1683 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001684 std::string TypeStr;
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001685 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001686 Ivar[1] = GetMethodVarType(TypeStr);
1687 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001688 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001689 }
1690
1691 // Return null for empty list.
1692 if (Ivars.empty())
1693 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1694
1695 std::vector<llvm::Constant*> Values(2);
1696 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1697 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1698 Ivars.size());
1699 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1700 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1701
1702 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1703 "\01L_OBJC_INSTANCE_VARIABLES_");
1704 llvm::GlobalVariable *GV =
1705 new llvm::GlobalVariable(Init->getType(), false,
1706 llvm::GlobalValue::InternalLinkage,
1707 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001708 Prefix + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001709 &CGM.getModule());
1710 if (ForClass) {
1711 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1712 // FIXME: Why is this only here?
1713 GV->setAlignment(32);
1714 } else {
1715 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1716 }
1717 UsedGlobals.push_back(GV);
1718 return llvm::ConstantExpr::getBitCast(GV,
1719 ObjCTypes.IvarListPtrTy);
1720}
1721
1722/*
1723 struct objc_method {
1724 SEL method_name;
1725 char *method_types;
1726 void *method;
1727 };
1728
1729 struct objc_method_list {
1730 struct objc_method_list *obsolete;
1731 int count;
1732 struct objc_method methods_list[count];
1733 };
1734*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00001735
1736/// GetMethodConstant - Return a struct objc_method constant for the
1737/// given method if it has been defined. The result is null if the
1738/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001739llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001740 // FIXME: Use DenseMap::lookup
1741 llvm::Function *Fn = MethodDefinitions[MD];
1742 if (!Fn)
1743 return 0;
1744
1745 std::vector<llvm::Constant*> Method(3);
1746 Method[0] =
1747 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1748 ObjCTypes.SelectorPtrTy);
1749 Method[1] = GetMethodVarType(MD);
1750 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1751 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1752}
1753
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001754llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1755 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001756 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001757 // Return null for empty list.
1758 if (Methods.empty())
1759 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1760
1761 std::vector<llvm::Constant*> Values(3);
1762 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1763 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1764 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1765 Methods.size());
1766 Values[2] = llvm::ConstantArray::get(AT, Methods);
1767 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1768
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001769 llvm::GlobalVariable *GV =
1770 new llvm::GlobalVariable(Init->getType(), false,
1771 llvm::GlobalValue::InternalLinkage,
1772 Init,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001773 Name,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001774 &CGM.getModule());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001775 GV->setSection(Section);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001776 UsedGlobals.push_back(GV);
1777 return llvm::ConstantExpr::getBitCast(GV,
1778 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00001779}
1780
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001781llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar9fc15a82009-02-02 21:43:58 +00001782 const ObjCContainerDecl *CD) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001783 std::string Name;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001784 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarace33292008-08-16 03:19:19 +00001785
Daniel Dunbar34bda882009-02-02 23:23:47 +00001786 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001787 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001788 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarace33292008-08-16 03:19:19 +00001789 llvm::Function *Method =
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001790 llvm::Function::Create(MethodTy,
Daniel Dunbarace33292008-08-16 03:19:19 +00001791 llvm::GlobalValue::InternalLinkage,
1792 Name,
1793 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00001794 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00001795
Daniel Dunbarace33292008-08-16 03:19:19 +00001796 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001797}
1798
1799llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001800 // Abuse this interface function as a place to finalize.
1801 FinishModule();
1802
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001803 return NULL;
1804}
1805
Daniel Dunbarf7103722008-09-24 03:38:44 +00001806llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1807 return ObjCTypes.GetPropertyFn;
1808}
1809
1810llvm::Function *CGObjCMac::GetPropertySetFunction() {
1811 return ObjCTypes.SetPropertyFn;
1812}
1813
Anders Carlsson58d16242008-08-31 04:05:03 +00001814llvm::Function *CGObjCMac::EnumerationMutationFunction()
1815{
1816 return ObjCTypes.EnumerationMutationFn;
1817}
1818
Daniel Dunbar83544842008-09-28 01:03:14 +00001819/*
1820
1821Objective-C setjmp-longjmp (sjlj) Exception Handling
1822--
1823
1824The basic framework for a @try-catch-finally is as follows:
1825{
1826 objc_exception_data d;
1827 id _rethrow = null;
Anders Carlsson8559de12009-02-07 21:26:04 +00001828 bool _call_try_exit = true;
1829
Daniel Dunbar83544842008-09-28 01:03:14 +00001830 objc_exception_try_enter(&d);
1831 if (!setjmp(d.jmp_buf)) {
1832 ... try body ...
1833 } else {
1834 // exception path
1835 id _caught = objc_exception_extract(&d);
1836
1837 // enter new try scope for handlers
1838 if (!setjmp(d.jmp_buf)) {
1839 ... match exception and execute catch blocks ...
1840
1841 // fell off end, rethrow.
1842 _rethrow = _caught;
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001843 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001844 } else {
1845 // exception in catch block
1846 _rethrow = objc_exception_extract(&d);
Anders Carlsson8559de12009-02-07 21:26:04 +00001847 _call_try_exit = false;
1848 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001849 }
1850 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001851 ... jump-through-finally to finally_end ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001852
1853finally:
Anders Carlsson8559de12009-02-07 21:26:04 +00001854 if (_call_try_exit)
1855 objc_exception_try_exit(&d);
1856
Daniel Dunbar83544842008-09-28 01:03:14 +00001857 ... finally block ....
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001858 ... dispatch to finally destination ...
1859
1860finally_rethrow:
1861 objc_exception_throw(_rethrow);
1862
1863finally_end:
Daniel Dunbar83544842008-09-28 01:03:14 +00001864}
1865
1866This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001867uses _rethrow to determine if objc_exception_try_exit should be called
1868and if the object should be rethrown. This breaks in the face of
1869throwing nil and introduces unnecessary branches.
Daniel Dunbar83544842008-09-28 01:03:14 +00001870
1871We specialize this framework for a few particular circumstances:
1872
1873 - If there are no catch blocks, then we avoid emitting the second
1874 exception handling context.
1875
1876 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1877 e)) we avoid emitting the code to rethrow an uncaught exception.
1878
1879 - FIXME: If there is no @finally block we can do a few more
1880 simplifications.
1881
1882Rethrows and Jumps-Through-Finally
1883--
1884
1885Support for implicit rethrows and jumping through the finally block is
1886handled by storing the current exception-handling context in
1887ObjCEHStack.
1888
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001889In order to implement proper @finally semantics, we support one basic
1890mechanism for jumping through the finally block to an arbitrary
1891destination. Constructs which generate exits from a @try or @catch
1892block use this mechanism to implement the proper semantics by chaining
1893jumps, as necessary.
1894
1895This mechanism works like the one used for indirect goto: we
1896arbitrarily assign an ID to each destination and store the ID for the
1897destination in a variable prior to entering the finally block. At the
1898end of the finally block we simply create a switch to the proper
1899destination.
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001900
1901Code gen for @synchronized(expr) stmt;
1902Effectively generating code for:
1903objc_sync_enter(expr);
1904@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar83544842008-09-28 01:03:14 +00001905*/
1906
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001907void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1908 const Stmt &S) {
1909 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001910 // Create various blocks we refer to for handling @finally.
Daniel Dunbar72f96552008-11-11 02:29:29 +00001911 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson8559de12009-02-07 21:26:04 +00001912 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar72f96552008-11-11 02:29:29 +00001913 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1914 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1915 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar34416d62009-02-24 01:43:46 +00001916
1917 // For @synchronized, call objc_sync_enter(sync.expr). The
1918 // evaluation of the expression must occur before we enter the
1919 // @synchronized. We can safely avoid a temp here because jumps into
1920 // @synchronized are illegal & this will dominate uses.
1921 llvm::Value *SyncArg = 0;
1922 if (!isTry) {
1923 SyncArg =
1924 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1925 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
1926 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
1927 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001928
1929 // Push an EH context entry, used for handling rethrows and jumps
1930 // through finally.
Anders Carlsson00ffb962009-02-09 20:38:58 +00001931 CGF.PushCleanupBlock(FinallyBlock);
1932
Anders Carlssonecd81832009-02-07 21:37:21 +00001933 CGF.ObjCEHValueStack.push_back(0);
1934
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001935 // Allocate memory for the exception data and rethrow pointer.
Anders Carlssonfca6c292008-09-09 17:59:25 +00001936 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1937 "exceptiondata.ptr");
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001938 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1939 "_rethrow");
Anders Carlsson8559de12009-02-07 21:26:04 +00001940 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1941 "_call_try_exit");
1942 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1943
Anders Carlssonfca6c292008-09-09 17:59:25 +00001944 // Enter a new try block and call setjmp.
1945 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1946 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1947 "jmpbufarray");
1948 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1949 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1950 JmpBufPtr, "result");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001951
Daniel Dunbar72f96552008-11-11 02:29:29 +00001952 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1953 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001954 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001955 TryHandler, TryBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001956
1957 // Emit the @try block.
1958 CGF.EmitBlock(TryBlock);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001959 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1960 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00001961 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001962
1963 // Emit the "exception in @try" block.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001964 CGF.EmitBlock(TryHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001965
1966 // Retrieve the exception object. We may emit multiple blocks but
1967 // nothing can cross this so the value is already in SSA form.
1968 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1969 ExceptionData,
1970 "caught");
Anders Carlssonecd81832009-02-07 21:37:21 +00001971 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001972 if (!isTry)
1973 {
1974 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00001975 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00001976 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001977 }
1978 else if (const ObjCAtCatchStmt* CatchStmt =
1979 cast<ObjCAtTryStmt>(S).getCatchStmts())
1980 {
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001981 // Enter a new exception try block (in case a @catch block throws
1982 // an exception).
Anders Carlssonfca6c292008-09-09 17:59:25 +00001983 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001984
Anders Carlssonfca6c292008-09-09 17:59:25 +00001985 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1986 JmpBufPtr, "result");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001987 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001988
Daniel Dunbar72f96552008-11-11 02:29:29 +00001989 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1990 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001991 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001992
1993 CGF.EmitBlock(CatchBlock);
1994
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001995 // Handle catch list. As a special case we check if everything is
1996 // matched and avoid generating code for falling off the end if
1997 // so.
1998 bool AllMatched = false;
Anders Carlssonfca6c292008-09-09 17:59:25 +00001999 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar72f96552008-11-11 02:29:29 +00002000 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002001
Daniel Dunbard23d9922009-03-01 04:28:32 +00002002 const DeclStmt *CatchParam = CatchStmt->getCatchParamStmt();
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002003 const VarDecl *VD = 0;
2004 const PointerType *PT = 0;
2005
Anders Carlssonfca6c292008-09-09 17:59:25 +00002006 // catch(...) always matches.
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002007 if (!CatchParam) {
2008 AllMatched = true;
2009 } else {
Ted Kremenekf41e9f72008-10-06 20:58:56 +00002010 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002011 PT = VD->getType()->getAsPointerType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002012
Daniel Dunbard04c9352008-09-27 22:21:14 +00002013 // catch(id e) always matches.
2014 // FIXME: For the time being we also match id<X>; this should
2015 // be rejected by Sema instead.
Steve Naroff17c03822009-02-12 17:52:19 +00002016 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbard04c9352008-09-27 22:21:14 +00002017 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002018 AllMatched = true;
Anders Carlssonfca6c292008-09-09 17:59:25 +00002019 }
2020
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002021 if (AllMatched) {
Anders Carlsson75d86732008-09-11 09:15:33 +00002022 if (CatchParam) {
2023 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002024 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002025 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00002026 }
Anders Carlsson1f4acc32008-09-11 08:21:54 +00002027
Anders Carlsson75d86732008-09-11 09:15:33 +00002028 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002029 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002030 break;
2031 }
2032
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002033 assert(PT && "Unexpected non-pointer type in @catch");
2034 QualType T = PT->getPointeeType();
Anders Carlssona4519172008-09-11 06:35:14 +00002035 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00002036 assert(ObjCType && "Catch parameter must have Objective-C type!");
2037
2038 // Check if the @catch block matches the exception object.
2039 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2040
Anders Carlssonfca6c292008-09-09 17:59:25 +00002041 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2042 Class, Caught, "match");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002043
Daniel Dunbar72f96552008-11-11 02:29:29 +00002044 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlssonfca6c292008-09-09 17:59:25 +00002045
Daniel Dunbarbe56f012008-10-02 17:05:36 +00002046 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002047 MatchedBlock, NextCatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002048
2049 // Emit the @catch block.
2050 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar83544842008-09-28 01:03:14 +00002051 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002052 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar83544842008-09-28 01:03:14 +00002053
2054 llvm::Value *Tmp =
2055 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2056 "tmp");
2057 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00002058
2059 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlsson00ffb962009-02-09 20:38:58 +00002060 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002061
2062 CGF.EmitBlock(NextCatchBlock);
2063 }
2064
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002065 if (!AllMatched) {
2066 // None of the handlers caught the exception, so store it to be
2067 // rethrown at the end of the @finally block.
2068 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002069 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002070 }
2071
2072 // Emit the exception handler for the @catch blocks.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002073 CGF.EmitBlock(CatchHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002074 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2075 ExceptionData),
2076 RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002077 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002078 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00002079 } else {
Anders Carlssonfca6c292008-09-09 17:59:25 +00002080 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson8559de12009-02-07 21:26:04 +00002081 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlsson00ffb962009-02-09 20:38:58 +00002082 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002083 }
2084
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002085 // Pop the exception-handling stack entry. It is important to do
2086 // this now, because the code in the @finally block is not in this
2087 // context.
Anders Carlsson00ffb962009-02-09 20:38:58 +00002088 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2089
Anders Carlssonecd81832009-02-07 21:37:21 +00002090 CGF.ObjCEHValueStack.pop_back();
2091
Anders Carlssonfca6c292008-09-09 17:59:25 +00002092 // Emit the @finally block.
2093 CGF.EmitBlock(FinallyBlock);
Anders Carlsson8559de12009-02-07 21:26:04 +00002094 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2095
2096 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2097
2098 CGF.EmitBlock(FinallyExit);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002099 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar7a68b452008-09-27 07:36:24 +00002100
2101 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00002102 if (isTry) {
2103 if (const ObjCAtFinallyStmt* FinallyStmt =
2104 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2105 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar34416d62009-02-24 01:43:46 +00002106 } else {
2107 // Emit objc_sync_exit(expr); as finally's sole statement for
2108 // @synchronized.
2109 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanian3895d922008-11-21 19:21:53 +00002110 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00002111
Anders Carlsson00ffb962009-02-09 20:38:58 +00002112 // Emit the switch block
2113 if (Info.SwitchBlock)
2114 CGF.EmitBlock(Info.SwitchBlock);
2115 if (Info.EndBlock)
2116 CGF.EmitBlock(Info.EndBlock);
2117
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002118 CGF.EmitBlock(FinallyRethrow);
2119 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2120 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00002121 CGF.Builder.CreateUnreachable();
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002122
2123 CGF.EmitBlock(FinallyEnd);
Anders Carlssonb01a2112008-09-09 10:04:29 +00002124}
2125
2126void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002127 const ObjCAtThrowStmt &S) {
Anders Carlsson05d7be72008-09-09 16:16:55 +00002128 llvm::Value *ExceptionAsObject;
2129
2130 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2131 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2132 ExceptionAsObject =
2133 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2134 } else {
Anders Carlssonecd81832009-02-07 21:37:21 +00002135 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar83544842008-09-28 01:03:14 +00002136 "Unexpected rethrow outside @catch block.");
Anders Carlssonecd81832009-02-07 21:37:21 +00002137 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson05d7be72008-09-09 16:16:55 +00002138 }
2139
2140 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002141 CGF.Builder.CreateUnreachable();
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002142
2143 // Clear the insertion point to indicate we are in unreachable code.
2144 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00002145}
2146
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002147/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002148/// object: objc_read_weak (id *src)
2149///
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002150llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002151 llvm::Value *AddrWeakObj)
2152{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002153 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002154 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002155 AddrWeakObj, "weakread");
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002156 return read_weak;
2157}
2158
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002159/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2160/// objc_assign_weak (id src, id *dst)
2161///
2162void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2163 llvm::Value *src, llvm::Value *dst)
2164{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002165 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2166 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002167 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2168 src, dst, "weakassign");
2169 return;
2170}
2171
Fariborz Jahanian17958902008-11-19 00:59:10 +00002172/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2173/// objc_assign_global (id src, id *dst)
2174///
2175void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2176 llvm::Value *src, llvm::Value *dst)
2177{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002178 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2179 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002180 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2181 src, dst, "globalassign");
2182 return;
2183}
2184
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002185/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2186/// objc_assign_ivar (id src, id *dst)
2187///
2188void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2189 llvm::Value *src, llvm::Value *dst)
2190{
2191 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2192 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2193 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2194 src, dst, "assignivar");
2195 return;
2196}
2197
Fariborz Jahanian17958902008-11-19 00:59:10 +00002198/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2199/// objc_assign_strongCast (id src, id *dst)
2200///
2201void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2202 llvm::Value *src, llvm::Value *dst)
2203{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002204 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2205 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002206 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2207 src, dst, "weakassign");
2208 return;
2209}
2210
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002211/// EmitObjCValueForIvar - Code Gen for ivar reference.
2212///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002213LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2214 QualType ObjectTy,
2215 llvm::Value *BaseValue,
2216 const ObjCIvarDecl *Ivar,
2217 const FieldDecl *Field,
2218 unsigned CVRQualifiers) {
2219 if (Ivar->isBitField())
2220 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2221 CVRQualifiers);
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002222 // TODO: Add a special case for isa (index 0)
2223 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2224 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002225 LValue LV = LValue::MakeAddr(V,
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002226 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2227 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00002228 LValue::SetObjCIvar(LV, true);
2229 return LV;
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00002230}
2231
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00002232llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2233 ObjCInterfaceDecl *Interface,
2234 const ObjCIvarDecl *Ivar) {
2235 const llvm::Type *InterfaceLTy =
2236 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2237 const llvm::StructLayout *Layout =
2238 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2239 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2240 uint64_t Offset =
2241 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2242
2243 return llvm::ConstantInt::get(
2244 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2245 Offset);
2246}
2247
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002248/* *** Private Interface *** */
2249
2250/// EmitImageInfo - Emit the image info marker used to encode some module
2251/// level information.
2252///
2253/// See: <rdr://4810609&4810587&4810587>
2254/// struct IMAGE_INFO {
2255/// unsigned version;
2256/// unsigned flags;
2257/// };
2258enum ImageInfoFlags {
2259 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2260 eImageInfo_GarbageCollected = (1 << 1),
2261 eImageInfo_GCOnly = (1 << 2)
2262};
2263
2264void CGObjCMac::EmitImageInfo() {
2265 unsigned version = 0; // Version is unused?
2266 unsigned flags = 0;
2267
2268 // FIXME: Fix and continue?
2269 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2270 flags |= eImageInfo_GarbageCollected;
2271 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2272 flags |= eImageInfo_GCOnly;
2273
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002274 // Emitted as int[2];
2275 llvm::Constant *values[2] = {
2276 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2277 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2278 };
2279 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002280 llvm::GlobalVariable *GV =
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002281 new llvm::GlobalVariable(AT, true,
2282 llvm::GlobalValue::InternalLinkage,
2283 llvm::ConstantArray::get(AT, values, 2),
2284 "\01L_OBJC_IMAGE_INFO",
2285 &CGM.getModule());
2286
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002287 if (ObjCABI == 1) {
2288 GV->setSection("__OBJC, __image_info,regular");
2289 } else {
2290 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2291 }
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002292
2293 UsedGlobals.push_back(GV);
2294}
2295
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002296
2297// struct objc_module {
2298// unsigned long version;
2299// unsigned long size;
2300// const char *name;
2301// Symtab symtab;
2302// };
2303
2304// FIXME: Get from somewhere
2305static const int ModuleVersion = 7;
2306
2307void CGObjCMac::EmitModuleInfo() {
Daniel Dunbard8439f22009-01-12 21:08:18 +00002308 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002309
2310 std::vector<llvm::Constant*> Values(4);
2311 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2312 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00002313 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002314 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002315 Values[3] = EmitModuleSymbols();
2316
2317 llvm::GlobalVariable *GV =
2318 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2319 llvm::GlobalValue::InternalLinkage,
2320 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2321 Values),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002322 "\01L_OBJC_MODULES",
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002323 &CGM.getModule());
2324 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2325 UsedGlobals.push_back(GV);
2326}
2327
2328llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002329 unsigned NumClasses = DefinedClasses.size();
2330 unsigned NumCategories = DefinedCategories.size();
2331
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002332 // Return null if no symbols were defined.
2333 if (!NumClasses && !NumCategories)
2334 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2335
2336 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002337 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2338 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2339 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2340 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2341
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002342 // The runtime expects exactly the list of defined classes followed
2343 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002344 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002345 for (unsigned i=0; i<NumClasses; i++)
2346 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2347 ObjCTypes.Int8PtrTy);
2348 for (unsigned i=0; i<NumCategories; i++)
2349 Symbols[NumClasses + i] =
2350 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2351 ObjCTypes.Int8PtrTy);
2352
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002353 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002354 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002355 NumClasses + NumCategories),
2356 Symbols);
2357
2358 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2359
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002360 llvm::GlobalVariable *GV =
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002361 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002362 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002363 Init,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002364 "\01L_OBJC_SYMBOLS",
2365 &CGM.getModule());
2366 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2367 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002368 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2369}
2370
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002371llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002372 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002373 LazySymbols.insert(ID->getIdentifier());
2374
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002375 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2376
2377 if (!Entry) {
2378 llvm::Constant *Casted =
2379 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2380 ObjCTypes.ClassPtrTy);
2381 Entry =
2382 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2383 llvm::GlobalValue::InternalLinkage,
2384 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2385 &CGM.getModule());
2386 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2387 UsedGlobals.push_back(Entry);
2388 }
2389
2390 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002391}
2392
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002393llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002394 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2395
2396 if (!Entry) {
2397 llvm::Constant *Casted =
2398 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2399 ObjCTypes.SelectorPtrTy);
2400 Entry =
2401 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2402 llvm::GlobalValue::InternalLinkage,
2403 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2404 &CGM.getModule());
2405 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2406 UsedGlobals.push_back(Entry);
2407 }
2408
2409 return Builder.CreateLoad(Entry, false, "tmp");
2410}
2411
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00002412llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002413 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002414
2415 if (!Entry) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002416 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002417 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002418 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002419 llvm::GlobalValue::InternalLinkage,
2420 C, "\01L_OBJC_CLASS_NAME_",
2421 &CGM.getModule());
2422 Entry->setSection("__TEXT,__cstring,cstring_literals");
2423 UsedGlobals.push_back(Entry);
2424 }
2425
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002426 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002427}
2428
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002429llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002430 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2431
2432 if (!Entry) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00002433 // FIXME: Avoid std::string copying.
2434 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002435 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002436 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002437 llvm::GlobalValue::InternalLinkage,
2438 C, "\01L_OBJC_METH_VAR_NAME_",
2439 &CGM.getModule());
2440 Entry->setSection("__TEXT,__cstring,cstring_literals");
2441 UsedGlobals.push_back(Entry);
2442 }
2443
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002444 return getConstantGEP(Entry, 0, 0);
2445}
2446
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002447// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002448llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002449 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2450}
2451
2452// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002453llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002454 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2455}
2456
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002457llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002458 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002459
2460 if (!Entry) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002461 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002462 Entry =
2463 new llvm::GlobalVariable(C->getType(), false,
2464 llvm::GlobalValue::InternalLinkage,
2465 C, "\01L_OBJC_METH_VAR_TYPE_",
2466 &CGM.getModule());
2467 Entry->setSection("__TEXT,__cstring,cstring_literals");
2468 UsedGlobals.push_back(Entry);
2469 }
2470
2471 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002472}
2473
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002474// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002475llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002476 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00002477 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2478 TypeStr);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002479 return GetMethodVarType(TypeStr);
2480}
2481
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002482// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002483llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002484 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2485
2486 if (!Entry) {
2487 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2488 Entry =
2489 new llvm::GlobalVariable(C->getType(), false,
2490 llvm::GlobalValue::InternalLinkage,
2491 C, "\01L_OBJC_PROP_NAME_ATTR_",
2492 &CGM.getModule());
2493 Entry->setSection("__TEXT,__cstring,cstring_literals");
2494 UsedGlobals.push_back(Entry);
2495 }
2496
2497 return getConstantGEP(Entry, 0, 0);
2498}
2499
2500// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002501// FIXME: This Decl should be more precise.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002502llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002503 const Decl *Container) {
2504 std::string TypeStr;
2505 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002506 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2507}
2508
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002509void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2510 const ObjCContainerDecl *CD,
2511 std::string &NameOut) {
Daniel Dunbarace33292008-08-16 03:19:19 +00002512 // FIXME: Find the mangling GCC uses.
Douglas Gregor5d764842009-01-09 17:18:27 +00002513 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner3a8f2942008-11-24 03:33:13 +00002514 NameOut += '[';
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00002515 assert (CD && "Missing container decl in GetNameForMethod");
2516 NameOut += CD->getNameAsString();
Fariborz Jahanianc6f0c212009-01-26 23:49:05 +00002517 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2518 // Right now! there is not enough info. to do this.
Chris Lattner3a8f2942008-11-24 03:33:13 +00002519 NameOut += ' ';
2520 NameOut += D->getSelector().getAsString();
2521 NameOut += ']';
Daniel Dunbarace33292008-08-16 03:19:19 +00002522}
2523
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002524/// GetFirstIvarInRecord - This routine returns the record for the
2525/// implementation of the fiven class OID. It also returns field
2526/// corresponding to the first ivar in the class in FIV. It also
2527/// returns the one before the first ivar.
2528///
2529const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2530 const ObjCInterfaceDecl *OID,
2531 RecordDecl::field_iterator &FIV,
2532 RecordDecl::field_iterator &PIV) {
2533 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2534 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2535 RecordDecl::field_iterator ifield = RD->field_begin();
2536 RecordDecl::field_iterator pfield = RD->field_end();
2537 while (countSuperClassIvars-- > 0) {
2538 pfield = ifield;
2539 ++ifield;
2540 }
2541 FIV = ifield;
2542 PIV = pfield;
2543 return RD;
2544}
2545
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002546void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002547 EmitModuleInfo();
2548
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002549 // Emit the dummy bodies for any protocols which were referenced but
2550 // never defined.
2551 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2552 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2553 if (i->second->hasInitializer())
2554 continue;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002555
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002556 std::vector<llvm::Constant*> Values(5);
2557 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2558 Values[1] = GetClassName(i->first);
2559 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2560 Values[3] = Values[4] =
2561 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2562 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2563 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2564 Values));
2565 }
2566
2567 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002568 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002569 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002570 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002571 }
2572
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002573 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002574 llvm::GlobalValue *GV =
2575 new llvm::GlobalVariable(AT, false,
2576 llvm::GlobalValue::AppendingLinkage,
2577 llvm::ConstantArray::get(AT, Used),
2578 "llvm.used",
2579 &CGM.getModule());
2580
2581 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002582
2583 // Add assembler directives to add lazy undefined symbol references
2584 // for classes which are referenced but not defined. This is
2585 // important for correct linker interaction.
2586
2587 // FIXME: Uh, this isn't particularly portable.
2588 std::stringstream s;
Anders Carlsson63f98352008-12-10 02:21:04 +00002589
2590 if (!CGM.getModule().getModuleInlineAsm().empty())
2591 s << "\n";
2592
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002593 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2594 e = LazySymbols.end(); i != e; ++i) {
2595 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2596 }
2597 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2598 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002599 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002600 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2601 }
Anders Carlsson63f98352008-12-10 02:21:04 +00002602
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002603 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002604}
2605
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002606CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002607 : CGObjCCommonMac(cgm),
2608 ObjCTypes(cgm)
2609{
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00002610 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002611 ObjCABI = 2;
2612}
2613
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002614/* *** */
2615
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002616ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2617: CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002618{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002619 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2620 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002621
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002622 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002623 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002624 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002625 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2626
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002627 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00002628 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002629 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002630
2631 // FIXME: It would be nice to unify this with the opaque type, so
2632 // that the IR comes out a bit cleaner.
2633 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2634 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002635
2636 // I'm not sure I like this. The implicit coordination is a bit
2637 // gross. We should solve this in a reasonable fashion because this
2638 // is a pretty common task (match some runtime data structure with
2639 // an LLVM data structure).
2640
2641 // FIXME: This is leaked.
2642 // FIXME: Merge with rewriter code?
2643
2644 // struct _objc_super {
2645 // id self;
2646 // Class cls;
2647 // }
2648 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2649 SourceLocation(),
2650 &Ctx.Idents.get("_objc_super"));
2651 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2652 Ctx.getObjCIdType(), 0, false));
2653 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2654 Ctx.getObjCClassType(), 0, false));
2655 RD->completeDefinition(Ctx);
2656
2657 SuperCTy = Ctx.getTagDeclType(RD);
2658 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2659
2660 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002661 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2662
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002663 // struct _prop_t {
2664 // char *name;
2665 // char *attributes;
2666 // }
2667 PropertyTy = llvm::StructType::get(Int8PtrTy,
2668 Int8PtrTy,
2669 NULL);
2670 CGM.getModule().addTypeName("struct._prop_t",
2671 PropertyTy);
2672
2673 // struct _prop_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002674 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002675 // uint32_t count_of_properties;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002676 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002677 // }
2678 PropertyListTy = llvm::StructType::get(IntTy,
2679 IntTy,
2680 llvm::ArrayType::get(PropertyTy, 0),
2681 NULL);
2682 CGM.getModule().addTypeName("struct._prop_list_t",
2683 PropertyListTy);
2684 // struct _prop_list_t *
2685 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2686
2687 // struct _objc_method {
2688 // SEL _cmd;
2689 // char *method_type;
2690 // char *_imp;
2691 // }
2692 MethodTy = llvm::StructType::get(SelectorPtrTy,
2693 Int8PtrTy,
2694 Int8PtrTy,
2695 NULL);
2696 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002697
2698 // struct _objc_cache *
2699 CacheTy = llvm::OpaqueType::get();
2700 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2701 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002702
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002703 // Property manipulation functions.
Daniel Dunbardac29922009-02-04 00:44:42 +00002704
2705 QualType IdType = Ctx.getObjCIdType();
2706 QualType SelType = Ctx.getObjCSelType();
2707 llvm::SmallVector<QualType,16> Params;
2708 const llvm::FunctionType *FTy;
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002709
2710 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbardac29922009-02-04 00:44:42 +00002711 Params.push_back(IdType);
2712 Params.push_back(SelType);
2713 Params.push_back(Ctx.LongTy);
2714 Params.push_back(Ctx.BoolTy);
2715 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2716 false);
2717 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002718
2719 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2720 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002721 Params.push_back(IdType);
2722 Params.push_back(SelType);
2723 Params.push_back(Ctx.LongTy);
2724 Params.push_back(IdType);
2725 Params.push_back(Ctx.BoolTy);
2726 Params.push_back(Ctx.BoolTy);
2727 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2728 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2729
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002730 // Enumeration mutation.
Daniel Dunbardac29922009-02-04 00:44:42 +00002731
2732 // void objc_enumerationMutation (id)
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002733 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002734 Params.push_back(IdType);
2735 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2736 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2737 "objc_enumerationMutation");
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002738
2739 // gc's API
2740 // id objc_read_weak (id *)
2741 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002742 Params.push_back(Ctx.getPointerType(IdType));
2743 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2744 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2745
2746 // id objc_assign_weak (id, id *)
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002747 Params.clear();
Daniel Dunbardac29922009-02-04 00:44:42 +00002748 Params.push_back(IdType);
2749 Params.push_back(Ctx.getPointerType(IdType));
2750
2751 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2752 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2753 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2754 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2755 GcAssignStrongCastFn =
2756 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlsson1cf75362009-02-16 22:59:18 +00002757
2758 // void objc_exception_throw(id)
2759 Params.clear();
2760 Params.push_back(IdType);
2761
2762 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlsson1cf75362009-02-16 22:59:18 +00002763 ExceptionThrowFn =
2764 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar34416d62009-02-24 01:43:46 +00002765
2766 // synchronized APIs
2767 // void objc_sync_enter (id)
2768 // void objc_sync_exit (id)
2769 Params.clear();
2770 Params.push_back(IdType);
2771
2772 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2773 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
2774 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002775}
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002776
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002777ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2778 : ObjCCommonTypesHelper(cgm)
2779{
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002780 // struct _objc_method_description {
2781 // SEL name;
2782 // char *types;
2783 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002784 MethodDescriptionTy =
2785 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002786 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002787 NULL);
2788 CGM.getModule().addTypeName("struct._objc_method_description",
2789 MethodDescriptionTy);
2790
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002791 // struct _objc_method_description_list {
2792 // int count;
2793 // struct _objc_method_description[1];
2794 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002795 MethodDescriptionListTy =
2796 llvm::StructType::get(IntTy,
2797 llvm::ArrayType::get(MethodDescriptionTy, 0),
2798 NULL);
2799 CGM.getModule().addTypeName("struct._objc_method_description_list",
2800 MethodDescriptionListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002801
2802 // struct _objc_method_description_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002803 MethodDescriptionListPtrTy =
2804 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2805
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002806 // Protocol description structures
2807
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002808 // struct _objc_protocol_extension {
2809 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2810 // struct _objc_method_description_list *optional_instance_methods;
2811 // struct _objc_method_description_list *optional_class_methods;
2812 // struct _objc_property_list *instance_properties;
2813 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002814 ProtocolExtensionTy =
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002815 llvm::StructType::get(IntTy,
2816 MethodDescriptionListPtrTy,
2817 MethodDescriptionListPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002818 PropertyListPtrTy,
2819 NULL);
2820 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2821 ProtocolExtensionTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002822
2823 // struct _objc_protocol_extension *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002824 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2825
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002826 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002827
2828 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2829 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2830
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002831 const llvm::Type *T =
2832 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2833 LongTy,
2834 llvm::ArrayType::get(ProtocolTyHolder, 0),
2835 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002836 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2837
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002838 // struct _objc_protocol {
2839 // struct _objc_protocol_extension *isa;
2840 // char *protocol_name;
2841 // struct _objc_protocol **_objc_protocol_list;
2842 // struct _objc_method_description_list *instance_methods;
2843 // struct _objc_method_description_list *class_methods;
2844 // }
2845 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002846 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002847 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2848 MethodDescriptionListPtrTy,
2849 MethodDescriptionListPtrTy,
2850 NULL);
2851 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2852
2853 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2854 CGM.getModule().addTypeName("struct._objc_protocol_list",
2855 ProtocolListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002856 // struct _objc_protocol_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002857 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2858
2859 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002860 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002861 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002862
2863 // Class description structures
2864
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002865 // struct _objc_ivar {
2866 // char *ivar_name;
2867 // char *ivar_type;
2868 // int ivar_offset;
2869 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002870 IvarTy = llvm::StructType::get(Int8PtrTy,
2871 Int8PtrTy,
2872 IntTy,
2873 NULL);
2874 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2875
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002876 // struct _objc_ivar_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002877 IvarListTy = llvm::OpaqueType::get();
2878 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2879 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2880
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002881 // struct _objc_method_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002882 MethodListTy = llvm::OpaqueType::get();
2883 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2884 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2885
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002886 // struct _objc_class_extension *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002887 ClassExtensionTy =
2888 llvm::StructType::get(IntTy,
2889 Int8PtrTy,
2890 PropertyListPtrTy,
2891 NULL);
2892 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2893 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2894
2895 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2896
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002897 // struct _objc_class {
2898 // Class isa;
2899 // Class super_class;
2900 // char *name;
2901 // long version;
2902 // long info;
2903 // long instance_size;
2904 // struct _objc_ivar_list *ivars;
2905 // struct _objc_method_list *methods;
2906 // struct _objc_cache *cache;
2907 // struct _objc_protocol_list *protocols;
2908 // char *ivar_layout;
2909 // struct _objc_class_ext *ext;
2910 // };
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002911 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2912 llvm::PointerType::getUnqual(ClassTyHolder),
2913 Int8PtrTy,
2914 LongTy,
2915 LongTy,
2916 LongTy,
2917 IvarListPtrTy,
2918 MethodListPtrTy,
2919 CachePtrTy,
2920 ProtocolListPtrTy,
2921 Int8PtrTy,
2922 ClassExtensionPtrTy,
2923 NULL);
2924 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2925
2926 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2927 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2928 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2929
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002930 // struct _objc_category {
2931 // char *category_name;
2932 // char *class_name;
2933 // struct _objc_method_list *instance_method;
2934 // struct _objc_method_list *class_method;
2935 // uint32_t size; // sizeof(struct _objc_category)
2936 // struct _objc_property_list *instance_properties;// category's @property
2937 // }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002938 CategoryTy = llvm::StructType::get(Int8PtrTy,
2939 Int8PtrTy,
2940 MethodListPtrTy,
2941 MethodListPtrTy,
2942 ProtocolListPtrTy,
2943 IntTy,
2944 PropertyListPtrTy,
2945 NULL);
2946 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2947
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002948 // Global metadata structures
2949
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002950 // struct _objc_symtab {
2951 // long sel_ref_cnt;
2952 // SEL *refs;
2953 // short cls_def_cnt;
2954 // short cat_def_cnt;
2955 // char *defs[cls_def_cnt + cat_def_cnt];
2956 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002957 SymtabTy = llvm::StructType::get(LongTy,
2958 SelectorPtrTy,
2959 ShortTy,
2960 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002961 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002962 NULL);
2963 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2964 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2965
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002966 // struct _objc_module {
2967 // long version;
2968 // long size; // sizeof(struct _objc_module)
2969 // char *name;
2970 // struct _objc_symtab* symtab;
2971 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002972 ModuleTy =
2973 llvm::StructType::get(LongTy,
2974 LongTy,
2975 Int8PtrTy,
2976 SymtabPtrTy,
2977 NULL);
2978 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002979
Daniel Dunbarf7103722008-09-24 03:38:44 +00002980 // Message send functions.
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002981
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002982 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002983 std::vector<const llvm::Type*> Params;
2984 Params.push_back(ObjectPtrTy);
2985 Params.push_back(SelectorPtrTy);
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002986 MessageSendFn =
2987 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2988 Params,
2989 true),
2990 "objc_msgSend");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002991
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002992 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002993 Params.clear();
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002994 Params.push_back(ObjectPtrTy);
2995 Params.push_back(SelectorPtrTy);
2996 MessageSendStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002997 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2998 Params,
2999 true),
3000 "objc_msgSend_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00003001
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003002 //
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00003003 Params.clear();
3004 Params.push_back(ObjectPtrTy);
3005 Params.push_back(SelectorPtrTy);
3006 // FIXME: This should be long double on x86_64?
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003007 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00003008 MessageSendFpretFn =
3009 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3010 Params,
3011 true),
3012 "objc_msgSend_fpret");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003013
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003014 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003015 Params.clear();
3016 Params.push_back(SuperPtrTy);
3017 Params.push_back(SelectorPtrTy);
3018 MessageSendSuperFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003019 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3020 Params,
3021 true),
3022 "objc_msgSendSuper");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003023
Fariborz Jahanian4b161702009-01-22 00:37:21 +00003024 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3025 // SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00003026 Params.clear();
3027 Params.push_back(Int8PtrTy);
3028 Params.push_back(SuperPtrTy);
3029 Params.push_back(SelectorPtrTy);
3030 MessageSendSuperStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003031 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3032 Params,
3033 true),
3034 "objc_msgSendSuper_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00003035
3036 // There is no objc_msgSendSuper_fpret? How can that work?
3037 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson58d16242008-08-31 04:05:03 +00003038
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003039 // FIXME: This is the size of the setjmp buffer and should be
3040 // target specific. 18 is what's used on 32-bit X86.
3041 uint64_t SetJmpBufferSize = 18;
3042
3043 // Exceptions
3044 const llvm::Type *StackPtrTy =
Daniel Dunbar1c5e4632008-09-27 06:32:25 +00003045 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003046
3047 ExceptionDataTy =
3048 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3049 SetJmpBufferSize),
3050 StackPtrTy, NULL);
3051 CGM.getModule().addTypeName("struct._objc_exception_data",
3052 ExceptionDataTy);
3053
3054 Params.clear();
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003055 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3056 ExceptionTryEnterFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003057 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3058 Params,
3059 false),
3060 "objc_exception_try_enter");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003061 ExceptionTryExitFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003062 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3063 Params,
3064 false),
3065 "objc_exception_try_exit");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003066 ExceptionExtractFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003067 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3068 Params,
3069 false),
3070 "objc_exception_extract");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003071
3072 Params.clear();
3073 Params.push_back(ClassPtrTy);
3074 Params.push_back(ObjectPtrTy);
3075 ExceptionMatchFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003076 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3077 Params,
3078 false),
3079 "objc_exception_match");
Chris Lattnerdd978702008-11-15 21:26:17 +00003080
Anders Carlsson9acb0a42008-09-09 10:10:21 +00003081 Params.clear();
3082 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3083 SetJmpFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00003084 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3085 Params,
3086 false),
3087 "_setjmp");
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00003088
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003089}
3090
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003091ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00003092: ObjCCommonTypesHelper(cgm)
3093{
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003094 // struct _method_list_t {
3095 // uint32_t entsize; // sizeof(struct _objc_method)
3096 // uint32_t method_count;
3097 // struct _objc_method method_list[method_count];
3098 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003099 MethodListnfABITy = llvm::StructType::get(IntTy,
3100 IntTy,
3101 llvm::ArrayType::get(MethodTy, 0),
3102 NULL);
3103 CGM.getModule().addTypeName("struct.__method_list_t",
3104 MethodListnfABITy);
3105 // struct method_list_t *
3106 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003107
3108 // struct _protocol_t {
3109 // id isa; // NULL
3110 // const char * const protocol_name;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003111 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003112 // const struct method_list_t * const instance_methods;
3113 // const struct method_list_t * const class_methods;
3114 // const struct method_list_t *optionalInstanceMethods;
3115 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003116 // const struct _prop_list_t * properties;
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003117 // const uint32_t size; // sizeof(struct _protocol_t)
3118 // const uint32_t flags; // = 0
3119 // }
3120
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003121 // Holder for struct _protocol_list_t *
3122 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3123
3124 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3125 Int8PtrTy,
3126 llvm::PointerType::getUnqual(
3127 ProtocolListTyHolder),
3128 MethodListnfABIPtrTy,
3129 MethodListnfABIPtrTy,
3130 MethodListnfABIPtrTy,
3131 MethodListnfABIPtrTy,
3132 PropertyListPtrTy,
3133 IntTy,
3134 IntTy,
3135 NULL);
3136 CGM.getModule().addTypeName("struct._protocol_t",
3137 ProtocolnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003138
3139 // struct _protocol_t*
3140 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003141
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003142 // struct _protocol_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003143 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003144 // struct _protocol_t *[protocol_count];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003145 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003146 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3147 llvm::ArrayType::get(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003148 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003149 NULL);
3150 CGM.getModule().addTypeName("struct._objc_protocol_list",
3151 ProtocolListnfABITy);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003152 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3153 ProtocolListnfABITy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003154
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003155 // struct _objc_protocol_list*
3156 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003157
3158 // struct _ivar_t {
3159 // unsigned long int *offset; // pointer to ivar offset location
3160 // char *name;
3161 // char *type;
3162 // uint32_t alignment;
3163 // uint32_t size;
3164 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003165 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3166 Int8PtrTy,
3167 Int8PtrTy,
3168 IntTy,
3169 IntTy,
3170 NULL);
3171 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3172
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003173 // struct _ivar_list_t {
3174 // uint32 entsize; // sizeof(struct _ivar_t)
3175 // uint32 count;
3176 // struct _iver_t list[count];
3177 // }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003178 IvarListnfABITy = llvm::StructType::get(IntTy,
3179 IntTy,
3180 llvm::ArrayType::get(
3181 IvarnfABITy, 0),
3182 NULL);
3183 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3184
3185 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003186
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003187 // struct _class_ro_t {
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003188 // uint32_t const flags;
3189 // uint32_t const instanceStart;
3190 // uint32_t const instanceSize;
3191 // uint32_t const reserved; // only when building for 64bit targets
3192 // const uint8_t * const ivarLayout;
3193 // const char *const name;
3194 // const struct _method_list_t * const baseMethods;
3195 // const struct _objc_protocol_list *const baseProtocols;
3196 // const struct _ivar_list_t *const ivars;
3197 // const uint8_t * const weakIvarLayout;
3198 // const struct _prop_list_t * const properties;
3199 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003200
3201 // FIXME. Add 'reserved' field in 64bit abi mode!
3202 ClassRonfABITy = llvm::StructType::get(IntTy,
3203 IntTy,
3204 IntTy,
3205 Int8PtrTy,
3206 Int8PtrTy,
3207 MethodListnfABIPtrTy,
3208 ProtocolListnfABIPtrTy,
3209 IvarListnfABIPtrTy,
3210 Int8PtrTy,
3211 PropertyListPtrTy,
3212 NULL);
3213 CGM.getModule().addTypeName("struct._class_ro_t",
3214 ClassRonfABITy);
3215
3216 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3217 std::vector<const llvm::Type*> Params;
3218 Params.push_back(ObjectPtrTy);
3219 Params.push_back(SelectorPtrTy);
3220 ImpnfABITy = llvm::PointerType::getUnqual(
3221 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3222
3223 // struct _class_t {
3224 // struct _class_t *isa;
3225 // struct _class_t * const superclass;
3226 // void *cache;
3227 // IMP *vtable;
3228 // struct class_ro_t *ro;
3229 // }
3230
3231 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3232 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3233 llvm::PointerType::getUnqual(ClassTyHolder),
3234 CachePtrTy,
3235 llvm::PointerType::getUnqual(ImpnfABITy),
3236 llvm::PointerType::getUnqual(
3237 ClassRonfABITy),
3238 NULL);
3239 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3240
3241 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3242 ClassnfABITy);
3243
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003244 // LLVM for struct _class_t *
3245 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3246
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003247 // struct _category_t {
3248 // const char * const name;
3249 // struct _class_t *const cls;
3250 // const struct _method_list_t * const instance_methods;
3251 // const struct _method_list_t * const class_methods;
3252 // const struct _protocol_list_t * const protocols;
3253 // const struct _prop_list_t * const properties;
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003254 // }
3255 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003256 ClassnfABIPtrTy,
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003257 MethodListnfABIPtrTy,
3258 MethodListnfABIPtrTy,
3259 ProtocolListnfABIPtrTy,
3260 PropertyListPtrTy,
3261 NULL);
3262 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003263
3264 // New types for nonfragile abi messaging.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003265 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3266 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003267
3268 // MessageRefTy - LLVM for:
3269 // struct _message_ref_t {
3270 // IMP messenger;
3271 // SEL name;
3272 // };
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003273
3274 // First the clang type for struct _message_ref_t
3275 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3276 SourceLocation(),
3277 &Ctx.Idents.get("_message_ref_t"));
3278 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3279 Ctx.VoidPtrTy, 0, false));
3280 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3281 Ctx.getObjCSelType(), 0, false));
3282 RD->completeDefinition(Ctx);
3283
3284 MessageRefCTy = Ctx.getTagDeclType(RD);
3285 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3286 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003287
3288 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3289 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3290
3291 // SuperMessageRefTy - LLVM for:
3292 // struct _super_message_ref_t {
3293 // SUPER_IMP messenger;
3294 // SEL name;
3295 // };
3296 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3297 SelectorPtrTy,
3298 NULL);
3299 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3300
3301 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3302 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3303
3304 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3305 Params.clear();
3306 Params.push_back(ObjectPtrTy);
3307 Params.push_back(MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00003308 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3309 Params,
3310 true);
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003311 MessageSendFixupFn =
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00003312 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003313 "objc_msgSend_fixup");
3314
3315 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3316 MessageSendFpretFixupFn =
3317 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3318 Params,
3319 true),
3320 "objc_msgSend_fpret_fixup");
3321
3322 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3323 MessageSendStretFixupFn =
3324 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3325 Params,
3326 true),
3327 "objc_msgSend_stret_fixup");
3328
3329 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3330 MessageSendIdFixupFn =
3331 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3332 Params,
3333 true),
3334 "objc_msgSendId_fixup");
3335
3336
3337 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3338 MessageSendIdStretFixupFn =
3339 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3340 Params,
3341 true),
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00003342 "objc_msgSendId_stret_fixup");
Fariborz Jahanian711e8dd2009-02-03 23:49:23 +00003343
3344 // id objc_msgSendSuper2_fixup (struct objc_super *,
3345 // struct _super_message_ref_t*, ...)
3346 Params.clear();
3347 Params.push_back(SuperPtrTy);
3348 Params.push_back(SuperMessageRefPtrTy);
3349 MessageSendSuper2FixupFn =
3350 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3351 Params,
3352 true),
3353 "objc_msgSendSuper2_fixup");
3354
3355
3356 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3357 // struct _super_message_ref_t*, ...)
3358 MessageSendSuper2StretFixupFn =
3359 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3360 Params,
3361 true),
3362 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar75de89f2009-02-24 07:47:38 +00003363
3364 Params.clear();
3365 llvm::Constant *Personality =
3366 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3367 Params,
3368 true),
3369 "__objc_personality_v0");
3370 EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
3371
3372 Params.clear();
3373 Params.push_back(Int8PtrTy);
3374 UnwindResumeOrRethrowFn =
3375 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3376 Params,
3377 false),
3378 "_Unwind_Resume_or_Rethrow");
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003379}
3380
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003381llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3382 FinishNonFragileABIModule();
3383
3384 return NULL;
3385}
3386
3387void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3388 // nonfragile abi has no module definition.
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003389
3390 // Build list of all implemented classe addresses in array
3391 // L_OBJC_LABEL_CLASS_$.
3392 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3393 // list of 'nonlazy' implementations (defined as those with a +load{}
3394 // method!!).
3395 unsigned NumClasses = DefinedClasses.size();
3396 if (NumClasses) {
3397 std::vector<llvm::Constant*> Symbols(NumClasses);
3398 for (unsigned i=0; i<NumClasses; i++)
3399 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3400 ObjCTypes.Int8PtrTy);
3401 llvm::Constant* Init =
3402 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3403 NumClasses),
3404 Symbols);
3405
3406 llvm::GlobalVariable *GV =
3407 new llvm::GlobalVariable(Init->getType(), false,
3408 llvm::GlobalValue::InternalLinkage,
3409 Init,
3410 "\01L_OBJC_LABEL_CLASS_$",
3411 &CGM.getModule());
Fariborz Jahanianc5a02552009-02-28 00:54:00 +00003412 GV->setAlignment(
3413 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003414 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3415 UsedGlobals.push_back(GV);
3416 }
3417
3418 // Build list of all implemented category addresses in array
3419 // L_OBJC_LABEL_CATEGORY_$.
3420 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3421 // list of 'nonlazy' category implementations (defined as those with a +load{}
3422 // method!!).
3423 unsigned NumCategory = DefinedCategories.size();
3424 if (NumCategory) {
3425 std::vector<llvm::Constant*> Symbols(NumCategory);
3426 for (unsigned i=0; i<NumCategory; i++)
3427 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3428 ObjCTypes.Int8PtrTy);
3429 llvm::Constant* Init =
3430 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3431 NumCategory),
3432 Symbols);
3433
3434 llvm::GlobalVariable *GV =
3435 new llvm::GlobalVariable(Init->getType(), false,
3436 llvm::GlobalValue::InternalLinkage,
3437 Init,
3438 "\01L_OBJC_LABEL_CATEGORY_$",
3439 &CGM.getModule());
3440 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3441 UsedGlobals.push_back(GV);
3442 }
3443
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00003444 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3445 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3446 std::vector<llvm::Constant*> Values(2);
3447 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian4d7933a2009-02-24 21:08:09 +00003448 unsigned int flags = 0;
Fariborz Jahanian27f58962009-02-24 23:34:44 +00003449 // FIXME: Fix and continue?
3450 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3451 flags |= eImageInfo_GarbageCollected;
3452 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3453 flags |= eImageInfo_GCOnly;
Fariborz Jahanian4d7933a2009-02-24 21:08:09 +00003454 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00003455 llvm::Constant* Init = llvm::ConstantArray::get(
3456 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3457 Values);
3458 llvm::GlobalVariable *IMGV =
3459 new llvm::GlobalVariable(Init->getType(), false,
3460 llvm::GlobalValue::InternalLinkage,
3461 Init,
3462 "\01L_OBJC_IMAGE_INFO",
3463 &CGM.getModule());
3464 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3465 UsedGlobals.push_back(IMGV);
3466
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003467 std::vector<llvm::Constant*> Used;
3468 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3469 e = UsedGlobals.end(); i != e; ++i) {
3470 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3471 }
3472
3473 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3474 llvm::GlobalValue *GV =
3475 new llvm::GlobalVariable(AT, false,
3476 llvm::GlobalValue::AppendingLinkage,
3477 llvm::ConstantArray::get(AT, Used),
3478 "llvm.used",
3479 &CGM.getModule());
3480
3481 GV->setSection("llvm.metadata");
3482
3483}
3484
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003485// Metadata flags
3486enum MetaDataDlags {
3487 CLS = 0x0,
3488 CLS_META = 0x1,
3489 CLS_ROOT = 0x2,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003490 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003491 CLS_EXCEPTION = 0x20
3492};
3493/// BuildClassRoTInitializer - generate meta-data for:
3494/// struct _class_ro_t {
3495/// uint32_t const flags;
3496/// uint32_t const instanceStart;
3497/// uint32_t const instanceSize;
3498/// uint32_t const reserved; // only when building for 64bit targets
3499/// const uint8_t * const ivarLayout;
3500/// const char *const name;
3501/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003502/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003503/// const struct _ivar_list_t *const ivars;
3504/// const uint8_t * const weakIvarLayout;
3505/// const struct _prop_list_t * const properties;
3506/// }
3507///
3508llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3509 unsigned flags,
3510 unsigned InstanceStart,
3511 unsigned InstanceSize,
3512 const ObjCImplementationDecl *ID) {
3513 std::string ClassName = ID->getNameAsString();
3514 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3515 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3516 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3517 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3518 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003519 // FIXME. ivarLayout is currently null!
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003520 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3521 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003522 // const struct _method_list_t * const baseMethods;
3523 std::vector<llvm::Constant*> Methods;
3524 std::string MethodListName("\01l_OBJC_$_");
3525 if (flags & CLS_META) {
3526 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3527 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3528 e = ID->classmeth_end(); i != e; ++i) {
3529 // Class methods should always be defined.
3530 Methods.push_back(GetMethodConstant(*i));
3531 }
3532 } else {
3533 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3534 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3535 e = ID->instmeth_end(); i != e; ++i) {
3536 // Instance methods should always be defined.
3537 Methods.push_back(GetMethodConstant(*i));
3538 }
Fariborz Jahanian78355ec2009-01-28 22:46:49 +00003539 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3540 e = ID->propimpl_end(); i != e; ++i) {
3541 ObjCPropertyImplDecl *PID = *i;
3542
3543 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3544 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3545
3546 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3547 if (llvm::Constant *C = GetMethodConstant(MD))
3548 Methods.push_back(C);
3549 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3550 if (llvm::Constant *C = GetMethodConstant(MD))
3551 Methods.push_back(C);
3552 }
3553 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003554 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003555 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003556 "__DATA, __objc_const", Methods);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003557
3558 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3559 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3560 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3561 + OID->getNameAsString(),
3562 OID->protocol_begin(),
3563 OID->protocol_end());
3564
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003565 if (flags & CLS_META)
3566 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3567 else
3568 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003569 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003570 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003571 if (flags & CLS_META)
3572 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3573 else
3574 Values[ 9] =
3575 EmitPropertyList(
3576 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3577 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003578 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3579 Values);
3580 llvm::GlobalVariable *CLASS_RO_GV =
3581 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3582 llvm::GlobalValue::InternalLinkage,
3583 Init,
3584 (flags & CLS_META) ?
3585 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3586 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3587 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003588 CLASS_RO_GV->setAlignment(
3589 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003590 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003591 UsedGlobals.push_back(CLASS_RO_GV);
3592 return CLASS_RO_GV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003593
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003594}
3595
3596/// BuildClassMetaData - This routine defines that to-level meta-data
3597/// for the given ClassName for:
3598/// struct _class_t {
3599/// struct _class_t *isa;
3600/// struct _class_t * const superclass;
3601/// void *cache;
3602/// IMP *vtable;
3603/// struct class_ro_t *ro;
3604/// }
3605///
Fariborz Jahanian06726462009-01-24 21:21:53 +00003606llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3607 std::string &ClassName,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003608 llvm::Constant *IsAGV,
3609 llvm::Constant *SuperClassGV,
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003610 llvm::Constant *ClassRoGV,
3611 bool HiddenVisibility) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003612 std::vector<llvm::Constant*> Values(5);
3613 Values[0] = IsAGV;
Fariborz Jahanian06726462009-01-24 21:21:53 +00003614 Values[1] = SuperClassGV
3615 ? SuperClassGV
3616 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003617 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3618 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3619 Values[4] = ClassRoGV; // &CLASS_RO_GV
3620 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3621 Values);
3622 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3623 if (GV)
3624 GV->setInitializer(Init);
3625 else
3626 GV =
3627 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3628 llvm::GlobalValue::ExternalLinkage,
3629 Init,
3630 ClassName,
3631 &CGM.getModule());
Fariborz Jahanian7c891592009-01-31 01:07:39 +00003632 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003633 GV->setAlignment(
3634 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003635 if (HiddenVisibility)
3636 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003637 UsedGlobals.push_back(GV);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003638 return GV;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003639}
3640
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003641void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3642 std::string ClassName = ID->getNameAsString();
3643 if (!ObjCEmptyCacheVar) {
3644 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003645 ObjCTypes.CacheTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003646 false,
3647 llvm::GlobalValue::ExternalLinkage,
3648 0,
Fariborz Jahaniane45fbce2009-02-03 17:34:34 +00003649 "\01__objc_empty_cache",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003650 &CGM.getModule());
3651 UsedGlobals.push_back(ObjCEmptyCacheVar);
3652
3653 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00003654 ObjCTypes.ImpnfABITy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003655 false,
3656 llvm::GlobalValue::ExternalLinkage,
3657 0,
Fariborz Jahaniane45fbce2009-02-03 17:34:34 +00003658 "\01__objc_empty_vtable",
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003659 &CGM.getModule());
3660 UsedGlobals.push_back(ObjCEmptyVtableVar);
3661 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003662 assert(ID->getClassInterface() &&
3663 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003664 uint32_t InstanceStart =
3665 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3666 uint32_t InstanceSize = InstanceStart;
3667 uint32_t flags = CLS_META;
3668 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3669 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003670
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003671 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003672
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003673 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3674 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003675 flags |= OBJC2_CLS_HIDDEN;
3676 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003677 // class is root
3678 flags |= CLS_ROOT;
3679 std::string SuperClassName = ObjCClassName + ClassName;
3680 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003681 if (!SuperClassGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003682 SuperClassGV =
3683 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3684 llvm::GlobalValue::ExternalLinkage,
3685 0,
3686 SuperClassName,
3687 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003688 UsedGlobals.push_back(SuperClassGV);
3689 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003690 std::string IsAClassName = ObjCMetaClassName + ClassName;
3691 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003692 if (!IsAGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003693 IsAGV =
3694 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3695 llvm::GlobalValue::ExternalLinkage,
3696 0,
3697 IsAClassName,
3698 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003699 UsedGlobals.push_back(IsAGV);
3700 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003701 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003702 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00003703 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3704 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3705 Root = Super;
3706 std::string RootClassName = ObjCMetaClassName + Root->getNameAsString();
3707 IsAGV = CGM.getModule().getGlobalVariable(RootClassName);
3708 if (!IsAGV) {
3709 IsAGV =
3710 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3711 llvm::GlobalValue::ExternalLinkage,
3712 0,
3713 RootClassName,
3714 &CGM.getModule());
3715 UsedGlobals.push_back(IsAGV);
3716 }
3717 // work on super class metadata symbol.
3718 std::string SuperClassName =
3719 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003720 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003721 if (!SuperClassGV) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003722 SuperClassGV =
3723 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3724 llvm::GlobalValue::ExternalLinkage,
3725 0,
3726 SuperClassName,
3727 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003728 UsedGlobals.push_back(SuperClassGV);
3729 }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003730 }
3731 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3732 InstanceStart,
3733 InstanceSize,ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003734 std::string TClassName = ObjCMetaClassName + ClassName;
3735 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003736 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3737 classIsHidden);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003738
3739 // Metadata for the class
3740 flags = CLS;
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003741 if (classIsHidden)
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003742 flags |= OBJC2_CLS_HIDDEN;
3743 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003744 flags |= CLS_ROOT;
3745 SuperClassGV = 0;
3746 }
3747 else {
3748 // Has a root. Current class is not a root.
Fariborz Jahanian514c63b2009-02-26 18:23:47 +00003749 std::string RootClassName =
Fariborz Jahanian06726462009-01-24 21:21:53 +00003750 ID->getClassInterface()->getSuperClass()->getNameAsString();
3751 std::string SuperClassName = ObjCClassName + RootClassName;
3752 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003753 if (!SuperClassGV) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003754 SuperClassGV =
3755 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3756 llvm::GlobalValue::ExternalLinkage,
3757 0,
3758 SuperClassName,
3759 &CGM.getModule());
Fariborz Jahanian917c0402009-02-05 20:41:40 +00003760 UsedGlobals.push_back(SuperClassGV);
3761 }
Fariborz Jahanian06726462009-01-24 21:21:53 +00003762 }
3763
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003764 InstanceStart = InstanceSize = 0;
3765 if (ObjCInterfaceDecl *OID =
3766 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3767 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003768 const llvm::Type *InterfaceTy =
Fariborz Jahanian27ecc672009-02-14 20:13:28 +00003769 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003770 const llvm::StructLayout *Layout =
3771 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003772
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003773 RecordDecl::field_iterator firstField, lastField;
3774 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003775
3776 for (RecordDecl::field_iterator e = RD->field_end(),
3777 ifield = firstField; ifield != e; ++ifield)
3778 lastField = ifield;
3779
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003780 if (lastField != RD->field_end()) {
3781 FieldDecl *Field = *lastField;
3782 const llvm::Type *FieldTy =
3783 CGM.getTypes().ConvertTypeForMem(Field->getType());
3784 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3785 InstanceSize = Layout->getElementOffset(
3786 CGM.getTypes().getLLVMFieldNo(Field)) +
3787 Size;
3788 if (firstField == RD->field_end())
3789 InstanceStart = InstanceSize;
3790 else
3791 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3792 getLLVMFieldNo(*firstField));
3793 }
3794 }
Fariborz Jahanian06726462009-01-24 21:21:53 +00003795 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003796 InstanceStart,
3797 InstanceSize,
3798 ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003799
3800 TClassName = ObjCClassName + ClassName;
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003801 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00003802 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3803 classIsHidden);
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003804 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003805}
3806
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00003807/// GenerateProtocolRef - This routine is called to generate code for
3808/// a protocol reference expression; as in:
3809/// @code
3810/// @protocol(Proto1);
3811/// @endcode
3812/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3813/// which will hold address of the protocol meta-data.
3814///
3815llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3816 const ObjCProtocolDecl *PD) {
3817
3818 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3819 ObjCTypes.ExternalProtocolPtrTy);
3820
3821 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3822 ProtocolName += PD->getNameAsCString();
3823
3824 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3825 if (PTGV)
3826 return Builder.CreateLoad(PTGV, false, "tmp");
3827 PTGV = new llvm::GlobalVariable(
3828 Init->getType(), false,
3829 llvm::GlobalValue::WeakLinkage,
3830 Init,
3831 ProtocolName,
3832 &CGM.getModule());
3833 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3834 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3835 UsedGlobals.push_back(PTGV);
3836 return Builder.CreateLoad(PTGV, false, "tmp");
3837}
3838
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003839/// GenerateCategory - Build metadata for a category implementation.
3840/// struct _category_t {
3841/// const char * const name;
3842/// struct _class_t *const cls;
3843/// const struct _method_list_t * const instance_methods;
3844/// const struct _method_list_t * const class_methods;
3845/// const struct _protocol_list_t * const protocols;
3846/// const struct _prop_list_t * const properties;
3847/// }
3848///
3849void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3850{
3851 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003852 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3853 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003854 "_$_" + OCD->getNameAsString());
3855 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3856
3857 std::vector<llvm::Constant*> Values(6);
3858 Values[0] = GetClassName(OCD->getIdentifier());
3859 // meta-class entry symbol
3860 llvm::GlobalVariable *ClassGV =
3861 CGM.getModule().getGlobalVariable(ExtClassName);
3862 if (!ClassGV)
3863 ClassGV =
3864 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3865 llvm::GlobalValue::ExternalLinkage,
3866 0,
3867 ExtClassName,
3868 &CGM.getModule());
3869 UsedGlobals.push_back(ClassGV);
3870 Values[1] = ClassGV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003871 std::vector<llvm::Constant*> Methods;
3872 std::string MethodListName(Prefix);
3873 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3874 "_$_" + OCD->getNameAsString();
3875
3876 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3877 e = OCD->instmeth_end(); i != e; ++i) {
3878 // Instance methods should always be defined.
3879 Methods.push_back(GetMethodConstant(*i));
3880 }
3881
3882 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003883 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003884 Methods);
3885
3886 MethodListName = Prefix;
3887 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3888 OCD->getNameAsString();
3889 Methods.clear();
3890 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3891 e = OCD->classmeth_end(); i != e; ++i) {
3892 // Class methods should always be defined.
3893 Methods.push_back(GetMethodConstant(*i));
3894 }
3895
3896 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003897 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003898 Methods);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003899 const ObjCCategoryDecl *Category =
3900 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian8c7904b2009-02-13 17:52:22 +00003901 if (Category) {
3902 std::string ExtName(Interface->getNameAsString() + "_$_" +
3903 OCD->getNameAsString());
3904 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3905 + Interface->getNameAsString() + "_$_"
3906 + Category->getNameAsString(),
3907 Category->protocol_begin(),
3908 Category->protocol_end());
3909 Values[5] =
3910 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3911 OCD, Category, ObjCTypes);
3912 }
3913 else {
3914 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3915 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3916 }
3917
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003918 llvm::Constant *Init =
3919 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3920 Values);
3921 llvm::GlobalVariable *GCATV
3922 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3923 false,
3924 llvm::GlobalValue::InternalLinkage,
3925 Init,
3926 ExtCatName,
3927 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003928 GCATV->setAlignment(
3929 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003930 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003931 UsedGlobals.push_back(GCATV);
3932 DefinedCategories.push_back(GCATV);
3933}
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003934
3935/// GetMethodConstant - Return a struct objc_method constant for the
3936/// given method if it has been defined. The result is null if the
3937/// method has not been defined. The return value has type MethodPtrTy.
3938llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3939 const ObjCMethodDecl *MD) {
3940 // FIXME: Use DenseMap::lookup
3941 llvm::Function *Fn = MethodDefinitions[MD];
3942 if (!Fn)
3943 return 0;
3944
3945 std::vector<llvm::Constant*> Method(3);
3946 Method[0] =
3947 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3948 ObjCTypes.SelectorPtrTy);
3949 Method[1] = GetMethodVarType(MD);
3950 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3951 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3952}
3953
3954/// EmitMethodList - Build meta-data for method declarations
3955/// struct _method_list_t {
3956/// uint32_t entsize; // sizeof(struct _objc_method)
3957/// uint32_t method_count;
3958/// struct _objc_method method_list[method_count];
3959/// }
3960///
3961llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3962 const std::string &Name,
3963 const char *Section,
3964 const ConstantVector &Methods) {
3965 // Return null for empty list.
3966 if (Methods.empty())
3967 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3968
3969 std::vector<llvm::Constant*> Values(3);
3970 // sizeof(struct _objc_method)
3971 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3972 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3973 // method_count
3974 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3975 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3976 Methods.size());
3977 Values[2] = llvm::ConstantArray::get(AT, Methods);
3978 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3979
3980 llvm::GlobalVariable *GV =
3981 new llvm::GlobalVariable(Init->getType(), false,
3982 llvm::GlobalValue::InternalLinkage,
3983 Init,
3984 Name,
3985 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00003986 GV->setAlignment(
3987 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003988 GV->setSection(Section);
3989 UsedGlobals.push_back(GV);
3990 return llvm::ConstantExpr::getBitCast(GV,
3991 ObjCTypes.MethodListnfABIPtrTy);
3992}
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003993
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00003994/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3995/// the given ivar.
3996///
3997llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3998 std::string &Name,
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00003999 const ObjCInterfaceDecl *ID,
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004000 const ObjCIvarDecl *Ivar) {
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00004001 Name += "\01_OBJC_IVAR_$_" +
4002 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004003 + Ivar->getNameAsString();
4004 llvm::GlobalVariable *IvarOffsetGV =
4005 CGM.getModule().getGlobalVariable(Name);
4006 if (!IvarOffsetGV)
4007 IvarOffsetGV =
4008 new llvm::GlobalVariable(ObjCTypes.LongTy,
4009 false,
4010 llvm::GlobalValue::ExternalLinkage,
4011 0,
4012 Name,
4013 &CGM.getModule());
4014 return IvarOffsetGV;
4015}
4016
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004017llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004018 const ObjCInterfaceDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004019 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004020 unsigned long int Offset) {
4021
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004022 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004023 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004024 + Ivar->getNameAsString());
Fariborz Jahanian55343922009-02-03 00:09:52 +00004025 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004026
4027 llvm::GlobalVariable *IvarOffsetGV =
4028 CGM.getModule().getGlobalVariable(ExternalName);
4029 if (IvarOffsetGV) {
4030 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004031 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004032 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004033 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian55343922009-02-03 00:09:52 +00004034 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004035 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004036 return IvarOffsetGV;
4037 }
4038
4039 IvarOffsetGV =
4040 new llvm::GlobalVariable(Init->getType(),
4041 false,
4042 llvm::GlobalValue::ExternalLinkage,
4043 Init,
4044 ExternalName,
4045 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004046 IvarOffsetGV->setAlignment(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004047 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004048 // @private and @package have hidden visibility.
4049 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4050 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4051 if (!globalVisibility)
4052 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004053 else
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004054 if (IsClassHidden(ID))
4055 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian51dcacb2009-01-31 00:59:10 +00004056
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004057 IvarOffsetGV->setSection("__DATA, __objc_const");
4058 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian55343922009-02-03 00:09:52 +00004059 return IvarOffsetGV;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004060}
4061
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004062/// EmitIvarList - Emit the ivar list for the given
4063/// implementation. If ForClass is true the list of class ivars
4064/// (i.e. metaclass ivars) is emitted, otherwise the list of
4065/// interface ivars will be emitted. The return value has type
4066/// IvarListnfABIPtrTy.
4067/// struct _ivar_t {
4068/// unsigned long int *offset; // pointer to ivar offset location
4069/// char *name;
4070/// char *type;
4071/// uint32_t alignment;
4072/// uint32_t size;
4073/// }
4074/// struct _ivar_list_t {
4075/// uint32 entsize; // sizeof(struct _ivar_t)
4076/// uint32 count;
4077/// struct _iver_t list[count];
4078/// }
4079///
4080llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4081 const ObjCImplementationDecl *ID) {
4082
4083 std::vector<llvm::Constant*> Ivars, Ivar(5);
4084
4085 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4086 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4087
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004088 // FIXME. Consolidate this with similar code in GenerateClass.
4089 const llvm::Type *InterfaceTy =
4090 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4091 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004092 const llvm::StructLayout *Layout =
4093 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00004094
4095 RecordDecl::field_iterator i,p;
4096 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004097 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4098
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004099 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004100 FieldDecl *Field = *i;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004101 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4102 getLLVMFieldNo(Field));
Fariborz Jahanian150f7732009-01-28 01:36:42 +00004103 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004104 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004105 if (Field->getIdentifier())
4106 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4107 else
4108 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4109 std::string TypeStr;
4110 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4111 Ivar[2] = GetMethodVarType(TypeStr);
4112 const llvm::Type *FieldTy =
4113 CGM.getTypes().ConvertTypeForMem(Field->getType());
4114 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4115 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4116 Field->getType().getTypePtr()) >> 3;
4117 Align = llvm::Log2_32(Align);
4118 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian71bcf672009-01-27 22:27:56 +00004119 // NOTE. Size of a bitfield does not match gcc's, because of the way
4120 // bitfields are treated special in each. But I am told that 'size'
4121 // for bitfield ivars is ignored by the runtime so it does not matter.
4122 // (even if it matters, some day, there is enough info. to get the bitfield
4123 // right!
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004124 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4125 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4126 }
4127 // Return null for empty list.
4128 if (Ivars.empty())
4129 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4130 std::vector<llvm::Constant*> Values(3);
4131 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4132 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4133 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4134 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4135 Ivars.size());
4136 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4137 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4138 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4139 llvm::GlobalVariable *GV =
4140 new llvm::GlobalVariable(Init->getType(), false,
4141 llvm::GlobalValue::InternalLinkage,
4142 Init,
4143 Prefix + OID->getNameAsString(),
4144 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004145 GV->setAlignment(
4146 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00004147 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00004148
4149 UsedGlobals.push_back(GV);
4150 return llvm::ConstantExpr::getBitCast(GV,
4151 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004152}
4153
4154llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4155 const ObjCProtocolDecl *PD) {
4156 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4157
4158 if (!Entry) {
4159 // We use the initializer as a marker of whether this is a forward
4160 // reference or not. At module finalization we add the empty
4161 // contents for protocols which were referenced but never defined.
4162 Entry =
4163 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4164 llvm::GlobalValue::ExternalLinkage,
4165 0,
4166 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4167 &CGM.getModule());
4168 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4169 UsedGlobals.push_back(Entry);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004170 }
4171
4172 return Entry;
4173}
4174
4175/// GetOrEmitProtocol - Generate the protocol meta-data:
4176/// @code
4177/// struct _protocol_t {
4178/// id isa; // NULL
4179/// const char * const protocol_name;
4180/// const struct _protocol_list_t * protocol_list; // super protocols
4181/// const struct method_list_t * const instance_methods;
4182/// const struct method_list_t * const class_methods;
4183/// const struct method_list_t *optionalInstanceMethods;
4184/// const struct method_list_t *optionalClassMethods;
4185/// const struct _prop_list_t * properties;
4186/// const uint32_t size; // sizeof(struct _protocol_t)
4187/// const uint32_t flags; // = 0
4188/// }
4189/// @endcode
4190///
4191
4192llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4193 const ObjCProtocolDecl *PD) {
4194 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4195
4196 // Early exit if a defining object has already been generated.
4197 if (Entry && Entry->hasInitializer())
4198 return Entry;
4199
4200 const char *ProtocolName = PD->getNameAsCString();
4201
4202 // Construct method lists.
4203 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4204 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4205 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4206 e = PD->instmeth_end(); i != e; ++i) {
4207 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004208 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004209 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4210 OptInstanceMethods.push_back(C);
4211 } else {
4212 InstanceMethods.push_back(C);
4213 }
4214 }
4215
4216 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4217 e = PD->classmeth_end(); i != e; ++i) {
4218 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004219 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004220 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4221 OptClassMethods.push_back(C);
4222 } else {
4223 ClassMethods.push_back(C);
4224 }
4225 }
4226
4227 std::vector<llvm::Constant*> Values(10);
4228 // isa is NULL
4229 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4230 Values[1] = GetClassName(PD->getIdentifier());
4231 Values[2] = EmitProtocolList(
4232 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4233 PD->protocol_begin(),
4234 PD->protocol_end());
4235
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004236 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004237 + PD->getNameAsString(),
4238 "__DATA, __objc_const",
4239 InstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004240 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004241 + PD->getNameAsString(),
4242 "__DATA, __objc_const",
4243 ClassMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004244 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004245 + PD->getNameAsString(),
4246 "__DATA, __objc_const",
4247 OptInstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004248 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004249 + PD->getNameAsString(),
4250 "__DATA, __objc_const",
4251 OptClassMethods);
4252 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4253 0, PD, ObjCTypes);
4254 uint32_t Size =
4255 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4256 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4257 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4258 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4259 Values);
4260
4261 if (Entry) {
4262 // Already created, fix the linkage and update the initializer.
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004263 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004264 Entry->setInitializer(Init);
4265 } else {
4266 Entry =
4267 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004268 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004269 Init,
4270 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4271 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004272 Entry->setAlignment(
4273 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004274 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004275 }
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004276 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4277
4278 // Use this protocol meta-data to build protocol list table in section
4279 // __DATA, __objc_protolist
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004280 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004281 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004282 llvm::GlobalValue::WeakLinkage,
4283 Entry,
4284 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4285 +ProtocolName,
4286 &CGM.getModule());
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004287 PTGV->setAlignment(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004288 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00004289 PTGV->setSection("__DATA, __objc_protolist");
4290 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4291 UsedGlobals.push_back(PTGV);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004292 return Entry;
4293}
4294
4295/// EmitProtocolList - Generate protocol list meta-data:
4296/// @code
4297/// struct _protocol_list_t {
4298/// long protocol_count; // Note, this is 32/64 bit
4299/// struct _protocol_t[protocol_count];
4300/// }
4301/// @endcode
4302///
4303llvm::Constant *
4304CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4305 ObjCProtocolDecl::protocol_iterator begin,
4306 ObjCProtocolDecl::protocol_iterator end) {
4307 std::vector<llvm::Constant*> ProtocolRefs;
4308
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004309 // Just return null for empty protocol lists
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004310 if (begin == end)
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004311 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4312
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004313 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004314 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4315 if (GV)
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004316 return llvm::ConstantExpr::getBitCast(GV,
4317 ObjCTypes.ProtocolListnfABIPtrTy);
4318
4319 for (; begin != end; ++begin)
4320 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4321
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004322 // This list is null terminated.
4323 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004324 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004325
4326 std::vector<llvm::Constant*> Values(2);
4327 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4328 Values[1] =
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004329 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004330 ProtocolRefs.size()),
4331 ProtocolRefs);
4332
4333 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4334 GV = new llvm::GlobalVariable(Init->getType(), false,
4335 llvm::GlobalValue::InternalLinkage,
4336 Init,
4337 Name,
4338 &CGM.getModule());
4339 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian2d6ecb22009-01-31 02:43:27 +00004340 GV->setAlignment(
4341 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004342 UsedGlobals.push_back(GV);
Daniel Dunbar1f42bb02009-02-15 07:36:20 +00004343 return llvm::ConstantExpr::getBitCast(GV,
4344 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004345}
4346
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004347/// GetMethodDescriptionConstant - This routine build following meta-data:
4348/// struct _objc_method {
4349/// SEL _cmd;
4350/// char *method_type;
4351/// char *_imp;
4352/// }
4353
4354llvm::Constant *
4355CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4356 std::vector<llvm::Constant*> Desc(3);
4357 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4358 ObjCTypes.SelectorPtrTy);
4359 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004360 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004361 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4362 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4363}
Fariborz Jahanian55343922009-02-03 00:09:52 +00004364
4365/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4366/// This code gen. amounts to generating code for:
4367/// @code
4368/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4369/// @encode
4370///
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004371LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian55343922009-02-03 00:09:52 +00004372 CodeGen::CodeGenFunction &CGF,
4373 QualType ObjectTy,
4374 llvm::Value *BaseValue,
4375 const ObjCIvarDecl *Ivar,
4376 const FieldDecl *Field,
4377 unsigned CVRQualifiers) {
4378 assert(ObjectTy->isObjCInterfaceType() &&
4379 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahaniana09a5142009-02-12 18:51:23 +00004380 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004381 std::string ExternalName;
4382 llvm::GlobalVariable *IvarOffsetGV =
4383 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004384
Fariborz Jahanian55343922009-02-03 00:09:52 +00004385 // (char *) BaseValue
4386 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4387 ObjCTypes.Int8PtrTy);
4388 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4389 // (char*)BaseValue + Offset_symbol
4390 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4391 // (type *)((char*)BaseValue + Offset_symbol)
4392 const llvm::Type *IvarTy =
4393 CGM.getTypes().ConvertType(Ivar->getType());
4394 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4395 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004396
4397 if (Ivar->isBitField())
4398 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4399 CVRQualifiers);
4400
4401 LValue LV = LValue::MakeAddr(V,
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00004402 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4403 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00004404 LValue::SetObjCIvar(LV, true);
4405 return LV;
Fariborz Jahanian55343922009-02-03 00:09:52 +00004406}
4407
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004408llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4409 CodeGen::CodeGenFunction &CGF,
4410 ObjCInterfaceDecl *Interface,
4411 const ObjCIvarDecl *Ivar) {
Fariborz Jahaniancc00f922009-02-10 20:21:06 +00004412 std::string ExternalName;
4413 llvm::GlobalVariable *IvarOffsetGV =
4414 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4415
4416 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00004417}
4418
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004419CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4420 CodeGen::CodeGenFunction &CGF,
4421 QualType ResultType,
4422 Selector Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004423 llvm::Value *Receiver,
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004424 QualType Arg0Ty,
4425 bool IsSuper,
4426 const CallArgList &CallArgs) {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004427 // FIXME. Even though IsSuper is passes. This function doese not
4428 // handle calls to 'super' receivers.
4429 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004430 llvm::Value *Arg0 = Receiver;
4431 if (!IsSuper)
4432 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004433
4434 // Find the message function name.
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004435 // FIXME. This is too much work to get the ABI-specific result type
4436 // needed to find the message name.
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004437 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4438 llvm::SmallVector<QualType, 16>());
4439 llvm::Constant *Fn;
4440 std::string Name("\01l_");
4441 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004442#if 0
4443 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004444 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4445 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4446 // FIXME. Is there a better way of getting these names.
4447 // They are available in RuntimeFunctions vector pair.
4448 Name += "objc_msgSendId_stret_fixup";
4449 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004450 else
4451#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004452 if (IsSuper) {
4453 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4454 Name += "objc_msgSendSuper2_stret_fixup";
4455 }
4456 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004457 {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004458 Fn = ObjCTypes.MessageSendStretFixupFn;
4459 Name += "objc_msgSend_stret_fixup";
4460 }
4461 }
Fariborz Jahanianbea03192009-02-05 19:35:43 +00004462 else if (ResultType->isFloatingType() &&
4463 // Selection of frret API only happens in 32bit nonfragile ABI.
4464 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004465 Fn = ObjCTypes.MessageSendFpretFixupFn;
4466 Name += "objc_msgSend_fpret_fixup";
4467 }
4468 else {
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004469#if 0
4470// unlike what is documented. gcc never generates this API!!
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004471 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4472 Fn = ObjCTypes.MessageSendIdFixupFn;
4473 Name += "objc_msgSendId_fixup";
4474 }
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004475 else
4476#endif
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004477 if (IsSuper) {
4478 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4479 Name += "objc_msgSendSuper2_fixup";
4480 }
4481 else
Fariborz Jahanian13ab25e2009-02-05 18:00:27 +00004482 {
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004483 Fn = ObjCTypes.MessageSendFixupFn;
4484 Name += "objc_msgSend_fixup";
4485 }
4486 }
4487 Name += '_';
4488 std::string SelName(Sel.getAsString());
4489 // Replace all ':' in selector name with '_' ouch!
4490 for(unsigned i = 0; i < SelName.size(); i++)
4491 if (SelName[i] == ':')
4492 SelName[i] = '_';
4493 Name += SelName;
4494 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4495 if (!GV) {
4496 // Build messafe ref table entry.
4497 std::vector<llvm::Constant*> Values(2);
4498 Values[0] = Fn;
4499 Values[1] = GetMethodVarName(Sel);
4500 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4501 GV = new llvm::GlobalVariable(Init->getType(), false,
4502 llvm::GlobalValue::WeakLinkage,
4503 Init,
4504 Name,
4505 &CGM.getModule());
4506 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4507 GV->setAlignment(
4508 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4509 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4510 UsedGlobals.push_back(GV);
4511 }
4512 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004513
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004514 CallArgList ActualArgs;
4515 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4516 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4517 ObjCTypes.MessageRefCPtrTy));
4518 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004519 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4520 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4521 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianf3c17752009-02-14 21:25:36 +00004522 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian10d69ea2009-02-05 01:13:09 +00004523 Callee = CGF.Builder.CreateBitCast(Callee,
4524 llvm::PointerType::getUnqual(FTy));
4525 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004526}
4527
4528/// Generate code for a message send expression in the nonfragile abi.
4529CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4530 CodeGen::CodeGenFunction &CGF,
4531 QualType ResultType,
4532 Selector Sel,
4533 llvm::Value *Receiver,
4534 bool IsClassMessage,
4535 const CallArgList &CallArgs) {
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004536 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanianf52110f2009-02-04 20:42:28 +00004537 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian7e881162009-02-04 00:22:57 +00004538 false, CallArgs);
4539}
4540
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004541llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004542 const ObjCInterfaceDecl *ID,
4543 bool IsSuper) {
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004544
4545 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4546
4547 if (!Entry) {
4548 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4549 llvm::GlobalVariable *ClassGV =
4550 CGM.getModule().getGlobalVariable(ClassName);
4551 if (!ClassGV) {
4552 ClassGV =
4553 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4554 llvm::GlobalValue::ExternalLinkage,
4555 0,
4556 ClassName,
4557 &CGM.getModule());
4558 UsedGlobals.push_back(ClassGV);
4559 }
4560 Entry =
4561 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4562 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004563 ClassGV,
4564 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4565 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004566 &CGM.getModule());
4567 Entry->setAlignment(
4568 CGM.getTargetData().getPrefTypeAlignment(
4569 ObjCTypes.ClassnfABIPtrTy));
4570
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004571 if (IsSuper)
Fariborz Jahanianba825d72009-02-26 22:30:39 +00004572 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004573 else
Fariborz Jahanianba825d72009-02-26 22:30:39 +00004574 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004575 UsedGlobals.push_back(Entry);
4576 }
4577
4578 return Builder.CreateLoad(Entry, false, "tmp");
4579}
4580
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004581/// EmitMetaClassRef - Return a Value * of the address of _class_t
4582/// meta-data
4583///
4584llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4585 const ObjCInterfaceDecl *ID) {
4586 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4587 if (Entry)
4588 return Builder.CreateLoad(Entry, false, "tmp");
4589
4590 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4591 llvm::GlobalVariable *MetaClassGV =
4592 CGM.getModule().getGlobalVariable(MetaClassName);
4593 if (!MetaClassGV) {
4594 MetaClassGV =
4595 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4596 llvm::GlobalValue::ExternalLinkage,
4597 0,
4598 MetaClassName,
4599 &CGM.getModule());
4600 UsedGlobals.push_back(MetaClassGV);
4601 }
4602
4603 Entry =
4604 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4605 llvm::GlobalValue::InternalLinkage,
4606 MetaClassGV,
4607 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4608 &CGM.getModule());
4609 Entry->setAlignment(
4610 CGM.getTargetData().getPrefTypeAlignment(
4611 ObjCTypes.ClassnfABIPtrTy));
4612
4613 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4614 UsedGlobals.push_back(Entry);
4615
4616 return Builder.CreateLoad(Entry, false, "tmp");
4617}
4618
Fariborz Jahanian917c0402009-02-05 20:41:40 +00004619/// GetClass - Return a reference to the class for the given interface
4620/// decl.
4621llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4622 const ObjCInterfaceDecl *ID) {
4623 return EmitClassRef(Builder, ID);
4624}
4625
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004626/// Generates a message send where the super is the receiver. This is
4627/// a message send to self with special delivery semantics indicating
4628/// which class's method should be called.
4629CodeGen::RValue
4630CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4631 QualType ResultType,
4632 Selector Sel,
4633 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00004634 bool isCategoryImpl,
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004635 llvm::Value *Receiver,
4636 bool IsClassMessage,
4637 const CodeGen::CallArgList &CallArgs) {
4638 // ...
4639 // Create and init a super structure; this is a (receiver, class)
4640 // pair we will pass to objc_msgSendSuper.
4641 llvm::Value *ObjCSuper =
4642 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4643
4644 llvm::Value *ReceiverAsObject =
4645 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4646 CGF.Builder.CreateStore(ReceiverAsObject,
4647 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4648
4649 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian17636fa2009-02-28 20:07:56 +00004650 llvm::Value *Target;
4651 if (IsClassMessage) {
4652 if (isCategoryImpl) {
4653 // Message sent to "super' in a class method defined in
4654 // a category implementation.
4655 Target = EmitClassRef(CGF.Builder, Class, false);
4656 Target = CGF.Builder.CreateStructGEP(Target, 0);
4657 Target = CGF.Builder.CreateLoad(Target);
4658 }
4659 else
4660 Target = EmitMetaClassRef(CGF.Builder, Class);
4661 }
4662 else
4663 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanianf3a44012009-02-06 20:09:23 +00004664
4665 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4666 // and ObjCTypes types.
4667 const llvm::Type *ClassTy =
4668 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4669 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4670 CGF.Builder.CreateStore(Target,
4671 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4672
4673 return EmitMessageSend(CGF, ResultType, Sel,
4674 ObjCSuper, ObjCTypes.SuperPtrCTy,
4675 true, CallArgs);
4676}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00004677
4678llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4679 Selector Sel) {
4680 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4681
4682 if (!Entry) {
4683 llvm::Constant *Casted =
4684 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4685 ObjCTypes.SelectorPtrTy);
4686 Entry =
4687 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4688 llvm::GlobalValue::InternalLinkage,
4689 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4690 &CGM.getModule());
4691 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4692 UsedGlobals.push_back(Entry);
4693 }
4694
4695 return Builder.CreateLoad(Entry, false, "tmp");
4696}
Fariborz Jahanian5eefb432009-02-16 22:52:32 +00004697/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4698/// objc_assign_ivar (id src, id *dst)
4699///
4700void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4701 llvm::Value *src, llvm::Value *dst)
4702{
4703 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4704 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4705 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4706 src, dst, "assignivar");
4707 return;
4708}
4709
4710/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4711/// objc_assign_strongCast (id src, id *dst)
4712///
4713void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4714 CodeGen::CodeGenFunction &CGF,
4715 llvm::Value *src, llvm::Value *dst)
4716{
4717 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4718 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4719 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4720 src, dst, "weakassign");
4721 return;
4722}
4723
4724/// EmitObjCWeakRead - Code gen for loading value of a __weak
4725/// object: objc_read_weak (id *src)
4726///
4727llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4728 CodeGen::CodeGenFunction &CGF,
4729 llvm::Value *AddrWeakObj)
4730{
4731 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4732 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4733 AddrWeakObj, "weakread");
4734 return read_weak;
4735}
4736
4737/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4738/// objc_assign_weak (id src, id *dst)
4739///
4740void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4741 llvm::Value *src, llvm::Value *dst)
4742{
4743 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4744 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4745 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4746 src, dst, "weakassign");
4747 return;
4748}
4749
4750/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4751/// objc_assign_global (id src, id *dst)
4752///
4753void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4754 llvm::Value *src, llvm::Value *dst)
4755{
4756 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4757 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4758 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4759 src, dst, "globalassign");
4760 return;
4761}
Fariborz Jahanianebb82c62009-02-11 20:51:17 +00004762
Daniel Dunbar75de89f2009-02-24 07:47:38 +00004763void
4764CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
4765 const Stmt &S) {
4766 // We don't handle anything interesting yet.
4767 if (const ObjCAtTryStmt *TS = dyn_cast<ObjCAtTryStmt>(&S))
4768 if (TS->getCatchStmts())
4769 return CGF.ErrorUnsupported(&S, "try (with catch) statement");
4770
4771 bool isTry = isa<ObjCAtTryStmt>(S);
4772 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
4773 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
4774 llvm::BasicBlock *LandingPad = CGF.createBasicBlock("try.pad");
4775 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
4776 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
4777
4778 // For @synchronized, call objc_sync_enter(sync.expr). The
4779 // evaluation of the expression must occur before we enter the
4780 // @synchronized. We can safely avoid a temp here because jumps into
4781 // @synchronized are illegal & this will dominate uses.
4782 llvm::Value *SyncArg = 0;
4783 if (!isTry) {
4784 SyncArg =
4785 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
4786 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
4787 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
4788 }
4789
4790 // Push an EH context entry, used for handling rethrows and jumps
4791 // through finally.
4792 CGF.PushCleanupBlock(FinallyBlock);
4793
4794 CGF.setInvokeDest(LandingPad);
4795
4796 CGF.EmitBlock(TryBlock);
4797 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
4798 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
4799 CGF.EmitBranchThroughCleanup(FinallyEnd);
4800
4801 // Pop the cleanup entry, the @finally is outside this cleanup
4802 // scope.
4803 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
4804 CGF.setInvokeDest(PrevLandingPad);
4805
4806 CGF.EmitBlock(FinallyBlock);
4807
4808 if (isTry) {
4809 if (const ObjCAtFinallyStmt* FinallyStmt =
4810 cast<ObjCAtTryStmt>(S).getFinallyStmt())
4811 CGF.EmitStmt(FinallyStmt->getFinallyBody());
4812 } else {
4813 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
4814 // @synchronized.
4815 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
4816 }
4817
4818 if (Info.SwitchBlock)
4819 CGF.EmitBlock(Info.SwitchBlock);
4820 if (Info.EndBlock)
4821 CGF.EmitBlock(Info.EndBlock);
4822
4823 // Branch around the landing pad if necessary.
4824 CGF.EmitBranch(FinallyEnd);
4825
4826 // Emit the landing pad.
4827
4828 // Clear insertion point to avoid chaining.
4829 CGF.Builder.ClearInsertionPoint();
4830 CGF.EmitBlock(LandingPad);
4831
4832 llvm::Value *llvm_eh_exception =
4833 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
4834 llvm::Value *llvm_eh_selector_i64 =
4835 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
4836 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
4837
4838 llvm::SmallVector<llvm::Value*, 8> Args;
4839 Args.push_back(Exc);
4840 Args.push_back(ObjCTypes.EHPersonalityPtr);
4841 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
4842
4843 llvm::Value *Selector =
4844 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
4845
4846 // The only valid result for the limited case we are considering is
4847 // the cleanup.
4848 (void) Selector;
4849
4850 // Re-emit cleanup code for exceptional case.
4851 if (isTry) {
4852 // FIXME: This is horrible, in many ways: (a) it is broken because
4853 // we are messing with some global data structures (like where
4854 // labels point at), (b) it is exponential in the size of code
4855 // generated, (c) seriously, its just gross.
4856 if (const ObjCAtFinallyStmt* FinallyStmt =
4857 cast<ObjCAtTryStmt>(S).getFinallyStmt())
4858 CGF.EmitStmt(FinallyStmt->getFinallyBody());
4859 } else {
4860 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
4861 // @synchronized.
4862 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
4863 }
4864
4865 CGF.EnsureInsertPoint();
4866 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, Exc);
4867 CGF.Builder.CreateUnreachable();
4868
4869 CGF.EmitBlock(FinallyEnd);
4870}
4871
Anders Carlsson1cf75362009-02-16 22:59:18 +00004872/// EmitThrowStmt - Generate code for a throw statement.
4873void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
4874 const ObjCAtThrowStmt &S) {
4875 llvm::Value *ExceptionAsObject;
4876
4877 if (const Expr *ThrowExpr = S.getThrowExpr()) {
4878 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
4879 ExceptionAsObject =
4880 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
4881
4882 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
4883 CGF.Builder.CreateUnreachable();
4884 } else {
4885 CGF.ErrorUnsupported(&S, "rethrow statement");
4886 }
4887
4888 // Clear the insertion point to indicate we are in unreachable code.
4889 CGF.Builder.ClearInsertionPoint();
4890}
4891
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00004892/* *** */
4893
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00004894CodeGen::CGObjCRuntime *
4895CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00004896 return new CGObjCMac(CGM);
4897}
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004898
4899CodeGen::CGObjCRuntime *
Fariborz Jahaniand0374812009-01-22 23:02:58 +00004900CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004901 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004902}