blob: ffc40b943b2e629ba647ca3e7dc211060d8eadc7 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000023#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000024#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000025#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000026#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000027#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000028
29using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000030using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000031
32namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000033
Daniel Dunbarae226fa2008-08-27 02:31:56 +000034 typedef std::vector<llvm::Constant*> ConstantVector;
35
Daniel Dunbar6efc0c52008-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 Jahanianee0af742009-01-21 22:04:16 +000039class ObjCCommonTypesHelper {
40protected:
41 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000043public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000044 const llvm::Type *ShortTy, *IntTy, *LongTy;
45 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000046
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000047 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
48 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000049
50 /// PtrObjectPtrTy - LLVM type for id *
51 const llvm::Type *PtrObjectPtrTy;
52
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000053 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000054 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000055 /// ProtocolPtrTy - LLVM type for external protocol handles
56 /// (typeof(Protocol))
57 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000058
Daniel Dunbar19cd87e2008-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 Jahanianee0af742009-01-21 22:04:16 +000063
Daniel Dunbare8b470d2008-08-23 04:28:29 +000064 /// SuperTy - LLVM type for struct objc_super.
65 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000066 /// SuperPtrTy - LLVM type for struct objc_super *.
67 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000068
Fariborz Jahanian30bc5712009-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 Jahaniand55b6fc2009-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 Jahaniandb286862009-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 Carlssonf57c5b22009-02-16 22:59:18 +0000105
106 /// ExceptionThrowFn - LLVM objc_exception_throw function.
107 llvm::Function *ExceptionThrowFn;
108
Daniel Dunbar1c566672009-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 Jahanianee0af742009-01-21 22:04:16 +0000115 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
116 ~ObjCCommonTypesHelper(){}
117};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000118
Fariborz Jahanianee0af742009-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 Dunbar4e2d7d02008-08-12 06:48:42 +0000129 /// SymtabTy - LLVM type for struct objc_symtab.
130 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000131 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
132 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000133 /// ModuleTy - LLVM type for struct objc_module.
134 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000135
Daniel Dunbar6efc0c52008-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 Dunbar6efc0c52008-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 Dunbar86e253a2008-08-22 20:34:54 +0000159 /// CategoryTy - LLVM type for struct objc_category.
160 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-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 Dunbar27f9d772008-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 Dunbar27f9d772008-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 Carlsson124526b2008-09-09 10:10:21 +0000179
180 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
181 const llvm::Type *ExceptionDataTy;
182
Anders Carlsson124526b2008-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 Lattner10cac6f2008-11-15 21:26:17 +0000197
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000198public:
199 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000200 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-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 Dunbarbbce49b2008-08-12 00:12:39 +0000214};
215
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000216/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000217/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000218class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000219public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000220 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
221 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
222 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
223 *MessageSendSuper2StretFixupFn;
224
Fariborz Jahaniand55b6fc2009-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 Dunbar948e2582009-02-15 07:36:20 +0000234 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
235 const llvm::Type *ProtocolnfABIPtrTy;
236
Fariborz Jahaniand55b6fc2009-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 Jahanianaa23b572009-01-23 23:53:38 +0000246 // ClassnfABIPtrTy - LLVM for struct _class_t*
247 const llvm::Type *ClassnfABIPtrTy;
248
Fariborz Jahaniand55b6fc2009-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 Jahanian2e4672b2009-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 Jahanian83a8a752009-02-04 20:42:28 +0000275 // MessageRefCTy - clang type for struct _message_ref_t
276 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000277
278 // MessageRefPtrTy - LLVM for struct _message_ref_t*
279 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000280 // MessageRefCPtrTy - clang type for struct _message_ref_t*
281 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000282
Fariborz Jahanianef163782009-02-05 01:13:09 +0000283 // MessengerTy - Type of the messenger (shown as IMP above)
284 const llvm::FunctionType *MessengerTy;
285
Fariborz Jahanian2e4672b2009-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 Dunbar8ecbaf22009-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
Daniel Dunbare588b992009-03-01 04:46:24 +0000300 llvm::Function *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn;
301
302 const llvm::StructType *EHTypeTy;
303 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000304
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000305 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
306 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000307};
308
309class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
310protected:
311 CodeGen::CodeGenModule &CGM;
312 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000313 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000314
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000315 /// LazySymbols - Symbols to generate a lazy reference for. See
316 /// DefinedSymbols and FinishModule().
317 std::set<IdentifierInfo*> LazySymbols;
318
319 /// DefinedSymbols - External symbols which are defined by this
320 /// module. The symbols in this list and LazySymbols are used to add
321 /// special linker symbols which ensure that Objective-C modules are
322 /// linked properly.
323 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000324
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000325 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000326 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000327
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000328 /// MethodVarNames - uniqued method variable names.
329 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000330
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000331 /// MethodVarTypes - uniqued method type signatures. We have to use
332 /// a StringMap here because have no other unique reference.
333 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000334
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000335 /// MethodDefinitions - map of methods which have been defined in
336 /// this translation unit.
337 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000338
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000339 /// PropertyNames - uniqued method variable names.
340 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000341
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000342 /// ClassReferences - uniqued class references.
343 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000344
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000345 /// SelectorReferences - uniqued selector references.
346 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000347
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000348 /// Protocols - Protocols for which an objc_protocol structure has
349 /// been emitted. Forward declarations are handled by creating an
350 /// empty structure whose initializer is filled in when/if defined.
351 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000352
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000353 /// DefinedProtocols - Protocols which have actually been
354 /// defined. We should not need this, see FIXME in GenerateProtocol.
355 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000356
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000357 /// DefinedClasses - List of defined classes.
358 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000359
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000360 /// DefinedCategories - List of defined categories.
361 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000362
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000363 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000364 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000365 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000366
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000367 /// GetNameForMethod - Return a name for the given method.
368 /// \param[out] NameOut - The return value.
369 void GetNameForMethod(const ObjCMethodDecl *OMD,
370 const ObjCContainerDecl *CD,
371 std::string &NameOut);
372
373 /// GetMethodVarName - Return a unique constant for the given
374 /// selector's name. The return value has type char *.
375 llvm::Constant *GetMethodVarName(Selector Sel);
376 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
377 llvm::Constant *GetMethodVarName(const std::string &Name);
378
379 /// GetMethodVarType - Return a unique constant for the given
380 /// selector's name. The return value has type char *.
381
382 // FIXME: This is a horrible name.
383 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Devang Patel7794bb82009-03-04 18:21:39 +0000384 llvm::Constant *GetMethodVarType(FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000385
386 /// GetPropertyName - Return a unique constant for the given
387 /// name. The return value has type char *.
388 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
389
390 // FIXME: This can be dropped once string functions are unified.
391 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
392 const Decl *Container);
393
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000394 /// GetClassName - Return a unique constant for the given selector's
395 /// name. The return value has type char *.
396 llvm::Constant *GetClassName(IdentifierInfo *Ident);
397
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000398 /// BuildIvarLayout - Builds ivar layout bitmap for the class
399 /// implementation for the __strong or __weak case.
400 ///
401 llvm::Constant *BuildIvarLayout(ObjCImplementationDecl *OI,
402 bool ForStrongLayout);
403
404 void BuildAggrIvarLayout(RecordDecl *RD,
405 const std::vector<FieldDecl*>& RecFields,
406 unsigned int BytePos, bool ForStrongLayout,
407 int &Index, int &SkIndex, bool &HasUnion);
408
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000409 /// GetIvarLayoutName - Returns a unique constant for the given
410 /// ivar layout bitmap.
411 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
412 const ObjCCommonTypesHelper &ObjCTypes);
413
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000414 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
415 RecordDecl::field_iterator &FIV,
416 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000417 /// EmitPropertyList - Emit the given property list. The return
418 /// value has type PropertyListPtrTy.
419 llvm::Constant *EmitPropertyList(const std::string &Name,
420 const Decl *Container,
421 const ObjCContainerDecl *OCD,
422 const ObjCCommonTypesHelper &ObjCTypes);
423
Fariborz Jahanianda320092009-01-29 19:24:30 +0000424 /// GetProtocolRef - Return a reference to the internal protocol
425 /// description, creating an empty one if it has not been
426 /// defined. The return value has type ProtocolPtrTy.
427 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000428
429 /// GetIvarBaseOffset - returns ivars byte offset.
430 uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout,
431 FieldDecl *Field);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000432
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000433 /// CreateMetadataVar - Create a global variable with internal
434 /// linkage for use by the Objective-C runtime.
435 ///
436 /// This is a convenience wrapper which not only creates the
437 /// variable, but also sets the section and alignment and adds the
438 /// global to the UsedGlobals list.
439 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
440 llvm::Constant *Init,
441 const char *Section,
442 bool SetAlignment,
443 bool IsUsed);
444
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000445public:
446 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
447 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000448
449 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000450
451 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
452 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000453
454 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
455
456 /// GetOrEmitProtocol - Get the protocol object for the given
457 /// declaration, emitting it if necessary. The return value has type
458 /// ProtocolPtrTy.
459 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
460
461 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
462 /// object for the given declaration, emitting it if needed. These
463 /// forward references will be filled in with empty bodies if no
464 /// definition is seen. The return value has type ProtocolPtrTy.
465 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000466};
467
468class CGObjCMac : public CGObjCCommonMac {
469private:
470 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000471 /// EmitImageInfo - Emit the image info marker used to encode some module
472 /// level information.
473 void EmitImageInfo();
474
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000475 /// EmitModuleInfo - Another marker encoding module level
476 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000477 void EmitModuleInfo();
478
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000479 /// EmitModuleSymols - Emit module symbols, the list of defined
480 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000481 llvm::Constant *EmitModuleSymbols();
482
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000483 /// FinishModule - Write out global data structures at the end of
484 /// processing a translation unit.
485 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000486
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000487 /// EmitClassExtension - Generate the class extension structure used
488 /// to store the weak ivar layout and properties. The return value
489 /// has type ClassExtensionPtrTy.
490 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
491
492 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
493 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000494 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 const ObjCInterfaceDecl *ID);
496
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000497 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000498 QualType ResultType,
499 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000500 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000501 QualType Arg0Ty,
502 bool IsSuper,
503 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000504
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000505 /// EmitIvarList - Emit the ivar list for the given
506 /// implementation. If ForClass is true the list of class ivars
507 /// (i.e. metaclass ivars) is emitted, otherwise the list of
508 /// interface ivars will be emitted. The return value has type
509 /// IvarListPtrTy.
510 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000511 bool ForClass);
512
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000513 /// EmitMetaClass - Emit a forward reference to the class structure
514 /// for the metaclass of the given interface. The return value has
515 /// type ClassPtrTy.
516 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
517
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000518 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000519 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000520 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
521 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000522 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000523 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000524
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000525 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000526
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000527 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528
529 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000530 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000531 llvm::Constant *EmitMethodList(const std::string &Name,
532 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000533 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534
535 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000536 /// method declarations.
537 /// - TypeName: The name for the type containing the methods.
538 /// - IsProtocol: True iff these methods are for a protocol.
539 /// - ClassMethds: True iff these are class methods.
540 /// - Required: When true, only "required" methods are
541 /// listed. Similarly, when false only "optional" methods are
542 /// listed. For classes this should always be true.
543 /// - begin, end: The method list to output.
544 ///
545 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000546 llvm::Constant *EmitMethodDescList(const std::string &Name,
547 const char *Section,
548 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000549
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000550 /// GetOrEmitProtocol - Get the protocol object for the given
551 /// declaration, emitting it if necessary. The return value has type
552 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000553 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000554
555 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
556 /// object for the given declaration, emitting it if needed. These
557 /// forward references will be filled in with empty bodies if no
558 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000559 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000560
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000561 /// EmitProtocolExtension - Generate the protocol extension
562 /// structure used to store optional instance and class methods, and
563 /// protocol properties. The return value has type
564 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000565 llvm::Constant *
566 EmitProtocolExtension(const ObjCProtocolDecl *PD,
567 const ConstantVector &OptInstanceMethods,
568 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000569
570 /// EmitProtocolList - Generate the list of referenced
571 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000572 llvm::Constant *EmitProtocolList(const std::string &Name,
573 ObjCProtocolDecl::protocol_iterator begin,
574 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000575
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000576 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
577 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000578 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000579
Fariborz Jahanianda320092009-01-29 19:24:30 +0000580 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000581 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000582
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000583 virtual llvm::Function *ModuleInitFunction();
584
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000585 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000586 QualType ResultType,
587 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000588 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000589 bool IsClassMessage,
590 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000591
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000592 virtual CodeGen::RValue
593 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000594 QualType ResultType,
595 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000596 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000597 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000598 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000599 bool IsClassMessage,
600 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000601
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000602 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000603 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000604
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000605 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000606
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000607 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000608
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000609 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000610
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000611 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000612 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000613
Daniel Dunbar49f66022008-09-24 03:38:44 +0000614 virtual llvm::Function *GetPropertyGetFunction();
615 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000616 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000617
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000618 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
619 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000620 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
621 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000622 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000623 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000624 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
625 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000626 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
627 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000628 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
629 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000630 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
631 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000632
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000633 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
634 QualType ObjectTy,
635 llvm::Value *BaseValue,
636 const ObjCIvarDecl *Ivar,
637 const FieldDecl *Field,
638 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000639 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
640 ObjCInterfaceDecl *Interface,
641 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000642};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000643
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000644class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000645private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000646 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000647 llvm::GlobalVariable* ObjCEmptyCacheVar;
648 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000649
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000650 /// MetaClassReferences - uniqued meta class references.
651 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +0000652
653 /// EHTypeReferences - uniqued class ehtype references.
654 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000655
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000656 /// FinishNonFragileABIModule - Write out global data structures at the end of
657 /// processing a translation unit.
658 void FinishNonFragileABIModule();
659
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000660 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
661 unsigned InstanceStart,
662 unsigned InstanceSize,
663 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000664 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
665 llvm::Constant *IsAGV,
666 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000667 llvm::Constant *ClassRoGV,
668 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000669
670 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
671
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000672 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
673
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000674 /// EmitMethodList - Emit the method list for the given
675 /// implementation. The return value has type MethodListnfABITy.
676 llvm::Constant *EmitMethodList(const std::string &Name,
677 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000678 const ConstantVector &Methods);
679 /// EmitIvarList - Emit the ivar list for the given
680 /// implementation. If ForClass is true the list of class ivars
681 /// (i.e. metaclass ivars) is emitted, otherwise the list of
682 /// interface ivars will be emitted. The return value has type
683 /// IvarListnfABIPtrTy.
684 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000685
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000686 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000687 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000688 unsigned long int offset);
689
Fariborz Jahanianda320092009-01-29 19:24:30 +0000690 /// GetOrEmitProtocol - Get the protocol object for the given
691 /// declaration, emitting it if necessary. The return value has type
692 /// ProtocolPtrTy.
693 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
694
695 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
696 /// object for the given declaration, emitting it if needed. These
697 /// forward references will be filled in with empty bodies if no
698 /// definition is seen. The return value has type ProtocolPtrTy.
699 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
700
701 /// EmitProtocolList - Generate the list of referenced
702 /// protocols. The return value has type ProtocolListPtrTy.
703 llvm::Constant *EmitProtocolList(const std::string &Name,
704 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000705 ObjCProtocolDecl::protocol_iterator end);
706
707 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
708 QualType ResultType,
709 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000710 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000711 QualType Arg0Ty,
712 bool IsSuper,
713 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +0000714
715 /// GetClassGlobal - Return the global variable for the Objective-C
716 /// class of the given name.
717 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000718
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000719 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
720 /// for the given class.
721 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000722 const ObjCInterfaceDecl *ID,
723 bool IsSuper = false);
724
725 /// EmitMetaClassRef - Return a Value * of the address of _class_t
726 /// meta-data
727 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
728 const ObjCInterfaceDecl *ID);
729
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000730 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
731 /// the given ivar.
732 ///
733 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000734 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000735 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000736
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000737 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
738 /// for the given selector.
739 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +0000740
741 /// GetInterfaceEHType - Get the ehtype for the given Objective-C
742 /// interface. The return value has type EHTypePtrTy.
743 llvm::Value *GetInterfaceEHType(const ObjCInterfaceType *IT);
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000744
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000745public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000746 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000747 // FIXME. All stubs for now!
748 virtual llvm::Function *ModuleInitFunction();
749
750 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
751 QualType ResultType,
752 Selector Sel,
753 llvm::Value *Receiver,
754 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000755 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000756
757 virtual CodeGen::RValue
758 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
759 QualType ResultType,
760 Selector Sel,
761 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000762 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000763 llvm::Value *Receiver,
764 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000765 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000766
767 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000768 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000769
770 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000771 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000772
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000773 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000774
775 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000776 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000777 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000778
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000779 virtual llvm::Function *GetPropertyGetFunction(){
780 return ObjCTypes.GetPropertyFn;
781 }
782 virtual llvm::Function *GetPropertySetFunction(){
783 return ObjCTypes.SetPropertyFn;
784 }
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000785 virtual llvm::Function *EnumerationMutationFunction() {
786 return ObjCTypes.EnumerationMutationFn;
787 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000788
789 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000790 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000791 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000792 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000793 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000794 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000795 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000796 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000797 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000798 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000799 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000800 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000801 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000802 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000803 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
804 QualType ObjectTy,
805 llvm::Value *BaseValue,
806 const ObjCIvarDecl *Ivar,
807 const FieldDecl *Field,
808 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000809 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
810 ObjCInterfaceDecl *Interface,
811 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000812};
813
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000814} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000815
816/* *** Helper Functions *** */
817
818/// getConstantGEP() - Help routine to construct simple GEPs.
819static llvm::Constant *getConstantGEP(llvm::Constant *C,
820 unsigned idx0,
821 unsigned idx1) {
822 llvm::Value *Idxs[] = {
823 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
824 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
825 };
826 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
827}
828
829/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000830
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000831CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
832 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000833{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000834 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000835 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000836}
837
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000838/// GetClass - Return a reference to the class for the given interface
839/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000840llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000841 const ObjCInterfaceDecl *ID) {
842 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000843}
844
845/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000846llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000847 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000848}
849
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000850/// Generate a constant CFString object.
851/*
852 struct __builtin_CFString {
853 const int *isa; // point to __CFConstantStringClassReference
854 int flags;
855 const char *str;
856 long length;
857 };
858*/
859
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000860llvm::Constant *CGObjCCommonMac::GenerateConstantString(
861 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000862 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000863}
864
865/// Generates a message send where the super is the receiver. This is
866/// a message send to self with special delivery semantics indicating
867/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000868CodeGen::RValue
869CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000870 QualType ResultType,
871 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000872 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000873 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000874 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000875 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000876 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000877 // Create and init a super structure; this is a (receiver, class)
878 // pair we will pass to objc_msgSendSuper.
879 llvm::Value *ObjCSuper =
880 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
881 llvm::Value *ReceiverAsObject =
882 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
883 CGF.Builder.CreateStore(ReceiverAsObject,
884 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000885
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000886 // If this is a class message the metaclass is passed as the target.
887 llvm::Value *Target;
888 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000889 if (isCategoryImpl) {
890 // Message sent to 'super' in a class method defined in a category
891 // implementation requires an odd treatment.
892 // If we are in a class method, we must retrieve the
893 // _metaclass_ for the current class, pointed at by
894 // the class's "isa" pointer. The following assumes that
895 // isa" is the first ivar in a class (which it must be).
896 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
897 Target = CGF.Builder.CreateStructGEP(Target, 0);
898 Target = CGF.Builder.CreateLoad(Target);
899 }
900 else {
901 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
902 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
903 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
904 Target = Super;
905 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000906 } else {
907 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
908 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000909 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
910 // and ObjCTypes types.
911 const llvm::Type *ClassTy =
912 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000913 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000914 CGF.Builder.CreateStore(Target,
915 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
916
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000917 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000918 ObjCSuper, ObjCTypes.SuperPtrCTy,
919 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000920}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000921
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000922/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000923CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000924 QualType ResultType,
925 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000926 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000927 bool IsClassMessage,
928 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000929 llvm::Value *Arg0 =
930 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000931 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000932 Arg0, CGF.getContext().getObjCIdType(),
933 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000934}
935
936CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000937 QualType ResultType,
938 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000939 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000940 QualType Arg0Ty,
941 bool IsSuper,
942 const CallArgList &CallArgs) {
943 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000944 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
945 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
946 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000947 CGF.getContext().getObjCSelType()));
948 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000949
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000950 CodeGenTypes &Types = CGM.getTypes();
951 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
952 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000953
954 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000955 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000956 Fn = ObjCTypes.getSendStretFn(IsSuper);
957 } else if (ResultType->isFloatingType()) {
958 // FIXME: Sadly, this is wrong. This actually depends on the
959 // architecture. This happens to be right for x86-32 though.
960 Fn = ObjCTypes.getSendFpretFn(IsSuper);
961 } else {
962 Fn = ObjCTypes.getSendFn(IsSuper);
963 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000964 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000965 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000966}
967
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000968llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000969 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +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
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000975 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
976 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000977}
978
Fariborz Jahanianda320092009-01-29 19:24:30 +0000979void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000980 // FIXME: We shouldn't need this, the protocol decl should contain
981 // enough information to tell us whether this was a declaration or a
982 // definition.
983 DefinedProtocols.insert(PD->getIdentifier());
984
985 // If we have generated a forward reference to this protocol, emit
986 // it now. Otherwise do nothing, the protocol objects are lazily
987 // emitted.
988 if (Protocols.count(PD->getIdentifier()))
989 GetOrEmitProtocol(PD);
990}
991
Fariborz Jahanianda320092009-01-29 19:24:30 +0000992llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000993 if (DefinedProtocols.count(PD->getIdentifier()))
994 return GetOrEmitProtocol(PD);
995 return GetOrEmitProtocolRef(PD);
996}
997
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998/*
999 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1000 struct _objc_protocol {
1001 struct _objc_protocol_extension *isa;
1002 char *protocol_name;
1003 struct _objc_protocol_list *protocol_list;
1004 struct _objc__method_prototype_list *instance_methods;
1005 struct _objc__method_prototype_list *class_methods
1006 };
1007
1008 See EmitProtocolExtension().
1009*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001010llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1011 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1012
1013 // Early exit if a defining object has already been generated.
1014 if (Entry && Entry->hasInitializer())
1015 return Entry;
1016
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001017 // FIXME: I don't understand why gcc generates this, or where it is
1018 // resolved. Investigate. Its also wasteful to look this up over and
1019 // over.
1020 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1021
Chris Lattner8ec03f52008-11-24 03:54:41 +00001022 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001023
1024 // Construct method lists.
1025 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1026 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
1027 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
1028 e = PD->instmeth_end(); i != e; ++i) {
1029 ObjCMethodDecl *MD = *i;
1030 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1031 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1032 OptInstanceMethods.push_back(C);
1033 } else {
1034 InstanceMethods.push_back(C);
1035 }
1036 }
1037
1038 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
1039 e = PD->classmeth_end(); i != e; ++i) {
1040 ObjCMethodDecl *MD = *i;
1041 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1042 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1043 OptClassMethods.push_back(C);
1044 } else {
1045 ClassMethods.push_back(C);
1046 }
1047 }
1048
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001049 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001050 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001051 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001052 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001053 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001054 PD->protocol_begin(),
1055 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001056 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001057 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1058 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001059 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1060 InstanceMethods);
1061 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001062 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1063 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1065 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001066 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1067 Values);
1068
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001069 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001070 // Already created, fix the linkage and update the initializer.
1071 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072 Entry->setInitializer(Init);
1073 } else {
1074 Entry =
1075 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1076 llvm::GlobalValue::InternalLinkage,
1077 Init,
1078 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1079 &CGM.getModule());
1080 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1081 UsedGlobals.push_back(Entry);
1082 // FIXME: Is this necessary? Why only for protocol?
1083 Entry->setAlignment(4);
1084 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001085
1086 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001087}
1088
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001089llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001090 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1091
1092 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001093 // We use the initializer as a marker of whether this is a forward
1094 // reference or not. At module finalization we add the empty
1095 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001096 Entry =
1097 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001098 llvm::GlobalValue::ExternalLinkage,
1099 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001100 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001101 &CGM.getModule());
1102 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1103 UsedGlobals.push_back(Entry);
1104 // FIXME: Is this necessary? Why only for protocol?
1105 Entry->setAlignment(4);
1106 }
1107
1108 return Entry;
1109}
1110
1111/*
1112 struct _objc_protocol_extension {
1113 uint32_t size;
1114 struct objc_method_description_list *optional_instance_methods;
1115 struct objc_method_description_list *optional_class_methods;
1116 struct objc_property_list *instance_properties;
1117 };
1118*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001119llvm::Constant *
1120CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1121 const ConstantVector &OptInstanceMethods,
1122 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001123 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001124 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001125 std::vector<llvm::Constant*> Values(4);
1126 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001127 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001128 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1129 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001130 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1131 OptInstanceMethods);
1132 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001133 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1134 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001135 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1136 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001137 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1138 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001139 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001140
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001141 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001142 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1143 Values[3]->isNullValue())
1144 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1145
1146 llvm::Constant *Init =
1147 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1148 llvm::GlobalVariable *GV =
1149 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1150 llvm::GlobalValue::InternalLinkage,
1151 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001152 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001153 &CGM.getModule());
1154 // No special section, but goes in llvm.used
1155 UsedGlobals.push_back(GV);
1156
1157 return GV;
1158}
1159
1160/*
1161 struct objc_protocol_list {
1162 struct objc_protocol_list *next;
1163 long count;
1164 Protocol *list[];
1165 };
1166*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001167llvm::Constant *
1168CGObjCMac::EmitProtocolList(const std::string &Name,
1169 ObjCProtocolDecl::protocol_iterator begin,
1170 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001171 std::vector<llvm::Constant*> ProtocolRefs;
1172
Daniel Dunbardbc933702008-08-21 21:57:41 +00001173 for (; begin != end; ++begin)
1174 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001175
1176 // Just return null for empty protocol lists
1177 if (ProtocolRefs.empty())
1178 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1179
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001180 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001181 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1182
1183 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001184 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001185 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1186 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1187 Values[2] =
1188 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1189 ProtocolRefs.size()),
1190 ProtocolRefs);
1191
1192 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1193 llvm::GlobalVariable *GV =
1194 new llvm::GlobalVariable(Init->getType(), false,
1195 llvm::GlobalValue::InternalLinkage,
1196 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001197 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001198 &CGM.getModule());
1199 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1200 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1201}
1202
1203/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001204 struct _objc_property {
1205 const char * const name;
1206 const char * const attributes;
1207 };
1208
1209 struct _objc_property_list {
1210 uint32_t entsize; // sizeof (struct _objc_property)
1211 uint32_t prop_count;
1212 struct _objc_property[prop_count];
1213 };
1214*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001215llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1216 const Decl *Container,
1217 const ObjCContainerDecl *OCD,
1218 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001219 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001220 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1221 E = OCD->prop_end(); I != E; ++I) {
1222 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001223 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001224 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001225 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1226 Prop));
1227 }
1228
1229 // Return null for empty list.
1230 if (Properties.empty())
1231 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1232
1233 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001234 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001235 std::vector<llvm::Constant*> Values(3);
1236 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1237 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1238 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1239 Properties.size());
1240 Values[2] = llvm::ConstantArray::get(AT, Properties);
1241 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1242
1243 llvm::GlobalVariable *GV =
1244 new llvm::GlobalVariable(Init->getType(), false,
1245 llvm::GlobalValue::InternalLinkage,
1246 Init,
1247 Name,
1248 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001249 if (ObjCABI == 2)
1250 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001251 // No special section on property lists?
1252 UsedGlobals.push_back(GV);
1253 return llvm::ConstantExpr::getBitCast(GV,
1254 ObjCTypes.PropertyListPtrTy);
1255
1256}
1257
1258/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001259 struct objc_method_description_list {
1260 int count;
1261 struct objc_method_description list[];
1262 };
1263*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001264llvm::Constant *
1265CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1266 std::vector<llvm::Constant*> Desc(2);
1267 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1268 ObjCTypes.SelectorPtrTy);
1269 Desc[1] = GetMethodVarType(MD);
1270 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1271 Desc);
1272}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001273
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001274llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1275 const char *Section,
1276 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001277 // Return null for empty list.
1278 if (Methods.empty())
1279 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1280
1281 std::vector<llvm::Constant*> Values(2);
1282 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1283 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1284 Methods.size());
1285 Values[1] = llvm::ConstantArray::get(AT, Methods);
1286 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1287
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001288 llvm::GlobalVariable *GV =
1289 new llvm::GlobalVariable(Init->getType(), false,
1290 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001291 Init, Name, &CGM.getModule());
1292 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001293 UsedGlobals.push_back(GV);
1294 return llvm::ConstantExpr::getBitCast(GV,
1295 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001296}
1297
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001298/*
1299 struct _objc_category {
1300 char *category_name;
1301 char *class_name;
1302 struct _objc_method_list *instance_methods;
1303 struct _objc_method_list *class_methods;
1304 struct _objc_protocol_list *protocols;
1305 uint32_t size; // <rdar://4585769>
1306 struct _objc_property_list *instance_properties;
1307 };
1308 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001309void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001310 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001311
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001312 // FIXME: This is poor design, the OCD should have a pointer to the
1313 // category decl. Additionally, note that Category can be null for
1314 // the @implementation w/o an @interface case. Sema should just
1315 // create one for us as it does for @implementation so everyone else
1316 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001317 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001318 const ObjCCategoryDecl *Category =
1319 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001320 std::string ExtName(Interface->getNameAsString() + "_" +
1321 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001322
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001323 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1324 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1325 e = OCD->instmeth_end(); i != e; ++i) {
1326 // Instance methods should always be defined.
1327 InstanceMethods.push_back(GetMethodConstant(*i));
1328 }
1329 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1330 e = OCD->classmeth_end(); i != e; ++i) {
1331 // Class methods should always be defined.
1332 ClassMethods.push_back(GetMethodConstant(*i));
1333 }
1334
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001335 std::vector<llvm::Constant*> Values(7);
1336 Values[0] = GetClassName(OCD->getIdentifier());
1337 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001338 Values[2] =
1339 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1340 ExtName,
1341 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001342 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001343 Values[3] =
1344 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1345 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001346 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001347 if (Category) {
1348 Values[4] =
1349 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1350 Category->protocol_begin(),
1351 Category->protocol_end());
1352 } else {
1353 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1354 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001355 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001356
1357 // If there is no category @interface then there can be no properties.
1358 if (Category) {
1359 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001360 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001361 } else {
1362 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1363 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001364
1365 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1366 Values);
1367
1368 llvm::GlobalVariable *GV =
1369 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1370 llvm::GlobalValue::InternalLinkage,
1371 Init,
1372 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1373 &CGM.getModule());
1374 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1375 UsedGlobals.push_back(GV);
1376 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001377}
1378
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001379// FIXME: Get from somewhere?
1380enum ClassFlags {
1381 eClassFlags_Factory = 0x00001,
1382 eClassFlags_Meta = 0x00002,
1383 // <rdr://5142207>
1384 eClassFlags_HasCXXStructors = 0x02000,
1385 eClassFlags_Hidden = 0x20000,
1386 eClassFlags_ABI2_Hidden = 0x00010,
1387 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1388};
1389
1390// <rdr://5142207&4705298&4843145>
1391static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1392 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1393 // FIXME: Support -fvisibility
1394 switch (attr->getVisibility()) {
1395 default:
1396 assert(0 && "Unknown visibility");
1397 return false;
1398 case VisibilityAttr::DefaultVisibility:
1399 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1400 return false;
1401 case VisibilityAttr::HiddenVisibility:
1402 return true;
1403 }
1404 } else {
1405 return false; // FIXME: Support -fvisibility
1406 }
1407}
1408
1409/*
1410 struct _objc_class {
1411 Class isa;
1412 Class super_class;
1413 const char *name;
1414 long version;
1415 long info;
1416 long instance_size;
1417 struct _objc_ivar_list *ivars;
1418 struct _objc_method_list *methods;
1419 struct _objc_cache *cache;
1420 struct _objc_protocol_list *protocols;
1421 // Objective-C 1.0 extensions (<rdr://4585769>)
1422 const char *ivar_layout;
1423 struct _objc_class_ext *ext;
1424 };
1425
1426 See EmitClassExtension();
1427 */
1428void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001429 DefinedSymbols.insert(ID->getIdentifier());
1430
Chris Lattner8ec03f52008-11-24 03:54:41 +00001431 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001432 // FIXME: Gross
1433 ObjCInterfaceDecl *Interface =
1434 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001435 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001436 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001437 Interface->protocol_begin(),
1438 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001439 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001440 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001441 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001442 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001443
1444 // FIXME: Set CXX-structors flag.
1445 if (IsClassHidden(ID->getClassInterface()))
1446 Flags |= eClassFlags_Hidden;
1447
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001448 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1449 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1450 e = ID->instmeth_end(); i != e; ++i) {
1451 // Instance methods should always be defined.
1452 InstanceMethods.push_back(GetMethodConstant(*i));
1453 }
1454 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1455 e = ID->classmeth_end(); i != e; ++i) {
1456 // Class methods should always be defined.
1457 ClassMethods.push_back(GetMethodConstant(*i));
1458 }
1459
1460 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1461 e = ID->propimpl_end(); i != e; ++i) {
1462 ObjCPropertyImplDecl *PID = *i;
1463
1464 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1465 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1466
1467 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1468 if (llvm::Constant *C = GetMethodConstant(MD))
1469 InstanceMethods.push_back(C);
1470 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1471 if (llvm::Constant *C = GetMethodConstant(MD))
1472 InstanceMethods.push_back(C);
1473 }
1474 }
1475
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001476 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001477 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001478 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001479 // Record a reference to the super class.
1480 LazySymbols.insert(Super->getIdentifier());
1481
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001482 Values[ 1] =
1483 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1484 ObjCTypes.ClassPtrTy);
1485 } else {
1486 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1487 }
1488 Values[ 2] = GetClassName(ID->getIdentifier());
1489 // Version is always 0.
1490 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1491 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1492 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001493 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001494 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001495 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001496 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001497 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001498 // cache is always NULL.
1499 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1500 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001501 // FIXME: Set ivar_layout
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001502 Values[10] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001503 Values[11] = EmitClassExtension(ID);
1504 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1505 Values);
1506
1507 llvm::GlobalVariable *GV =
1508 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1509 llvm::GlobalValue::InternalLinkage,
1510 Init,
1511 std::string("\01L_OBJC_CLASS_")+ClassName,
1512 &CGM.getModule());
1513 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1514 UsedGlobals.push_back(GV);
1515 // FIXME: Why?
1516 GV->setAlignment(32);
1517 DefinedClasses.push_back(GV);
1518}
1519
1520llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1521 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001522 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001523 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001524 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001525 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001526
1527 if (IsClassHidden(ID->getClassInterface()))
1528 Flags |= eClassFlags_Hidden;
1529
1530 std::vector<llvm::Constant*> Values(12);
1531 // The isa for the metaclass is the root of the hierarchy.
1532 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1533 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1534 Root = Super;
1535 Values[ 0] =
1536 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1537 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001538 // The super class for the metaclass is emitted as the name of the
1539 // super class. The runtime fixes this up to point to the
1540 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001541 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1542 Values[ 1] =
1543 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1544 ObjCTypes.ClassPtrTy);
1545 } else {
1546 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1547 }
1548 Values[ 2] = GetClassName(ID->getIdentifier());
1549 // Version is always 0.
1550 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1551 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1552 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001553 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001554 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001555 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001556 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001557 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001558 // cache is always NULL.
1559 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1560 Values[ 9] = Protocols;
1561 // ivar_layout for metaclass is always NULL.
1562 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1563 // The class extension is always unused for metaclasses.
1564 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1565 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1566 Values);
1567
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001568 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001569 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001570
1571 // Check for a forward reference.
1572 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1573 if (GV) {
1574 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1575 "Forward metaclass reference has incorrect type.");
1576 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1577 GV->setInitializer(Init);
1578 } else {
1579 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1580 llvm::GlobalValue::InternalLinkage,
1581 Init, Name,
1582 &CGM.getModule());
1583 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001584 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1585 UsedGlobals.push_back(GV);
1586 // FIXME: Why?
1587 GV->setAlignment(32);
1588
1589 return GV;
1590}
1591
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001592llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001593 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001594
1595 // FIXME: Should we look these up somewhere other than the
1596 // module. Its a bit silly since we only generate these while
1597 // processing an implementation, so exactly one pointer would work
1598 // if know when we entered/exitted an implementation block.
1599
1600 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001601 // Previously, metaclass with internal linkage may have been defined.
1602 // pass 'true' as 2nd argument so it is returned.
1603 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001604 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1605 "Forward metaclass reference has incorrect type.");
1606 return GV;
1607 } else {
1608 // Generate as an external reference to keep a consistent
1609 // module. This will be patched up when we emit the metaclass.
1610 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1611 llvm::GlobalValue::ExternalLinkage,
1612 0,
1613 Name,
1614 &CGM.getModule());
1615 }
1616}
1617
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001618/*
1619 struct objc_class_ext {
1620 uint32_t size;
1621 const char *weak_ivar_layout;
1622 struct _objc_property_list *properties;
1623 };
1624*/
1625llvm::Constant *
1626CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1627 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001628 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001629
1630 std::vector<llvm::Constant*> Values(3);
1631 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001632 // FIXME: Output weak_ivar_layout string.
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001633 Values[1] = GetIvarLayoutName(0, ObjCTypes);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001634 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001635 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001636
1637 // Return null if no extension bits are used.
1638 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1639 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1640
1641 llvm::Constant *Init =
1642 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1643 llvm::GlobalVariable *GV =
1644 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1645 llvm::GlobalValue::InternalLinkage,
1646 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001647 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001648 &CGM.getModule());
1649 // No special section, but goes in llvm.used
1650 UsedGlobals.push_back(GV);
1651
1652 return GV;
1653}
1654
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001655/// countInheritedIvars - count number of ivars in class and its super class(s)
1656///
1657static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1658 int count = 0;
1659 if (!OI)
1660 return 0;
1661 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1662 if (SuperClass)
1663 count += countInheritedIvars(SuperClass);
1664 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1665 E = OI->ivar_end(); I != E; ++I)
1666 ++count;
1667 return count;
1668}
1669
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001670/// getInterfaceDeclForIvar - Get the interface declaration node where
1671/// this ivar is declared in.
1672/// FIXME. Ideally, this info should be in the ivar node. But currently
1673/// it is not and prevailing wisdom is that ASTs should not have more
1674/// info than is absolutely needed, even though this info reflects the
1675/// source language.
1676///
1677static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1678 const ObjCInterfaceDecl *OI,
1679 const ObjCIvarDecl *IVD) {
1680 if (!OI)
1681 return 0;
1682 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1683 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1684 E = OI->ivar_end(); I != E; ++I)
1685 if ((*I)->getIdentifier() == IVD->getIdentifier())
1686 return OI;
1687 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1688}
1689
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001690/*
1691 struct objc_ivar {
1692 char *ivar_name;
1693 char *ivar_type;
1694 int ivar_offset;
1695 };
1696
1697 struct objc_ivar_list {
1698 int ivar_count;
1699 struct objc_ivar list[count];
1700 };
1701 */
1702llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001703 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001704 std::vector<llvm::Constant*> Ivars, Ivar(3);
1705
1706 // When emitting the root class GCC emits ivar entries for the
1707 // actual class structure. It is not clear if we need to follow this
1708 // behavior; for now lets try and get away with not doing it. If so,
1709 // the cleanest solution would be to make up an ObjCInterfaceDecl
1710 // for the class.
1711 if (ForClass)
1712 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001713
1714 ObjCInterfaceDecl *OID =
1715 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1716 const llvm::Type *InterfaceTy =
1717 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001718 const llvm::StructLayout *Layout =
1719 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001720
1721 RecordDecl::field_iterator ifield, pfield;
1722 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001723 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1724 FieldDecl *Field = *ifield;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001725 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001726 if (Field->getIdentifier())
1727 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1728 else
1729 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00001730 Ivar[1] = GetMethodVarType(Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001731 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001732 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001733 }
1734
1735 // Return null for empty list.
1736 if (Ivars.empty())
1737 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1738
1739 std::vector<llvm::Constant*> Values(2);
1740 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1741 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1742 Ivars.size());
1743 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1744 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1745
1746 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1747 "\01L_OBJC_INSTANCE_VARIABLES_");
1748 llvm::GlobalVariable *GV =
1749 new llvm::GlobalVariable(Init->getType(), false,
1750 llvm::GlobalValue::InternalLinkage,
1751 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001752 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001753 &CGM.getModule());
1754 if (ForClass) {
1755 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1756 // FIXME: Why is this only here?
1757 GV->setAlignment(32);
1758 } else {
1759 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1760 }
1761 UsedGlobals.push_back(GV);
1762 return llvm::ConstantExpr::getBitCast(GV,
1763 ObjCTypes.IvarListPtrTy);
1764}
1765
1766/*
1767 struct objc_method {
1768 SEL method_name;
1769 char *method_types;
1770 void *method;
1771 };
1772
1773 struct objc_method_list {
1774 struct objc_method_list *obsolete;
1775 int count;
1776 struct objc_method methods_list[count];
1777 };
1778*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001779
1780/// GetMethodConstant - Return a struct objc_method constant for the
1781/// given method if it has been defined. The result is null if the
1782/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001783llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001784 // FIXME: Use DenseMap::lookup
1785 llvm::Function *Fn = MethodDefinitions[MD];
1786 if (!Fn)
1787 return 0;
1788
1789 std::vector<llvm::Constant*> Method(3);
1790 Method[0] =
1791 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1792 ObjCTypes.SelectorPtrTy);
1793 Method[1] = GetMethodVarType(MD);
1794 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1795 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1796}
1797
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001798llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1799 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001800 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001801 // Return null for empty list.
1802 if (Methods.empty())
1803 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1804
1805 std::vector<llvm::Constant*> Values(3);
1806 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1807 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1808 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1809 Methods.size());
1810 Values[2] = llvm::ConstantArray::get(AT, Methods);
1811 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1812
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001813 llvm::GlobalVariable *GV =
1814 new llvm::GlobalVariable(Init->getType(), false,
1815 llvm::GlobalValue::InternalLinkage,
1816 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001817 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001818 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001819 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001820 UsedGlobals.push_back(GV);
1821 return llvm::ConstantExpr::getBitCast(GV,
1822 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001823}
1824
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001825llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001826 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001827 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001828 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001829
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001830 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001831 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001832 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001833 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001834 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001835 llvm::GlobalValue::InternalLinkage,
1836 Name,
1837 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001838 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001839
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001840 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001841}
1842
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001843uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
1844 FieldDecl *Field) {
1845 return Field->isBitField()
1846 ? CGM.getTypes().getLLVMFieldNo(Field)
1847 : Layout->getElementOffset(
1848 CGM.getTypes().getLLVMFieldNo(Field));
1849}
1850
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001851llvm::GlobalVariable *
1852CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
1853 llvm::Constant *Init,
1854 const char *Section,
1855 bool SetAlignment,
1856 bool IsUsed) {
1857 const llvm::Type *Ty = Init->getType();
1858 llvm::GlobalVariable *GV =
1859 new llvm::GlobalVariable(Ty, false,
1860 llvm::GlobalValue::InternalLinkage,
1861 Init,
1862 Name,
1863 &CGM.getModule());
1864 if (Section)
1865 GV->setSection(Section);
1866 if (SetAlignment)
1867 GV->setAlignment(CGM.getTargetData().getPreferredAlignment(Ty));
1868 if (IsUsed)
1869 UsedGlobals.push_back(GV);
1870 return GV;
1871}
1872
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001873llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001874 // Abuse this interface function as a place to finalize.
1875 FinishModule();
1876
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001877 return NULL;
1878}
1879
Daniel Dunbar49f66022008-09-24 03:38:44 +00001880llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1881 return ObjCTypes.GetPropertyFn;
1882}
1883
1884llvm::Function *CGObjCMac::GetPropertySetFunction() {
1885 return ObjCTypes.SetPropertyFn;
1886}
1887
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001888llvm::Function *CGObjCMac::EnumerationMutationFunction()
1889{
1890 return ObjCTypes.EnumerationMutationFn;
1891}
1892
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001893/*
1894
1895Objective-C setjmp-longjmp (sjlj) Exception Handling
1896--
1897
1898The basic framework for a @try-catch-finally is as follows:
1899{
1900 objc_exception_data d;
1901 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001902 bool _call_try_exit = true;
1903
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001904 objc_exception_try_enter(&d);
1905 if (!setjmp(d.jmp_buf)) {
1906 ... try body ...
1907 } else {
1908 // exception path
1909 id _caught = objc_exception_extract(&d);
1910
1911 // enter new try scope for handlers
1912 if (!setjmp(d.jmp_buf)) {
1913 ... match exception and execute catch blocks ...
1914
1915 // fell off end, rethrow.
1916 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001917 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001918 } else {
1919 // exception in catch block
1920 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001921 _call_try_exit = false;
1922 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001923 }
1924 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001925 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001926
1927finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001928 if (_call_try_exit)
1929 objc_exception_try_exit(&d);
1930
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001931 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001932 ... dispatch to finally destination ...
1933
1934finally_rethrow:
1935 objc_exception_throw(_rethrow);
1936
1937finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001938}
1939
1940This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001941uses _rethrow to determine if objc_exception_try_exit should be called
1942and if the object should be rethrown. This breaks in the face of
1943throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001944
1945We specialize this framework for a few particular circumstances:
1946
1947 - If there are no catch blocks, then we avoid emitting the second
1948 exception handling context.
1949
1950 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1951 e)) we avoid emitting the code to rethrow an uncaught exception.
1952
1953 - FIXME: If there is no @finally block we can do a few more
1954 simplifications.
1955
1956Rethrows and Jumps-Through-Finally
1957--
1958
1959Support for implicit rethrows and jumping through the finally block is
1960handled by storing the current exception-handling context in
1961ObjCEHStack.
1962
Daniel Dunbar898d5082008-09-30 01:06:03 +00001963In order to implement proper @finally semantics, we support one basic
1964mechanism for jumping through the finally block to an arbitrary
1965destination. Constructs which generate exits from a @try or @catch
1966block use this mechanism to implement the proper semantics by chaining
1967jumps, as necessary.
1968
1969This mechanism works like the one used for indirect goto: we
1970arbitrarily assign an ID to each destination and store the ID for the
1971destination in a variable prior to entering the finally block. At the
1972end of the finally block we simply create a switch to the proper
1973destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001974
1975Code gen for @synchronized(expr) stmt;
1976Effectively generating code for:
1977objc_sync_enter(expr);
1978@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001979*/
1980
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001981void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1982 const Stmt &S) {
1983 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001984 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001985 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001986 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001987 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1988 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1989 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00001990
1991 // For @synchronized, call objc_sync_enter(sync.expr). The
1992 // evaluation of the expression must occur before we enter the
1993 // @synchronized. We can safely avoid a temp here because jumps into
1994 // @synchronized are illegal & this will dominate uses.
1995 llvm::Value *SyncArg = 0;
1996 if (!isTry) {
1997 SyncArg =
1998 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1999 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
2000 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
2001 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002002
2003 // Push an EH context entry, used for handling rethrows and jumps
2004 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002005 CGF.PushCleanupBlock(FinallyBlock);
2006
Anders Carlsson273558f2009-02-07 21:37:21 +00002007 CGF.ObjCEHValueStack.push_back(0);
2008
Daniel Dunbar898d5082008-09-30 01:06:03 +00002009 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002010 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2011 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002012 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2013 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002014 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2015 "_call_try_exit");
2016 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2017
Anders Carlsson80f25672008-09-09 17:59:25 +00002018 // Enter a new try block and call setjmp.
2019 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
2020 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2021 "jmpbufarray");
2022 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
2023 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2024 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002025
Daniel Dunbar55e87422008-11-11 02:29:29 +00002026 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2027 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002028 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002029 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002030
2031 // Emit the @try block.
2032 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002033 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2034 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002035 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002036
2037 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002038 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002039
2040 // Retrieve the exception object. We may emit multiple blocks but
2041 // nothing can cross this so the value is already in SSA form.
2042 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2043 ExceptionData,
2044 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002045 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002046 if (!isTry)
2047 {
2048 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002049 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002050 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002051 }
2052 else if (const ObjCAtCatchStmt* CatchStmt =
2053 cast<ObjCAtTryStmt>(S).getCatchStmts())
2054 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002055 // Enter a new exception try block (in case a @catch block throws
2056 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00002057 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002058
Anders Carlsson80f25672008-09-09 17:59:25 +00002059 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2060 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002061 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002062
Daniel Dunbar55e87422008-11-11 02:29:29 +00002063 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2064 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002065 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002066
2067 CGF.EmitBlock(CatchBlock);
2068
Daniel Dunbar55e40722008-09-27 07:03:52 +00002069 // Handle catch list. As a special case we check if everything is
2070 // matched and avoid generating code for falling off the end if
2071 // so.
2072 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002073 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002074 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002075
Steve Naroff7ba138a2009-03-03 19:52:17 +00002076 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002077 const PointerType *PT = 0;
2078
Anders Carlsson80f25672008-09-09 17:59:25 +00002079 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002080 if (!CatchParam) {
2081 AllMatched = true;
2082 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002083 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002084
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002085 // catch(id e) always matches.
2086 // FIXME: For the time being we also match id<X>; this should
2087 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002088 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002089 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002090 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002091 }
2092
Daniel Dunbar55e40722008-09-27 07:03:52 +00002093 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002094 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002095 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002096 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002097 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002098 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002099
Anders Carlssondde0a942008-09-11 09:15:33 +00002100 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002101 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002102 break;
2103 }
2104
Daniel Dunbar129271a2008-09-27 07:36:24 +00002105 assert(PT && "Unexpected non-pointer type in @catch");
2106 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002107 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002108 assert(ObjCType && "Catch parameter must have Objective-C type!");
2109
2110 // Check if the @catch block matches the exception object.
2111 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2112
Anders Carlsson80f25672008-09-09 17:59:25 +00002113 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2114 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002115
Daniel Dunbar55e87422008-11-11 02:29:29 +00002116 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002117
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002118 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002119 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002120
2121 // Emit the @catch block.
2122 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002123 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002124 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002125
2126 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002127 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002128 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002129 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002130
2131 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002132 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002133
2134 CGF.EmitBlock(NextCatchBlock);
2135 }
2136
Daniel Dunbar55e40722008-09-27 07:03:52 +00002137 if (!AllMatched) {
2138 // None of the handlers caught the exception, so store it to be
2139 // rethrown at the end of the @finally block.
2140 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002141 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002142 }
2143
2144 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002145 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002146 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2147 ExceptionData),
2148 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002149 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002150 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002151 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002152 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002153 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002154 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002155 }
2156
Daniel Dunbar898d5082008-09-30 01:06:03 +00002157 // Pop the exception-handling stack entry. It is important to do
2158 // this now, because the code in the @finally block is not in this
2159 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002160 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2161
Anders Carlsson273558f2009-02-07 21:37:21 +00002162 CGF.ObjCEHValueStack.pop_back();
2163
Anders Carlsson80f25672008-09-09 17:59:25 +00002164 // Emit the @finally block.
2165 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002166 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2167
2168 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2169
2170 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002171 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002172
2173 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002174 if (isTry) {
2175 if (const ObjCAtFinallyStmt* FinallyStmt =
2176 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2177 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002178 } else {
2179 // Emit objc_sync_exit(expr); as finally's sole statement for
2180 // @synchronized.
2181 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002182 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002183
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002184 // Emit the switch block
2185 if (Info.SwitchBlock)
2186 CGF.EmitBlock(Info.SwitchBlock);
2187 if (Info.EndBlock)
2188 CGF.EmitBlock(Info.EndBlock);
2189
Daniel Dunbar898d5082008-09-30 01:06:03 +00002190 CGF.EmitBlock(FinallyRethrow);
2191 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2192 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002193 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002194
2195 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002196}
2197
2198void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002199 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002200 llvm::Value *ExceptionAsObject;
2201
2202 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2203 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2204 ExceptionAsObject =
2205 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2206 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002207 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002208 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002209 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002210 }
2211
2212 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002213 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002214
2215 // Clear the insertion point to indicate we are in unreachable code.
2216 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002217}
2218
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002219/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002220/// object: objc_read_weak (id *src)
2221///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002222llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002223 llvm::Value *AddrWeakObj)
2224{
Eli Friedman8339b352009-03-07 03:57:15 +00002225 const llvm::Type* DestTy =
2226 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002227 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002228 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002229 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002230 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002231 return read_weak;
2232}
2233
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002234/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2235/// objc_assign_weak (id src, id *dst)
2236///
2237void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2238 llvm::Value *src, llvm::Value *dst)
2239{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002240 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2241 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002242 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2243 src, dst, "weakassign");
2244 return;
2245}
2246
Fariborz Jahanian58626502008-11-19 00:59:10 +00002247/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2248/// objc_assign_global (id src, id *dst)
2249///
2250void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2251 llvm::Value *src, llvm::Value *dst)
2252{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002253 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2254 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002255 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2256 src, dst, "globalassign");
2257 return;
2258}
2259
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002260/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2261/// objc_assign_ivar (id src, id *dst)
2262///
2263void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2264 llvm::Value *src, llvm::Value *dst)
2265{
2266 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2267 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2268 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2269 src, dst, "assignivar");
2270 return;
2271}
2272
Fariborz Jahanian58626502008-11-19 00:59:10 +00002273/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2274/// objc_assign_strongCast (id src, id *dst)
2275///
2276void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2277 llvm::Value *src, llvm::Value *dst)
2278{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002279 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2280 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002281 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2282 src, dst, "weakassign");
2283 return;
2284}
2285
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002286/// EmitObjCValueForIvar - Code Gen for ivar reference.
2287///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002288LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2289 QualType ObjectTy,
2290 llvm::Value *BaseValue,
2291 const ObjCIvarDecl *Ivar,
2292 const FieldDecl *Field,
2293 unsigned CVRQualifiers) {
2294 if (Ivar->isBitField())
2295 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2296 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002297 // TODO: Add a special case for isa (index 0)
2298 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2299 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002300 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002301 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2302 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002303 LValue::SetObjCIvar(LV, true);
2304 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002305}
2306
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002307llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2308 ObjCInterfaceDecl *Interface,
2309 const ObjCIvarDecl *Ivar) {
2310 const llvm::Type *InterfaceLTy =
2311 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2312 const llvm::StructLayout *Layout =
2313 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2314 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00002315 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002316 return llvm::ConstantInt::get(
2317 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2318 Offset);
2319}
2320
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002321/* *** Private Interface *** */
2322
2323/// EmitImageInfo - Emit the image info marker used to encode some module
2324/// level information.
2325///
2326/// See: <rdr://4810609&4810587&4810587>
2327/// struct IMAGE_INFO {
2328/// unsigned version;
2329/// unsigned flags;
2330/// };
2331enum ImageInfoFlags {
2332 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2333 eImageInfo_GarbageCollected = (1 << 1),
2334 eImageInfo_GCOnly = (1 << 2)
2335};
2336
2337void CGObjCMac::EmitImageInfo() {
2338 unsigned version = 0; // Version is unused?
2339 unsigned flags = 0;
2340
2341 // FIXME: Fix and continue?
2342 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2343 flags |= eImageInfo_GarbageCollected;
2344 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2345 flags |= eImageInfo_GCOnly;
2346
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002347 // Emitted as int[2];
2348 llvm::Constant *values[2] = {
2349 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2350 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2351 };
2352 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002353 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002354 new llvm::GlobalVariable(AT, true,
2355 llvm::GlobalValue::InternalLinkage,
2356 llvm::ConstantArray::get(AT, values, 2),
2357 "\01L_OBJC_IMAGE_INFO",
2358 &CGM.getModule());
2359
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002360 if (ObjCABI == 1) {
2361 GV->setSection("__OBJC, __image_info,regular");
2362 } else {
2363 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2364 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002365
2366 UsedGlobals.push_back(GV);
2367}
2368
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002369
2370// struct objc_module {
2371// unsigned long version;
2372// unsigned long size;
2373// const char *name;
2374// Symtab symtab;
2375// };
2376
2377// FIXME: Get from somewhere
2378static const int ModuleVersion = 7;
2379
2380void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002381 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002382
2383 std::vector<llvm::Constant*> Values(4);
2384 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2385 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002386 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002387 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002388 Values[3] = EmitModuleSymbols();
2389
2390 llvm::GlobalVariable *GV =
2391 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2392 llvm::GlobalValue::InternalLinkage,
2393 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2394 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002395 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002396 &CGM.getModule());
2397 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2398 UsedGlobals.push_back(GV);
2399}
2400
2401llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002402 unsigned NumClasses = DefinedClasses.size();
2403 unsigned NumCategories = DefinedCategories.size();
2404
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002405 // Return null if no symbols were defined.
2406 if (!NumClasses && !NumCategories)
2407 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2408
2409 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002410 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2411 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2412 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2413 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2414
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002415 // The runtime expects exactly the list of defined classes followed
2416 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002417 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002418 for (unsigned i=0; i<NumClasses; i++)
2419 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2420 ObjCTypes.Int8PtrTy);
2421 for (unsigned i=0; i<NumCategories; i++)
2422 Symbols[NumClasses + i] =
2423 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2424 ObjCTypes.Int8PtrTy);
2425
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002427 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428 NumClasses + NumCategories),
2429 Symbols);
2430
2431 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2432
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002433 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002435 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002436 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002437 "\01L_OBJC_SYMBOLS",
2438 &CGM.getModule());
2439 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2440 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2442}
2443
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002444llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002446 LazySymbols.insert(ID->getIdentifier());
2447
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2449
2450 if (!Entry) {
2451 llvm::Constant *Casted =
2452 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2453 ObjCTypes.ClassPtrTy);
2454 Entry =
2455 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2456 llvm::GlobalValue::InternalLinkage,
2457 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2458 &CGM.getModule());
2459 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2460 UsedGlobals.push_back(Entry);
2461 }
2462
2463 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002464}
2465
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002466llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002467 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2468
2469 if (!Entry) {
2470 llvm::Constant *Casted =
2471 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2472 ObjCTypes.SelectorPtrTy);
2473 Entry =
2474 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2475 llvm::GlobalValue::InternalLinkage,
2476 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2477 &CGM.getModule());
2478 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2479 UsedGlobals.push_back(Entry);
2480 }
2481
2482 return Builder.CreateLoad(Entry, false, "tmp");
2483}
2484
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002485llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002486 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002487
2488 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002489 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002490 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002491 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002492 llvm::GlobalValue::InternalLinkage,
2493 C, "\01L_OBJC_CLASS_NAME_",
2494 &CGM.getModule());
2495 Entry->setSection("__TEXT,__cstring,cstring_literals");
2496 UsedGlobals.push_back(Entry);
2497 }
2498
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002499 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002500}
2501
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002502/// GetIvarLayoutName - Returns a unique constant for the given
2503/// ivar layout bitmap.
2504llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2505 const ObjCCommonTypesHelper &ObjCTypes) {
2506 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2507}
2508
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002509void CGObjCCommonMac::BuildAggrIvarLayout(RecordDecl *RD,
2510 const std::vector<FieldDecl*>& RecFields,
2511 unsigned int BytePos, bool ForStrongLayout,
2512 int &Index, int &SkIndex, bool &HasUnion) {
2513 return;
2514}
2515
2516/// BuildIvarLayout - Builds ivar layout bitmap for the class
2517/// implementation for the __strong or __weak case.
2518/// The layout map displays which words in ivar list must be skipped
2519/// and which must be scanned by GC (see below). String is built of bytes.
2520/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
2521/// of words to skip and right nibble is count of words to scan. So, each
2522/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
2523/// represented by a 0x00 byte which also ends the string.
2524/// 1. when ForStrongLayout is true, following ivars are scanned:
2525/// - id, Class
2526/// - object *
2527/// - __strong anything
2528///
2529/// 2. When ForStrongLayout is false, following ivars are scanned:
2530/// - __weak anything
2531///
2532llvm::Constant *CGObjCCommonMac::BuildIvarLayout(ObjCImplementationDecl *OMD,
2533 bool ForStrongLayout) {
2534 int iIndex = -1;
2535 int iSkIndex = -1;
2536 bool hasUnion = false;
2537
2538 std::vector<FieldDecl*> RecFields;
2539 ObjCInterfaceDecl *OI = OMD->getClassInterface();
2540 CGM.getContext().CollectObjCIvars(OI, RecFields);
2541 if (RecFields.empty())
2542 return 0;
2543 BuildAggrIvarLayout (0, RecFields, 0, ForStrongLayout,
2544 iIndex, iSkIndex, hasUnion);
2545 return 0;
2546}
2547
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002548llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002549 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2550
2551 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002552 // FIXME: Avoid std::string copying.
2553 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002554 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002555 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002556 llvm::GlobalValue::InternalLinkage,
2557 C, "\01L_OBJC_METH_VAR_NAME_",
2558 &CGM.getModule());
2559 Entry->setSection("__TEXT,__cstring,cstring_literals");
2560 UsedGlobals.push_back(Entry);
2561 }
2562
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002563 return getConstantGEP(Entry, 0, 0);
2564}
2565
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002566// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002567llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002568 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2569}
2570
2571// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002572llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002573 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2574}
2575
Devang Patel7794bb82009-03-04 18:21:39 +00002576llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) {
2577 std::string TypeStr;
2578 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
2579
2580 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002581
2582 if (!Entry) {
Devang Patel7794bb82009-03-04 18:21:39 +00002583 llvm::Constant *C = llvm::ConstantArray::get(TypeStr);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002584 Entry =
2585 new llvm::GlobalVariable(C->getType(), false,
2586 llvm::GlobalValue::InternalLinkage,
2587 C, "\01L_OBJC_METH_VAR_TYPE_",
2588 &CGM.getModule());
2589 Entry->setSection("__TEXT,__cstring,cstring_literals");
2590 UsedGlobals.push_back(Entry);
2591 }
2592
2593 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002594}
2595
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002596llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002597 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002598 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2599 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00002600
2601 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
2602
2603 if (!Entry) {
2604 llvm::Constant *C = llvm::ConstantArray::get(TypeStr);
2605 Entry =
2606 new llvm::GlobalVariable(C->getType(), false,
2607 llvm::GlobalValue::InternalLinkage,
2608 C, "\01L_OBJC_METH_VAR_TYPE_",
2609 &CGM.getModule());
2610 Entry->setSection("__TEXT,__cstring,cstring_literals");
2611 UsedGlobals.push_back(Entry);
2612 }
2613
2614 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002615}
2616
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002617// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002618llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002619 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2620
2621 if (!Entry) {
2622 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2623 Entry =
2624 new llvm::GlobalVariable(C->getType(), false,
2625 llvm::GlobalValue::InternalLinkage,
2626 C, "\01L_OBJC_PROP_NAME_ATTR_",
2627 &CGM.getModule());
2628 Entry->setSection("__TEXT,__cstring,cstring_literals");
2629 UsedGlobals.push_back(Entry);
2630 }
2631
2632 return getConstantGEP(Entry, 0, 0);
2633}
2634
2635// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002636// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002637llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002638 const Decl *Container) {
2639 std::string TypeStr;
2640 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002641 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2642}
2643
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002644void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2645 const ObjCContainerDecl *CD,
2646 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002647 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002648 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002649 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002650 assert (CD && "Missing container decl in GetNameForMethod");
2651 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002652 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2653 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002654 NameOut += ' ';
2655 NameOut += D->getSelector().getAsString();
2656 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002657}
2658
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002659/// GetFirstIvarInRecord - This routine returns the record for the
2660/// implementation of the fiven class OID. It also returns field
2661/// corresponding to the first ivar in the class in FIV. It also
2662/// returns the one before the first ivar.
2663///
2664const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2665 const ObjCInterfaceDecl *OID,
2666 RecordDecl::field_iterator &FIV,
2667 RecordDecl::field_iterator &PIV) {
2668 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2669 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2670 RecordDecl::field_iterator ifield = RD->field_begin();
2671 RecordDecl::field_iterator pfield = RD->field_end();
2672 while (countSuperClassIvars-- > 0) {
2673 pfield = ifield;
2674 ++ifield;
2675 }
2676 FIV = ifield;
2677 PIV = pfield;
2678 return RD;
2679}
2680
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002681void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002682 EmitModuleInfo();
2683
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002684 // Emit the dummy bodies for any protocols which were referenced but
2685 // never defined.
2686 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2687 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2688 if (i->second->hasInitializer())
2689 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002690
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002691 std::vector<llvm::Constant*> Values(5);
2692 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2693 Values[1] = GetClassName(i->first);
2694 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2695 Values[3] = Values[4] =
2696 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2697 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2698 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2699 Values));
2700 }
2701
2702 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002703 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002704 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002705 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002706 }
2707
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002708 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002709 llvm::GlobalValue *GV =
2710 new llvm::GlobalVariable(AT, false,
2711 llvm::GlobalValue::AppendingLinkage,
2712 llvm::ConstantArray::get(AT, Used),
2713 "llvm.used",
2714 &CGM.getModule());
2715
2716 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002717
2718 // Add assembler directives to add lazy undefined symbol references
2719 // for classes which are referenced but not defined. This is
2720 // important for correct linker interaction.
2721
2722 // FIXME: Uh, this isn't particularly portable.
2723 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002724
2725 if (!CGM.getModule().getModuleInlineAsm().empty())
2726 s << "\n";
2727
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002728 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2729 e = LazySymbols.end(); i != e; ++i) {
2730 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2731 }
2732 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2733 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002734 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002735 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2736 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002737
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002738 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002739}
2740
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002741CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002742 : CGObjCCommonMac(cgm),
2743 ObjCTypes(cgm)
2744{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002745 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002746 ObjCABI = 2;
2747}
2748
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002749/* *** */
2750
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002751ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2752: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002753{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002754 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2755 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002756
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002757 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002758 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002759 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002760 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2761
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002762 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002763 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002764 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002765
2766 // FIXME: It would be nice to unify this with the opaque type, so
2767 // that the IR comes out a bit cleaner.
2768 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2769 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002770
2771 // I'm not sure I like this. The implicit coordination is a bit
2772 // gross. We should solve this in a reasonable fashion because this
2773 // is a pretty common task (match some runtime data structure with
2774 // an LLVM data structure).
2775
2776 // FIXME: This is leaked.
2777 // FIXME: Merge with rewriter code?
2778
2779 // struct _objc_super {
2780 // id self;
2781 // Class cls;
2782 // }
2783 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2784 SourceLocation(),
2785 &Ctx.Idents.get("_objc_super"));
2786 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2787 Ctx.getObjCIdType(), 0, false));
2788 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2789 Ctx.getObjCClassType(), 0, false));
2790 RD->completeDefinition(Ctx);
2791
2792 SuperCTy = Ctx.getTagDeclType(RD);
2793 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2794
2795 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002796 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2797
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002798 // struct _prop_t {
2799 // char *name;
2800 // char *attributes;
2801 // }
2802 PropertyTy = llvm::StructType::get(Int8PtrTy,
2803 Int8PtrTy,
2804 NULL);
2805 CGM.getModule().addTypeName("struct._prop_t",
2806 PropertyTy);
2807
2808 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002809 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002810 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002811 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002812 // }
2813 PropertyListTy = llvm::StructType::get(IntTy,
2814 IntTy,
2815 llvm::ArrayType::get(PropertyTy, 0),
2816 NULL);
2817 CGM.getModule().addTypeName("struct._prop_list_t",
2818 PropertyListTy);
2819 // struct _prop_list_t *
2820 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2821
2822 // struct _objc_method {
2823 // SEL _cmd;
2824 // char *method_type;
2825 // char *_imp;
2826 // }
2827 MethodTy = llvm::StructType::get(SelectorPtrTy,
2828 Int8PtrTy,
2829 Int8PtrTy,
2830 NULL);
2831 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002832
2833 // struct _objc_cache *
2834 CacheTy = llvm::OpaqueType::get();
2835 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2836 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002837
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002838 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002839
2840 QualType IdType = Ctx.getObjCIdType();
2841 QualType SelType = Ctx.getObjCSelType();
2842 llvm::SmallVector<QualType,16> Params;
2843 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002844
2845 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002846 Params.push_back(IdType);
2847 Params.push_back(SelType);
2848 Params.push_back(Ctx.LongTy);
2849 Params.push_back(Ctx.BoolTy);
2850 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2851 false);
2852 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002853
2854 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2855 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002856 Params.push_back(IdType);
2857 Params.push_back(SelType);
2858 Params.push_back(Ctx.LongTy);
2859 Params.push_back(IdType);
2860 Params.push_back(Ctx.BoolTy);
2861 Params.push_back(Ctx.BoolTy);
2862 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2863 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2864
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002865 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002866
2867 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002868 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002869 Params.push_back(IdType);
2870 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2871 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2872 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002873
2874 // gc's API
2875 // id objc_read_weak (id *)
2876 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002877 Params.push_back(Ctx.getPointerType(IdType));
2878 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2879 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2880
2881 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002882 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002883 Params.push_back(IdType);
2884 Params.push_back(Ctx.getPointerType(IdType));
2885
2886 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2887 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2888 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2889 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2890 GcAssignStrongCastFn =
2891 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002892
2893 // void objc_exception_throw(id)
2894 Params.clear();
2895 Params.push_back(IdType);
2896
2897 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00002898 ExceptionThrowFn =
2899 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002900
2901 // synchronized APIs
2902 // void objc_sync_enter (id)
2903 // void objc_sync_exit (id)
2904 Params.clear();
2905 Params.push_back(IdType);
2906
2907 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2908 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
2909 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002910}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002911
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002912ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2913 : ObjCCommonTypesHelper(cgm)
2914{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002915 // struct _objc_method_description {
2916 // SEL name;
2917 // char *types;
2918 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002919 MethodDescriptionTy =
2920 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002921 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002922 NULL);
2923 CGM.getModule().addTypeName("struct._objc_method_description",
2924 MethodDescriptionTy);
2925
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002926 // struct _objc_method_description_list {
2927 // int count;
2928 // struct _objc_method_description[1];
2929 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002930 MethodDescriptionListTy =
2931 llvm::StructType::get(IntTy,
2932 llvm::ArrayType::get(MethodDescriptionTy, 0),
2933 NULL);
2934 CGM.getModule().addTypeName("struct._objc_method_description_list",
2935 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002936
2937 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002938 MethodDescriptionListPtrTy =
2939 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2940
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002941 // Protocol description structures
2942
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002943 // struct _objc_protocol_extension {
2944 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2945 // struct _objc_method_description_list *optional_instance_methods;
2946 // struct _objc_method_description_list *optional_class_methods;
2947 // struct _objc_property_list *instance_properties;
2948 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002949 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002950 llvm::StructType::get(IntTy,
2951 MethodDescriptionListPtrTy,
2952 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002953 PropertyListPtrTy,
2954 NULL);
2955 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2956 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002957
2958 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002959 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2960
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002961 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002962
2963 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2964 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2965
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002966 const llvm::Type *T =
2967 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2968 LongTy,
2969 llvm::ArrayType::get(ProtocolTyHolder, 0),
2970 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002971 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2972
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002973 // struct _objc_protocol {
2974 // struct _objc_protocol_extension *isa;
2975 // char *protocol_name;
2976 // struct _objc_protocol **_objc_protocol_list;
2977 // struct _objc_method_description_list *instance_methods;
2978 // struct _objc_method_description_list *class_methods;
2979 // }
2980 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002981 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002982 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2983 MethodDescriptionListPtrTy,
2984 MethodDescriptionListPtrTy,
2985 NULL);
2986 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2987
2988 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2989 CGM.getModule().addTypeName("struct._objc_protocol_list",
2990 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002991 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002992 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2993
2994 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002995 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002996 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002997
2998 // Class description structures
2999
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003000 // struct _objc_ivar {
3001 // char *ivar_name;
3002 // char *ivar_type;
3003 // int ivar_offset;
3004 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003005 IvarTy = llvm::StructType::get(Int8PtrTy,
3006 Int8PtrTy,
3007 IntTy,
3008 NULL);
3009 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3010
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003011 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003012 IvarListTy = llvm::OpaqueType::get();
3013 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3014 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3015
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003016 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003017 MethodListTy = llvm::OpaqueType::get();
3018 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3019 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3020
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003021 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003022 ClassExtensionTy =
3023 llvm::StructType::get(IntTy,
3024 Int8PtrTy,
3025 PropertyListPtrTy,
3026 NULL);
3027 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3028 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3029
3030 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3031
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003032 // struct _objc_class {
3033 // Class isa;
3034 // Class super_class;
3035 // char *name;
3036 // long version;
3037 // long info;
3038 // long instance_size;
3039 // struct _objc_ivar_list *ivars;
3040 // struct _objc_method_list *methods;
3041 // struct _objc_cache *cache;
3042 // struct _objc_protocol_list *protocols;
3043 // char *ivar_layout;
3044 // struct _objc_class_ext *ext;
3045 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003046 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3047 llvm::PointerType::getUnqual(ClassTyHolder),
3048 Int8PtrTy,
3049 LongTy,
3050 LongTy,
3051 LongTy,
3052 IvarListPtrTy,
3053 MethodListPtrTy,
3054 CachePtrTy,
3055 ProtocolListPtrTy,
3056 Int8PtrTy,
3057 ClassExtensionPtrTy,
3058 NULL);
3059 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3060
3061 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3062 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3063 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3064
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003065 // struct _objc_category {
3066 // char *category_name;
3067 // char *class_name;
3068 // struct _objc_method_list *instance_method;
3069 // struct _objc_method_list *class_method;
3070 // uint32_t size; // sizeof(struct _objc_category)
3071 // struct _objc_property_list *instance_properties;// category's @property
3072 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003073 CategoryTy = llvm::StructType::get(Int8PtrTy,
3074 Int8PtrTy,
3075 MethodListPtrTy,
3076 MethodListPtrTy,
3077 ProtocolListPtrTy,
3078 IntTy,
3079 PropertyListPtrTy,
3080 NULL);
3081 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3082
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003083 // Global metadata structures
3084
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003085 // struct _objc_symtab {
3086 // long sel_ref_cnt;
3087 // SEL *refs;
3088 // short cls_def_cnt;
3089 // short cat_def_cnt;
3090 // char *defs[cls_def_cnt + cat_def_cnt];
3091 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003092 SymtabTy = llvm::StructType::get(LongTy,
3093 SelectorPtrTy,
3094 ShortTy,
3095 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003096 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003097 NULL);
3098 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3099 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3100
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003101 // struct _objc_module {
3102 // long version;
3103 // long size; // sizeof(struct _objc_module)
3104 // char *name;
3105 // struct _objc_symtab* symtab;
3106 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003107 ModuleTy =
3108 llvm::StructType::get(LongTy,
3109 LongTy,
3110 Int8PtrTy,
3111 SymtabPtrTy,
3112 NULL);
3113 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003114
Daniel Dunbar49f66022008-09-24 03:38:44 +00003115 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003116
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003117 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003118 std::vector<const llvm::Type*> Params;
3119 Params.push_back(ObjectPtrTy);
3120 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003121 MessageSendFn =
3122 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3123 Params,
3124 true),
3125 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003126
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003127 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003128 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003129 Params.push_back(ObjectPtrTy);
3130 Params.push_back(SelectorPtrTy);
3131 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003132 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3133 Params,
3134 true),
3135 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003136
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003137 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003138 Params.clear();
3139 Params.push_back(ObjectPtrTy);
3140 Params.push_back(SelectorPtrTy);
3141 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003142 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003143 MessageSendFpretFn =
3144 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3145 Params,
3146 true),
3147 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003148
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003149 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003150 Params.clear();
3151 Params.push_back(SuperPtrTy);
3152 Params.push_back(SelectorPtrTy);
3153 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003154 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3155 Params,
3156 true),
3157 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003158
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003159 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3160 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003161 Params.clear();
3162 Params.push_back(Int8PtrTy);
3163 Params.push_back(SuperPtrTy);
3164 Params.push_back(SelectorPtrTy);
3165 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003166 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3167 Params,
3168 true),
3169 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003170
3171 // There is no objc_msgSendSuper_fpret? How can that work?
3172 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003173
Anders Carlsson124526b2008-09-09 10:10:21 +00003174 // FIXME: This is the size of the setjmp buffer and should be
3175 // target specific. 18 is what's used on 32-bit X86.
3176 uint64_t SetJmpBufferSize = 18;
3177
3178 // Exceptions
3179 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003180 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003181
3182 ExceptionDataTy =
3183 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3184 SetJmpBufferSize),
3185 StackPtrTy, NULL);
3186 CGM.getModule().addTypeName("struct._objc_exception_data",
3187 ExceptionDataTy);
3188
3189 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003190 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3191 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003192 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3193 Params,
3194 false),
3195 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003196 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003197 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3198 Params,
3199 false),
3200 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003201 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003202 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3203 Params,
3204 false),
3205 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003206
3207 Params.clear();
3208 Params.push_back(ClassPtrTy);
3209 Params.push_back(ObjectPtrTy);
3210 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003211 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3212 Params,
3213 false),
3214 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003215
Anders Carlsson124526b2008-09-09 10:10:21 +00003216 Params.clear();
3217 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3218 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003219 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3220 Params,
3221 false),
3222 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003223
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003224}
3225
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003226ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003227: ObjCCommonTypesHelper(cgm)
3228{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003229 // struct _method_list_t {
3230 // uint32_t entsize; // sizeof(struct _objc_method)
3231 // uint32_t method_count;
3232 // struct _objc_method method_list[method_count];
3233 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003234 MethodListnfABITy = llvm::StructType::get(IntTy,
3235 IntTy,
3236 llvm::ArrayType::get(MethodTy, 0),
3237 NULL);
3238 CGM.getModule().addTypeName("struct.__method_list_t",
3239 MethodListnfABITy);
3240 // struct method_list_t *
3241 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003242
3243 // struct _protocol_t {
3244 // id isa; // NULL
3245 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003246 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003247 // const struct method_list_t * const instance_methods;
3248 // const struct method_list_t * const class_methods;
3249 // const struct method_list_t *optionalInstanceMethods;
3250 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003251 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003252 // const uint32_t size; // sizeof(struct _protocol_t)
3253 // const uint32_t flags; // = 0
3254 // }
3255
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003256 // Holder for struct _protocol_list_t *
3257 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3258
3259 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3260 Int8PtrTy,
3261 llvm::PointerType::getUnqual(
3262 ProtocolListTyHolder),
3263 MethodListnfABIPtrTy,
3264 MethodListnfABIPtrTy,
3265 MethodListnfABIPtrTy,
3266 MethodListnfABIPtrTy,
3267 PropertyListPtrTy,
3268 IntTy,
3269 IntTy,
3270 NULL);
3271 CGM.getModule().addTypeName("struct._protocol_t",
3272 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003273
3274 // struct _protocol_t*
3275 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003276
Fariborz Jahanianda320092009-01-29 19:24:30 +00003277 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003278 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003279 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003280 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003281 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3282 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003283 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003284 NULL);
3285 CGM.getModule().addTypeName("struct._objc_protocol_list",
3286 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003287 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3288 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003289
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003290 // struct _objc_protocol_list*
3291 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003292
3293 // struct _ivar_t {
3294 // unsigned long int *offset; // pointer to ivar offset location
3295 // char *name;
3296 // char *type;
3297 // uint32_t alignment;
3298 // uint32_t size;
3299 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003300 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3301 Int8PtrTy,
3302 Int8PtrTy,
3303 IntTy,
3304 IntTy,
3305 NULL);
3306 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3307
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003308 // struct _ivar_list_t {
3309 // uint32 entsize; // sizeof(struct _ivar_t)
3310 // uint32 count;
3311 // struct _iver_t list[count];
3312 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003313 IvarListnfABITy = llvm::StructType::get(IntTy,
3314 IntTy,
3315 llvm::ArrayType::get(
3316 IvarnfABITy, 0),
3317 NULL);
3318 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3319
3320 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003321
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003322 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003323 // uint32_t const flags;
3324 // uint32_t const instanceStart;
3325 // uint32_t const instanceSize;
3326 // uint32_t const reserved; // only when building for 64bit targets
3327 // const uint8_t * const ivarLayout;
3328 // const char *const name;
3329 // const struct _method_list_t * const baseMethods;
3330 // const struct _objc_protocol_list *const baseProtocols;
3331 // const struct _ivar_list_t *const ivars;
3332 // const uint8_t * const weakIvarLayout;
3333 // const struct _prop_list_t * const properties;
3334 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003335
3336 // FIXME. Add 'reserved' field in 64bit abi mode!
3337 ClassRonfABITy = llvm::StructType::get(IntTy,
3338 IntTy,
3339 IntTy,
3340 Int8PtrTy,
3341 Int8PtrTy,
3342 MethodListnfABIPtrTy,
3343 ProtocolListnfABIPtrTy,
3344 IvarListnfABIPtrTy,
3345 Int8PtrTy,
3346 PropertyListPtrTy,
3347 NULL);
3348 CGM.getModule().addTypeName("struct._class_ro_t",
3349 ClassRonfABITy);
3350
3351 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3352 std::vector<const llvm::Type*> Params;
3353 Params.push_back(ObjectPtrTy);
3354 Params.push_back(SelectorPtrTy);
3355 ImpnfABITy = llvm::PointerType::getUnqual(
3356 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3357
3358 // struct _class_t {
3359 // struct _class_t *isa;
3360 // struct _class_t * const superclass;
3361 // void *cache;
3362 // IMP *vtable;
3363 // struct class_ro_t *ro;
3364 // }
3365
3366 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3367 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3368 llvm::PointerType::getUnqual(ClassTyHolder),
3369 CachePtrTy,
3370 llvm::PointerType::getUnqual(ImpnfABITy),
3371 llvm::PointerType::getUnqual(
3372 ClassRonfABITy),
3373 NULL);
3374 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3375
3376 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3377 ClassnfABITy);
3378
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003379 // LLVM for struct _class_t *
3380 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3381
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003382 // struct _category_t {
3383 // const char * const name;
3384 // struct _class_t *const cls;
3385 // const struct _method_list_t * const instance_methods;
3386 // const struct _method_list_t * const class_methods;
3387 // const struct _protocol_list_t * const protocols;
3388 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003389 // }
3390 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003391 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003392 MethodListnfABIPtrTy,
3393 MethodListnfABIPtrTy,
3394 ProtocolListnfABIPtrTy,
3395 PropertyListPtrTy,
3396 NULL);
3397 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003398
3399 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003400 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3401 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003402
3403 // MessageRefTy - LLVM for:
3404 // struct _message_ref_t {
3405 // IMP messenger;
3406 // SEL name;
3407 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003408
3409 // First the clang type for struct _message_ref_t
3410 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3411 SourceLocation(),
3412 &Ctx.Idents.get("_message_ref_t"));
3413 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3414 Ctx.VoidPtrTy, 0, false));
3415 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3416 Ctx.getObjCSelType(), 0, false));
3417 RD->completeDefinition(Ctx);
3418
3419 MessageRefCTy = Ctx.getTagDeclType(RD);
3420 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3421 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003422
3423 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3424 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3425
3426 // SuperMessageRefTy - LLVM for:
3427 // struct _super_message_ref_t {
3428 // SUPER_IMP messenger;
3429 // SEL name;
3430 // };
3431 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3432 SelectorPtrTy,
3433 NULL);
3434 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3435
3436 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3437 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3438
3439 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3440 Params.clear();
3441 Params.push_back(ObjectPtrTy);
3442 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003443 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3444 Params,
3445 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003446 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003447 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003448 "objc_msgSend_fixup");
3449
3450 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3451 MessageSendFpretFixupFn =
3452 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3453 Params,
3454 true),
3455 "objc_msgSend_fpret_fixup");
3456
3457 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3458 MessageSendStretFixupFn =
3459 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3460 Params,
3461 true),
3462 "objc_msgSend_stret_fixup");
3463
3464 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3465 MessageSendIdFixupFn =
3466 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3467 Params,
3468 true),
3469 "objc_msgSendId_fixup");
3470
3471
3472 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3473 MessageSendIdStretFixupFn =
3474 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3475 Params,
3476 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003477 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003478
3479 // id objc_msgSendSuper2_fixup (struct objc_super *,
3480 // struct _super_message_ref_t*, ...)
3481 Params.clear();
3482 Params.push_back(SuperPtrTy);
3483 Params.push_back(SuperMessageRefPtrTy);
3484 MessageSendSuper2FixupFn =
3485 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3486 Params,
3487 true),
3488 "objc_msgSendSuper2_fixup");
3489
3490
3491 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3492 // struct _super_message_ref_t*, ...)
3493 MessageSendSuper2StretFixupFn =
3494 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3495 Params,
3496 true),
3497 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003498
3499 Params.clear();
3500 llvm::Constant *Personality =
3501 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3502 Params,
3503 true),
3504 "__objc_personality_v0");
3505 EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
3506
3507 Params.clear();
3508 Params.push_back(Int8PtrTy);
3509 UnwindResumeOrRethrowFn =
3510 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3511 Params,
3512 false),
3513 "_Unwind_Resume_or_Rethrow");
Daniel Dunbare588b992009-03-01 04:46:24 +00003514 ObjCBeginCatchFn =
3515 CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
3516 Params,
3517 false),
3518 "objc_begin_catch");
3519
3520 Params.clear();
3521 ObjCEndCatchFn =
3522 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3523 Params,
3524 false),
3525 "objc_end_catch");
3526
3527 // struct objc_typeinfo {
3528 // const void** vtable; // objc_ehtype_vtable + 2
3529 // const char* name; // c++ typeinfo string
3530 // Class cls;
3531 // };
3532 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3533 Int8PtrTy,
3534 ClassnfABIPtrTy,
3535 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003536 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003537 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003538}
3539
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003540llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3541 FinishNonFragileABIModule();
3542
3543 return NULL;
3544}
3545
3546void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3547 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003548
3549 // Build list of all implemented classe addresses in array
3550 // L_OBJC_LABEL_CLASS_$.
3551 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3552 // list of 'nonlazy' implementations (defined as those with a +load{}
3553 // method!!).
3554 unsigned NumClasses = DefinedClasses.size();
3555 if (NumClasses) {
3556 std::vector<llvm::Constant*> Symbols(NumClasses);
3557 for (unsigned i=0; i<NumClasses; i++)
3558 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3559 ObjCTypes.Int8PtrTy);
3560 llvm::Constant* Init =
3561 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3562 NumClasses),
3563 Symbols);
3564
3565 llvm::GlobalVariable *GV =
3566 new llvm::GlobalVariable(Init->getType(), false,
3567 llvm::GlobalValue::InternalLinkage,
3568 Init,
3569 "\01L_OBJC_LABEL_CLASS_$",
3570 &CGM.getModule());
Fariborz Jahanian5a1edf62009-02-28 00:54:00 +00003571 GV->setAlignment(
3572 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003573 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3574 UsedGlobals.push_back(GV);
3575 }
3576
3577 // Build list of all implemented category addresses in array
3578 // L_OBJC_LABEL_CATEGORY_$.
3579 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3580 // list of 'nonlazy' category implementations (defined as those with a +load{}
3581 // method!!).
3582 unsigned NumCategory = DefinedCategories.size();
3583 if (NumCategory) {
3584 std::vector<llvm::Constant*> Symbols(NumCategory);
3585 for (unsigned i=0; i<NumCategory; i++)
3586 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3587 ObjCTypes.Int8PtrTy);
3588 llvm::Constant* Init =
3589 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3590 NumCategory),
3591 Symbols);
3592
3593 llvm::GlobalVariable *GV =
3594 new llvm::GlobalVariable(Init->getType(), false,
3595 llvm::GlobalValue::InternalLinkage,
3596 Init,
3597 "\01L_OBJC_LABEL_CATEGORY_$",
3598 &CGM.getModule());
3599 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3600 UsedGlobals.push_back(GV);
3601 }
3602
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003603 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3604 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3605 std::vector<llvm::Constant*> Values(2);
3606 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003607 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00003608 // FIXME: Fix and continue?
3609 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3610 flags |= eImageInfo_GarbageCollected;
3611 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3612 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003613 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003614 llvm::Constant* Init = llvm::ConstantArray::get(
3615 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3616 Values);
3617 llvm::GlobalVariable *IMGV =
3618 new llvm::GlobalVariable(Init->getType(), false,
3619 llvm::GlobalValue::InternalLinkage,
3620 Init,
3621 "\01L_OBJC_IMAGE_INFO",
3622 &CGM.getModule());
3623 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3624 UsedGlobals.push_back(IMGV);
3625
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003626 std::vector<llvm::Constant*> Used;
3627 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3628 e = UsedGlobals.end(); i != e; ++i) {
3629 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3630 }
3631
3632 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3633 llvm::GlobalValue *GV =
3634 new llvm::GlobalVariable(AT, false,
3635 llvm::GlobalValue::AppendingLinkage,
3636 llvm::ConstantArray::get(AT, Used),
3637 "llvm.used",
3638 &CGM.getModule());
3639
3640 GV->setSection("llvm.metadata");
3641
3642}
3643
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003644// Metadata flags
3645enum MetaDataDlags {
3646 CLS = 0x0,
3647 CLS_META = 0x1,
3648 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003649 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003650 CLS_EXCEPTION = 0x20
3651};
3652/// BuildClassRoTInitializer - generate meta-data for:
3653/// struct _class_ro_t {
3654/// uint32_t const flags;
3655/// uint32_t const instanceStart;
3656/// uint32_t const instanceSize;
3657/// uint32_t const reserved; // only when building for 64bit targets
3658/// const uint8_t * const ivarLayout;
3659/// const char *const name;
3660/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003661/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003662/// const struct _ivar_list_t *const ivars;
3663/// const uint8_t * const weakIvarLayout;
3664/// const struct _prop_list_t * const properties;
3665/// }
3666///
3667llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3668 unsigned flags,
3669 unsigned InstanceStart,
3670 unsigned InstanceSize,
3671 const ObjCImplementationDecl *ID) {
3672 std::string ClassName = ID->getNameAsString();
3673 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3674 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3675 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3676 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3677 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003678 // FIXME. ivarLayout is currently null!
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003679 Values[ 3] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003680 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003681 // const struct _method_list_t * const baseMethods;
3682 std::vector<llvm::Constant*> Methods;
3683 std::string MethodListName("\01l_OBJC_$_");
3684 if (flags & CLS_META) {
3685 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3686 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3687 e = ID->classmeth_end(); i != e; ++i) {
3688 // Class methods should always be defined.
3689 Methods.push_back(GetMethodConstant(*i));
3690 }
3691 } else {
3692 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3693 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3694 e = ID->instmeth_end(); i != e; ++i) {
3695 // Instance methods should always be defined.
3696 Methods.push_back(GetMethodConstant(*i));
3697 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003698 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3699 e = ID->propimpl_end(); i != e; ++i) {
3700 ObjCPropertyImplDecl *PID = *i;
3701
3702 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3703 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3704
3705 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3706 if (llvm::Constant *C = GetMethodConstant(MD))
3707 Methods.push_back(C);
3708 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3709 if (llvm::Constant *C = GetMethodConstant(MD))
3710 Methods.push_back(C);
3711 }
3712 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003713 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003714 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003715 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003716
3717 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3718 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3719 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3720 + OID->getNameAsString(),
3721 OID->protocol_begin(),
3722 OID->protocol_end());
3723
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003724 if (flags & CLS_META)
3725 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3726 else
3727 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003728 // FIXME. weakIvarLayout is currently null.
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003729 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003730 if (flags & CLS_META)
3731 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3732 else
3733 Values[ 9] =
3734 EmitPropertyList(
3735 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3736 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003737 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3738 Values);
3739 llvm::GlobalVariable *CLASS_RO_GV =
3740 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3741 llvm::GlobalValue::InternalLinkage,
3742 Init,
3743 (flags & CLS_META) ?
3744 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3745 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3746 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003747 CLASS_RO_GV->setAlignment(
3748 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003749 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003750 UsedGlobals.push_back(CLASS_RO_GV);
3751 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003752
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003753}
3754
3755/// BuildClassMetaData - This routine defines that to-level meta-data
3756/// for the given ClassName for:
3757/// struct _class_t {
3758/// struct _class_t *isa;
3759/// struct _class_t * const superclass;
3760/// void *cache;
3761/// IMP *vtable;
3762/// struct class_ro_t *ro;
3763/// }
3764///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003765llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3766 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003767 llvm::Constant *IsAGV,
3768 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003769 llvm::Constant *ClassRoGV,
3770 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003771 std::vector<llvm::Constant*> Values(5);
3772 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003773 Values[1] = SuperClassGV
3774 ? SuperClassGV
3775 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003776 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3777 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3778 Values[4] = ClassRoGV; // &CLASS_RO_GV
3779 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3780 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003781 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
3782 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003783 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003784 GV->setAlignment(
3785 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003786 if (HiddenVisibility)
3787 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003788 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003789 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003790}
3791
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003792void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3793 std::string ClassName = ID->getNameAsString();
3794 if (!ObjCEmptyCacheVar) {
3795 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003796 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003797 false,
3798 llvm::GlobalValue::ExternalLinkage,
3799 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003800 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003801 &CGM.getModule());
3802 UsedGlobals.push_back(ObjCEmptyCacheVar);
3803
3804 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003805 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003806 false,
3807 llvm::GlobalValue::ExternalLinkage,
3808 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003809 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003810 &CGM.getModule());
3811 UsedGlobals.push_back(ObjCEmptyVtableVar);
3812 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003813 assert(ID->getClassInterface() &&
3814 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003815 uint32_t InstanceStart =
3816 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3817 uint32_t InstanceSize = InstanceStart;
3818 uint32_t flags = CLS_META;
3819 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3820 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003821
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003822 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003823
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003824 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3825 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003826 flags |= OBJC2_CLS_HIDDEN;
3827 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003828 // class is root
3829 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003830 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
3831 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003832 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003833 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00003834 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3835 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3836 Root = Super;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003837 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00003838 // work on super class metadata symbol.
3839 std::string SuperClassName =
3840 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003841 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003842 }
3843 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3844 InstanceStart,
3845 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003846 std::string TClassName = ObjCMetaClassName + ClassName;
3847 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003848 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3849 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003850
3851 // Metadata for the class
3852 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003853 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003854 flags |= OBJC2_CLS_HIDDEN;
3855 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003856 flags |= CLS_ROOT;
3857 SuperClassGV = 0;
3858 }
3859 else {
3860 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00003861 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003862 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003863 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003864 }
3865
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003866 InstanceStart = InstanceSize = 0;
3867 if (ObjCInterfaceDecl *OID =
3868 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3869 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003870 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003871 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003872 const llvm::StructLayout *Layout =
3873 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003874
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003875 RecordDecl::field_iterator firstField, lastField;
3876 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003877
3878 for (RecordDecl::field_iterator e = RD->field_end(),
3879 ifield = firstField; ifield != e; ++ifield)
3880 lastField = ifield;
3881
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003882 if (lastField != RD->field_end()) {
3883 FieldDecl *Field = *lastField;
3884 const llvm::Type *FieldTy =
3885 CGM.getTypes().ConvertTypeForMem(Field->getType());
3886 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00003887 InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003888 if (firstField == RD->field_end())
3889 InstanceStart = InstanceSize;
Fariborz Jahanian11894a42009-03-07 19:43:20 +00003890 else {
3891 Field = *firstField;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00003892 InstanceStart = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian11894a42009-03-07 19:43:20 +00003893 }
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003894 }
3895 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003896 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003897 InstanceStart,
3898 InstanceSize,
3899 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003900
3901 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003902 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003903 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3904 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003905 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003906}
3907
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003908/// GenerateProtocolRef - This routine is called to generate code for
3909/// a protocol reference expression; as in:
3910/// @code
3911/// @protocol(Proto1);
3912/// @endcode
3913/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3914/// which will hold address of the protocol meta-data.
3915///
3916llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3917 const ObjCProtocolDecl *PD) {
3918
3919 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3920 ObjCTypes.ExternalProtocolPtrTy);
3921
3922 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3923 ProtocolName += PD->getNameAsCString();
3924
3925 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3926 if (PTGV)
3927 return Builder.CreateLoad(PTGV, false, "tmp");
3928 PTGV = new llvm::GlobalVariable(
3929 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00003930 // FIXME: review, was WeakLinkage,
3931 // also review all other WeakLinkage changes
3932 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003933 Init,
3934 ProtocolName,
3935 &CGM.getModule());
3936 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3937 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3938 UsedGlobals.push_back(PTGV);
3939 return Builder.CreateLoad(PTGV, false, "tmp");
3940}
3941
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003942/// GenerateCategory - Build metadata for a category implementation.
3943/// struct _category_t {
3944/// const char * const name;
3945/// struct _class_t *const cls;
3946/// const struct _method_list_t * const instance_methods;
3947/// const struct _method_list_t * const class_methods;
3948/// const struct _protocol_list_t * const protocols;
3949/// const struct _prop_list_t * const properties;
3950/// }
3951///
3952void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3953{
3954 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003955 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3956 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003957 "_$_" + OCD->getNameAsString());
3958 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3959
3960 std::vector<llvm::Constant*> Values(6);
3961 Values[0] = GetClassName(OCD->getIdentifier());
3962 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00003963 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003964 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003965 std::vector<llvm::Constant*> Methods;
3966 std::string MethodListName(Prefix);
3967 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3968 "_$_" + OCD->getNameAsString();
3969
3970 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3971 e = OCD->instmeth_end(); i != e; ++i) {
3972 // Instance methods should always be defined.
3973 Methods.push_back(GetMethodConstant(*i));
3974 }
3975
3976 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003977 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003978 Methods);
3979
3980 MethodListName = Prefix;
3981 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3982 OCD->getNameAsString();
3983 Methods.clear();
3984 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3985 e = OCD->classmeth_end(); i != e; ++i) {
3986 // Class methods should always be defined.
3987 Methods.push_back(GetMethodConstant(*i));
3988 }
3989
3990 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003991 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003992 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003993 const ObjCCategoryDecl *Category =
3994 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003995 if (Category) {
3996 std::string ExtName(Interface->getNameAsString() + "_$_" +
3997 OCD->getNameAsString());
3998 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3999 + Interface->getNameAsString() + "_$_"
4000 + Category->getNameAsString(),
4001 Category->protocol_begin(),
4002 Category->protocol_end());
4003 Values[5] =
4004 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4005 OCD, Category, ObjCTypes);
4006 }
4007 else {
4008 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4009 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4010 }
4011
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004012 llvm::Constant *Init =
4013 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4014 Values);
4015 llvm::GlobalVariable *GCATV
4016 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4017 false,
4018 llvm::GlobalValue::InternalLinkage,
4019 Init,
4020 ExtCatName,
4021 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004022 GCATV->setAlignment(
4023 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004024 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004025 UsedGlobals.push_back(GCATV);
4026 DefinedCategories.push_back(GCATV);
4027}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004028
4029/// GetMethodConstant - Return a struct objc_method constant for the
4030/// given method if it has been defined. The result is null if the
4031/// method has not been defined. The return value has type MethodPtrTy.
4032llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4033 const ObjCMethodDecl *MD) {
4034 // FIXME: Use DenseMap::lookup
4035 llvm::Function *Fn = MethodDefinitions[MD];
4036 if (!Fn)
4037 return 0;
4038
4039 std::vector<llvm::Constant*> Method(3);
4040 Method[0] =
4041 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4042 ObjCTypes.SelectorPtrTy);
4043 Method[1] = GetMethodVarType(MD);
4044 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4045 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4046}
4047
4048/// EmitMethodList - Build meta-data for method declarations
4049/// struct _method_list_t {
4050/// uint32_t entsize; // sizeof(struct _objc_method)
4051/// uint32_t method_count;
4052/// struct _objc_method method_list[method_count];
4053/// }
4054///
4055llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4056 const std::string &Name,
4057 const char *Section,
4058 const ConstantVector &Methods) {
4059 // Return null for empty list.
4060 if (Methods.empty())
4061 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4062
4063 std::vector<llvm::Constant*> Values(3);
4064 // sizeof(struct _objc_method)
4065 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4066 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4067 // method_count
4068 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4069 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4070 Methods.size());
4071 Values[2] = llvm::ConstantArray::get(AT, Methods);
4072 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4073
4074 llvm::GlobalVariable *GV =
4075 new llvm::GlobalVariable(Init->getType(), false,
4076 llvm::GlobalValue::InternalLinkage,
4077 Init,
4078 Name,
4079 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004080 GV->setAlignment(
4081 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004082 GV->setSection(Section);
4083 UsedGlobals.push_back(GV);
4084 return llvm::ConstantExpr::getBitCast(GV,
4085 ObjCTypes.MethodListnfABIPtrTy);
4086}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004087
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004088/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4089/// the given ivar.
4090///
4091llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
4092 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004093 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004094 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004095 Name += "\01_OBJC_IVAR_$_" +
4096 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004097 + Ivar->getNameAsString();
4098 llvm::GlobalVariable *IvarOffsetGV =
4099 CGM.getModule().getGlobalVariable(Name);
4100 if (!IvarOffsetGV)
4101 IvarOffsetGV =
4102 new llvm::GlobalVariable(ObjCTypes.LongTy,
4103 false,
4104 llvm::GlobalValue::ExternalLinkage,
4105 0,
4106 Name,
4107 &CGM.getModule());
4108 return IvarOffsetGV;
4109}
4110
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004111llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004112 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004113 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004114 unsigned long int Offset) {
4115
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004116 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004117 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004118 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004119 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004120
4121 llvm::GlobalVariable *IvarOffsetGV =
4122 CGM.getModule().getGlobalVariable(ExternalName);
4123 if (IvarOffsetGV) {
4124 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004125 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004126 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004127 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004128 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004129 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004130 return IvarOffsetGV;
4131 }
4132
4133 IvarOffsetGV =
4134 new llvm::GlobalVariable(Init->getType(),
4135 false,
4136 llvm::GlobalValue::ExternalLinkage,
4137 Init,
4138 ExternalName,
4139 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004140 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004141 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004142 // @private and @package have hidden visibility.
4143 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4144 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4145 if (!globalVisibility)
4146 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004147 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004148 if (IsClassHidden(ID))
4149 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004150
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004151 IvarOffsetGV->setSection("__DATA, __objc_const");
4152 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004153 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004154}
4155
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004156/// EmitIvarList - Emit the ivar list for the given
4157/// implementation. If ForClass is true the list of class ivars
4158/// (i.e. metaclass ivars) is emitted, otherwise the list of
4159/// interface ivars will be emitted. The return value has type
4160/// IvarListnfABIPtrTy.
4161/// struct _ivar_t {
4162/// unsigned long int *offset; // pointer to ivar offset location
4163/// char *name;
4164/// char *type;
4165/// uint32_t alignment;
4166/// uint32_t size;
4167/// }
4168/// struct _ivar_list_t {
4169/// uint32 entsize; // sizeof(struct _ivar_t)
4170/// uint32 count;
4171/// struct _iver_t list[count];
4172/// }
4173///
4174llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4175 const ObjCImplementationDecl *ID) {
4176
4177 std::vector<llvm::Constant*> Ivars, Ivar(5);
4178
4179 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4180 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4181
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004182 // FIXME. Consolidate this with similar code in GenerateClass.
4183 const llvm::Type *InterfaceTy =
4184 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4185 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004186 const llvm::StructLayout *Layout =
4187 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004188
4189 RecordDecl::field_iterator i,p;
4190 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004191 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4192
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004193 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004194 FieldDecl *Field = *i;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004195 uint64_t offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004196 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004197 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004198 if (Field->getIdentifier())
4199 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4200 else
4201 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00004202 Ivar[2] = GetMethodVarType(Field);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004203 const llvm::Type *FieldTy =
4204 CGM.getTypes().ConvertTypeForMem(Field->getType());
4205 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4206 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4207 Field->getType().getTypePtr()) >> 3;
4208 Align = llvm::Log2_32(Align);
4209 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004210 // NOTE. Size of a bitfield does not match gcc's, because of the way
4211 // bitfields are treated special in each. But I am told that 'size'
4212 // for bitfield ivars is ignored by the runtime so it does not matter.
4213 // (even if it matters, some day, there is enough info. to get the bitfield
4214 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004215 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4216 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4217 }
4218 // Return null for empty list.
4219 if (Ivars.empty())
4220 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4221 std::vector<llvm::Constant*> Values(3);
4222 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4223 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4224 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4225 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4226 Ivars.size());
4227 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4228 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4229 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4230 llvm::GlobalVariable *GV =
4231 new llvm::GlobalVariable(Init->getType(), false,
4232 llvm::GlobalValue::InternalLinkage,
4233 Init,
4234 Prefix + OID->getNameAsString(),
4235 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004236 GV->setAlignment(
4237 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004238 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004239
4240 UsedGlobals.push_back(GV);
4241 return llvm::ConstantExpr::getBitCast(GV,
4242 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004243}
4244
4245llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4246 const ObjCProtocolDecl *PD) {
4247 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4248
4249 if (!Entry) {
4250 // We use the initializer as a marker of whether this is a forward
4251 // reference or not. At module finalization we add the empty
4252 // contents for protocols which were referenced but never defined.
4253 Entry =
4254 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4255 llvm::GlobalValue::ExternalLinkage,
4256 0,
4257 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4258 &CGM.getModule());
4259 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4260 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004261 }
4262
4263 return Entry;
4264}
4265
4266/// GetOrEmitProtocol - Generate the protocol meta-data:
4267/// @code
4268/// struct _protocol_t {
4269/// id isa; // NULL
4270/// const char * const protocol_name;
4271/// const struct _protocol_list_t * protocol_list; // super protocols
4272/// const struct method_list_t * const instance_methods;
4273/// const struct method_list_t * const class_methods;
4274/// const struct method_list_t *optionalInstanceMethods;
4275/// const struct method_list_t *optionalClassMethods;
4276/// const struct _prop_list_t * properties;
4277/// const uint32_t size; // sizeof(struct _protocol_t)
4278/// const uint32_t flags; // = 0
4279/// }
4280/// @endcode
4281///
4282
4283llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4284 const ObjCProtocolDecl *PD) {
4285 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4286
4287 // Early exit if a defining object has already been generated.
4288 if (Entry && Entry->hasInitializer())
4289 return Entry;
4290
4291 const char *ProtocolName = PD->getNameAsCString();
4292
4293 // Construct method lists.
4294 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4295 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4296 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4297 e = PD->instmeth_end(); i != e; ++i) {
4298 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004299 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004300 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4301 OptInstanceMethods.push_back(C);
4302 } else {
4303 InstanceMethods.push_back(C);
4304 }
4305 }
4306
4307 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4308 e = PD->classmeth_end(); i != e; ++i) {
4309 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004310 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004311 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4312 OptClassMethods.push_back(C);
4313 } else {
4314 ClassMethods.push_back(C);
4315 }
4316 }
4317
4318 std::vector<llvm::Constant*> Values(10);
4319 // isa is NULL
4320 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4321 Values[1] = GetClassName(PD->getIdentifier());
4322 Values[2] = EmitProtocolList(
4323 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4324 PD->protocol_begin(),
4325 PD->protocol_end());
4326
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004327 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004328 + PD->getNameAsString(),
4329 "__DATA, __objc_const",
4330 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004331 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004332 + PD->getNameAsString(),
4333 "__DATA, __objc_const",
4334 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004335 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004336 + PD->getNameAsString(),
4337 "__DATA, __objc_const",
4338 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004339 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004340 + PD->getNameAsString(),
4341 "__DATA, __objc_const",
4342 OptClassMethods);
4343 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4344 0, PD, ObjCTypes);
4345 uint32_t Size =
4346 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4347 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4348 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4349 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4350 Values);
4351
4352 if (Entry) {
4353 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004354 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004355 Entry->setInitializer(Init);
4356 } else {
4357 Entry =
4358 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004359 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004360 Init,
4361 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4362 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004363 Entry->setAlignment(
4364 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004365 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004366 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004367 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4368
4369 // Use this protocol meta-data to build protocol list table in section
4370 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004371 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004372 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004373 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004374 Entry,
4375 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4376 +ProtocolName,
4377 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004378 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004379 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004380 PTGV->setSection("__DATA, __objc_protolist");
4381 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4382 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004383 return Entry;
4384}
4385
4386/// EmitProtocolList - Generate protocol list meta-data:
4387/// @code
4388/// struct _protocol_list_t {
4389/// long protocol_count; // Note, this is 32/64 bit
4390/// struct _protocol_t[protocol_count];
4391/// }
4392/// @endcode
4393///
4394llvm::Constant *
4395CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4396 ObjCProtocolDecl::protocol_iterator begin,
4397 ObjCProtocolDecl::protocol_iterator end) {
4398 std::vector<llvm::Constant*> ProtocolRefs;
4399
Fariborz Jahanianda320092009-01-29 19:24:30 +00004400 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004401 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004402 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4403
Daniel Dunbar948e2582009-02-15 07:36:20 +00004404 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004405 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4406 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004407 return llvm::ConstantExpr::getBitCast(GV,
4408 ObjCTypes.ProtocolListnfABIPtrTy);
4409
4410 for (; begin != end; ++begin)
4411 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4412
Fariborz Jahanianda320092009-01-29 19:24:30 +00004413 // This list is null terminated.
4414 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004415 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004416
4417 std::vector<llvm::Constant*> Values(2);
4418 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4419 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004420 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004421 ProtocolRefs.size()),
4422 ProtocolRefs);
4423
4424 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4425 GV = new llvm::GlobalVariable(Init->getType(), false,
4426 llvm::GlobalValue::InternalLinkage,
4427 Init,
4428 Name,
4429 &CGM.getModule());
4430 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004431 GV->setAlignment(
4432 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004433 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004434 return llvm::ConstantExpr::getBitCast(GV,
4435 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004436}
4437
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004438/// GetMethodDescriptionConstant - This routine build following meta-data:
4439/// struct _objc_method {
4440/// SEL _cmd;
4441/// char *method_type;
4442/// char *_imp;
4443/// }
4444
4445llvm::Constant *
4446CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4447 std::vector<llvm::Constant*> Desc(3);
4448 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4449 ObjCTypes.SelectorPtrTy);
4450 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004451 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004452 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4453 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4454}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004455
4456/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4457/// This code gen. amounts to generating code for:
4458/// @code
4459/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4460/// @encode
4461///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004462LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004463 CodeGen::CodeGenFunction &CGF,
4464 QualType ObjectTy,
4465 llvm::Value *BaseValue,
4466 const ObjCIvarDecl *Ivar,
4467 const FieldDecl *Field,
4468 unsigned CVRQualifiers) {
4469 assert(ObjectTy->isObjCInterfaceType() &&
4470 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004471 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004472 std::string ExternalName;
4473 llvm::GlobalVariable *IvarOffsetGV =
4474 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004475
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004476 // (char *) BaseValue
4477 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4478 ObjCTypes.Int8PtrTy);
4479 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4480 // (char*)BaseValue + Offset_symbol
4481 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4482 // (type *)((char*)BaseValue + Offset_symbol)
4483 const llvm::Type *IvarTy =
4484 CGM.getTypes().ConvertType(Ivar->getType());
4485 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4486 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004487
4488 if (Ivar->isBitField())
4489 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4490 CVRQualifiers);
4491
4492 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004493 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4494 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004495 LValue::SetObjCIvar(LV, true);
4496 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004497}
4498
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004499llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4500 CodeGen::CodeGenFunction &CGF,
4501 ObjCInterfaceDecl *Interface,
4502 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004503 std::string ExternalName;
4504 llvm::GlobalVariable *IvarOffsetGV =
4505 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4506
4507 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004508}
4509
Fariborz Jahanian46551122009-02-04 00:22:57 +00004510CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4511 CodeGen::CodeGenFunction &CGF,
4512 QualType ResultType,
4513 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004514 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004515 QualType Arg0Ty,
4516 bool IsSuper,
4517 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004518 // FIXME. Even though IsSuper is passes. This function doese not
4519 // handle calls to 'super' receivers.
4520 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004521 llvm::Value *Arg0 = Receiver;
4522 if (!IsSuper)
4523 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004524
4525 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004526 // FIXME. This is too much work to get the ABI-specific result type
4527 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004528 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4529 llvm::SmallVector<QualType, 16>());
4530 llvm::Constant *Fn;
4531 std::string Name("\01l_");
4532 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004533#if 0
4534 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004535 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4536 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4537 // FIXME. Is there a better way of getting these names.
4538 // They are available in RuntimeFunctions vector pair.
4539 Name += "objc_msgSendId_stret_fixup";
4540 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004541 else
4542#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004543 if (IsSuper) {
4544 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4545 Name += "objc_msgSendSuper2_stret_fixup";
4546 }
4547 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004548 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004549 Fn = ObjCTypes.MessageSendStretFixupFn;
4550 Name += "objc_msgSend_stret_fixup";
4551 }
4552 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004553 else if (ResultType->isFloatingType() &&
4554 // Selection of frret API only happens in 32bit nonfragile ABI.
4555 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004556 Fn = ObjCTypes.MessageSendFpretFixupFn;
4557 Name += "objc_msgSend_fpret_fixup";
4558 }
4559 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004560#if 0
4561// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004562 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4563 Fn = ObjCTypes.MessageSendIdFixupFn;
4564 Name += "objc_msgSendId_fixup";
4565 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004566 else
4567#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004568 if (IsSuper) {
4569 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4570 Name += "objc_msgSendSuper2_fixup";
4571 }
4572 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004573 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004574 Fn = ObjCTypes.MessageSendFixupFn;
4575 Name += "objc_msgSend_fixup";
4576 }
4577 }
4578 Name += '_';
4579 std::string SelName(Sel.getAsString());
4580 // Replace all ':' in selector name with '_' ouch!
4581 for(unsigned i = 0; i < SelName.size(); i++)
4582 if (SelName[i] == ':')
4583 SelName[i] = '_';
4584 Name += SelName;
4585 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4586 if (!GV) {
4587 // Build messafe ref table entry.
4588 std::vector<llvm::Constant*> Values(2);
4589 Values[0] = Fn;
4590 Values[1] = GetMethodVarName(Sel);
4591 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4592 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004593 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004594 Init,
4595 Name,
4596 &CGM.getModule());
4597 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4598 GV->setAlignment(
4599 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4600 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4601 UsedGlobals.push_back(GV);
4602 }
4603 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004604
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004605 CallArgList ActualArgs;
4606 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4607 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4608 ObjCTypes.MessageRefCPtrTy));
4609 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004610 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4611 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4612 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004613 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004614 Callee = CGF.Builder.CreateBitCast(Callee,
4615 llvm::PointerType::getUnqual(FTy));
4616 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004617}
4618
4619/// Generate code for a message send expression in the nonfragile abi.
4620CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4621 CodeGen::CodeGenFunction &CGF,
4622 QualType ResultType,
4623 Selector Sel,
4624 llvm::Value *Receiver,
4625 bool IsClassMessage,
4626 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004627 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004628 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004629 false, CallArgs);
4630}
4631
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004632llvm::GlobalVariable *
4633CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
4634 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4635
Daniel Dunbardfff2302009-03-02 05:18:14 +00004636 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004637 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4638 llvm::GlobalValue::ExternalLinkage,
4639 0, Name, &CGM.getModule());
4640 UsedGlobals.push_back(GV);
4641 }
4642
4643 return GV;
4644}
4645
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004646llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004647 const ObjCInterfaceDecl *ID,
4648 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004649
4650 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4651
4652 if (!Entry) {
4653 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004654 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004655 Entry =
4656 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4657 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004658 ClassGV,
4659 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4660 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004661 &CGM.getModule());
4662 Entry->setAlignment(
4663 CGM.getTargetData().getPrefTypeAlignment(
4664 ObjCTypes.ClassnfABIPtrTy));
4665
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004666 if (IsSuper)
Fariborz Jahanian21228b72009-02-26 22:30:39 +00004667 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004668 else
Fariborz Jahanian21228b72009-02-26 22:30:39 +00004669 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004670 UsedGlobals.push_back(Entry);
4671 }
4672
4673 return Builder.CreateLoad(Entry, false, "tmp");
4674}
4675
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004676/// EmitMetaClassRef - Return a Value * of the address of _class_t
4677/// meta-data
4678///
4679llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4680 const ObjCInterfaceDecl *ID) {
4681 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4682 if (Entry)
4683 return Builder.CreateLoad(Entry, false, "tmp");
4684
4685 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
Daniel Dunbar8def7992009-03-01 04:51:18 +00004686 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004687 Entry =
4688 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4689 llvm::GlobalValue::InternalLinkage,
4690 MetaClassGV,
4691 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4692 &CGM.getModule());
4693 Entry->setAlignment(
4694 CGM.getTargetData().getPrefTypeAlignment(
4695 ObjCTypes.ClassnfABIPtrTy));
4696
4697 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4698 UsedGlobals.push_back(Entry);
4699
4700 return Builder.CreateLoad(Entry, false, "tmp");
4701}
4702
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004703/// GetClass - Return a reference to the class for the given interface
4704/// decl.
4705llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4706 const ObjCInterfaceDecl *ID) {
4707 return EmitClassRef(Builder, ID);
4708}
4709
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004710/// Generates a message send where the super is the receiver. This is
4711/// a message send to self with special delivery semantics indicating
4712/// which class's method should be called.
4713CodeGen::RValue
4714CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4715 QualType ResultType,
4716 Selector Sel,
4717 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00004718 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004719 llvm::Value *Receiver,
4720 bool IsClassMessage,
4721 const CodeGen::CallArgList &CallArgs) {
4722 // ...
4723 // Create and init a super structure; this is a (receiver, class)
4724 // pair we will pass to objc_msgSendSuper.
4725 llvm::Value *ObjCSuper =
4726 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4727
4728 llvm::Value *ReceiverAsObject =
4729 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4730 CGF.Builder.CreateStore(ReceiverAsObject,
4731 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4732
4733 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00004734 llvm::Value *Target;
4735 if (IsClassMessage) {
4736 if (isCategoryImpl) {
4737 // Message sent to "super' in a class method defined in
4738 // a category implementation.
4739 Target = EmitClassRef(CGF.Builder, Class, false);
4740 Target = CGF.Builder.CreateStructGEP(Target, 0);
4741 Target = CGF.Builder.CreateLoad(Target);
4742 }
4743 else
4744 Target = EmitMetaClassRef(CGF.Builder, Class);
4745 }
4746 else
4747 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004748
4749 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4750 // and ObjCTypes types.
4751 const llvm::Type *ClassTy =
4752 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4753 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4754 CGF.Builder.CreateStore(Target,
4755 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4756
4757 return EmitMessageSend(CGF, ResultType, Sel,
4758 ObjCSuper, ObjCTypes.SuperPtrCTy,
4759 true, CallArgs);
4760}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004761
4762llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4763 Selector Sel) {
4764 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4765
4766 if (!Entry) {
4767 llvm::Constant *Casted =
4768 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4769 ObjCTypes.SelectorPtrTy);
4770 Entry =
4771 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4772 llvm::GlobalValue::InternalLinkage,
4773 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4774 &CGM.getModule());
4775 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4776 UsedGlobals.push_back(Entry);
4777 }
4778
4779 return Builder.CreateLoad(Entry, false, "tmp");
4780}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004781/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
4782/// objc_assign_ivar (id src, id *dst)
4783///
4784void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
4785 llvm::Value *src, llvm::Value *dst)
4786{
4787 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4788 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4789 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
4790 src, dst, "assignivar");
4791 return;
4792}
4793
4794/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4795/// objc_assign_strongCast (id src, id *dst)
4796///
4797void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
4798 CodeGen::CodeGenFunction &CGF,
4799 llvm::Value *src, llvm::Value *dst)
4800{
4801 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4802 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4803 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
4804 src, dst, "weakassign");
4805 return;
4806}
4807
4808/// EmitObjCWeakRead - Code gen for loading value of a __weak
4809/// object: objc_read_weak (id *src)
4810///
4811llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
4812 CodeGen::CodeGenFunction &CGF,
4813 llvm::Value *AddrWeakObj)
4814{
Eli Friedman8339b352009-03-07 03:57:15 +00004815 const llvm::Type* DestTy =
4816 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004817 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
4818 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
4819 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004820 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00004821 return read_weak;
4822}
4823
4824/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4825/// objc_assign_weak (id src, id *dst)
4826///
4827void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
4828 llvm::Value *src, llvm::Value *dst)
4829{
4830 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4831 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4832 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
4833 src, dst, "weakassign");
4834 return;
4835}
4836
4837/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4838/// objc_assign_global (id src, id *dst)
4839///
4840void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
4841 llvm::Value *src, llvm::Value *dst)
4842{
4843 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4844 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
4845 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
4846 src, dst, "globalassign");
4847 return;
4848}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004849
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004850void
4851CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
4852 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004853 bool isTry = isa<ObjCAtTryStmt>(S);
4854 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
4855 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004856 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004857 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004858 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004859 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
4860
4861 // For @synchronized, call objc_sync_enter(sync.expr). The
4862 // evaluation of the expression must occur before we enter the
4863 // @synchronized. We can safely avoid a temp here because jumps into
4864 // @synchronized are illegal & this will dominate uses.
4865 llvm::Value *SyncArg = 0;
4866 if (!isTry) {
4867 SyncArg =
4868 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
4869 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
4870 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
4871 }
4872
4873 // Push an EH context entry, used for handling rethrows and jumps
4874 // through finally.
4875 CGF.PushCleanupBlock(FinallyBlock);
4876
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004877 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00004878
4879 CGF.EmitBlock(TryBlock);
4880 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
4881 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
4882 CGF.EmitBranchThroughCleanup(FinallyEnd);
4883
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004884 // Emit the exception handler.
4885
4886 CGF.EmitBlock(TryHandler);
4887
4888 llvm::Value *llvm_eh_exception =
4889 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
4890 llvm::Value *llvm_eh_selector_i64 =
4891 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
4892 llvm::Value *llvm_eh_typeid_for_i64 =
4893 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
4894 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
4895 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
4896
4897 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
4898 SelectorArgs.push_back(Exc);
4899 SelectorArgs.push_back(ObjCTypes.EHPersonalityPtr);
4900
4901 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00004902 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004903 bool HasCatchAll = false;
4904 if (isTry) {
4905 if (const ObjCAtCatchStmt* CatchStmt =
4906 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
4907 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00004908 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00004909 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004910
4911 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00004912 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004913 // Use i8* null here to signal this is a catch all, not a cleanup.
4914 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4915 SelectorArgs.push_back(Null);
4916 HasCatchAll = true;
4917 break;
4918 }
4919
Daniel Dunbarede8de92009-03-06 00:01:21 +00004920 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
4921 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004922 llvm::Value *IDEHType =
4923 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
4924 if (!IDEHType)
4925 IDEHType =
4926 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
4927 llvm::GlobalValue::ExternalLinkage,
4928 0, "OBJC_EHTYPE_id", &CGM.getModule());
4929 SelectorArgs.push_back(IDEHType);
4930 HasCatchAll = true;
4931 break;
4932 }
4933
4934 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00004935 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004936 assert(PT && "Invalid @catch type.");
4937 const ObjCInterfaceType *IT =
4938 PT->getPointeeType()->getAsObjCInterfaceType();
4939 assert(IT && "Invalid @catch type.");
4940 llvm::Value *EHType = GetInterfaceEHType(IT);
4941 SelectorArgs.push_back(EHType);
4942 }
4943 }
4944 }
4945
4946 // We use a cleanup unless there was already a catch all.
4947 if (!HasCatchAll) {
4948 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00004949 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004950 }
4951
4952 llvm::Value *Selector =
4953 CGF.Builder.CreateCall(llvm_eh_selector_i64,
4954 SelectorArgs.begin(), SelectorArgs.end(),
4955 "selector");
4956 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00004957 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004958 const Stmt *CatchBody = Handlers[i].second;
4959
4960 llvm::BasicBlock *Next = 0;
4961
4962 // The last handler always matches.
4963 if (i + 1 != e) {
4964 assert(CatchParam && "Only last handler can be a catch all.");
4965
4966 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
4967 Next = CGF.createBasicBlock("catch.next");
4968 llvm::Value *Id =
4969 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
4970 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
4971 ObjCTypes.Int8PtrTy));
4972 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
4973 Match, Next);
4974
4975 CGF.EmitBlock(Match);
4976 }
4977
4978 if (CatchBody) {
4979 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
4980 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
4981
4982 // Cleanups must call objc_end_catch.
4983 //
4984 // FIXME: It seems incorrect for objc_begin_catch to be inside
4985 // this context, but this matches gcc.
4986 CGF.PushCleanupBlock(MatchEnd);
4987 CGF.setInvokeDest(MatchHandler);
4988
4989 llvm::Value *ExcObject =
4990 CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc);
4991
4992 // Bind the catch parameter if it exists.
4993 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00004994 ExcObject =
4995 CGF.Builder.CreateBitCast(ExcObject,
4996 CGF.ConvertType(CatchParam->getType()));
4997 // CatchParam is a ParmVarDecl because of the grammar
4998 // construction used to handle this, but for codegen purposes
4999 // we treat this as a local decl.
5000 CGF.EmitLocalBlockVarDecl(*CatchParam);
5001 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005002 }
5003
5004 CGF.ObjCEHValueStack.push_back(ExcObject);
5005 CGF.EmitStmt(CatchBody);
5006 CGF.ObjCEHValueStack.pop_back();
5007
5008 CGF.EmitBranchThroughCleanup(FinallyEnd);
5009
5010 CGF.EmitBlock(MatchHandler);
5011
5012 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5013 // We are required to emit this call to satisfy LLVM, even
5014 // though we don't use the result.
5015 llvm::SmallVector<llvm::Value*, 8> Args;
5016 Args.push_back(Exc);
5017 Args.push_back(ObjCTypes.EHPersonalityPtr);
5018 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5019 0));
5020 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5021 CGF.Builder.CreateStore(Exc, RethrowPtr);
5022 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5023
5024 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5025
5026 CGF.EmitBlock(MatchEnd);
5027
5028 // Unfortunately, we also have to generate another EH frame here
5029 // in case this throws.
5030 llvm::BasicBlock *MatchEndHandler =
5031 CGF.createBasicBlock("match.end.handler");
5032 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5033 CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn,
5034 Cont, MatchEndHandler,
5035 Args.begin(), Args.begin());
5036
5037 CGF.EmitBlock(Cont);
5038 if (Info.SwitchBlock)
5039 CGF.EmitBlock(Info.SwitchBlock);
5040 if (Info.EndBlock)
5041 CGF.EmitBlock(Info.EndBlock);
5042
5043 CGF.EmitBlock(MatchEndHandler);
5044 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5045 // We are required to emit this call to satisfy LLVM, even
5046 // though we don't use the result.
5047 Args.clear();
5048 Args.push_back(Exc);
5049 Args.push_back(ObjCTypes.EHPersonalityPtr);
5050 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5051 0));
5052 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5053 CGF.Builder.CreateStore(Exc, RethrowPtr);
5054 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5055
5056 if (Next)
5057 CGF.EmitBlock(Next);
5058 } else {
5059 assert(!Next && "catchup should be last handler.");
5060
5061 CGF.Builder.CreateStore(Exc, RethrowPtr);
5062 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5063 }
5064 }
5065
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005066 // Pop the cleanup entry, the @finally is outside this cleanup
5067 // scope.
5068 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5069 CGF.setInvokeDest(PrevLandingPad);
5070
5071 CGF.EmitBlock(FinallyBlock);
5072
5073 if (isTry) {
5074 if (const ObjCAtFinallyStmt* FinallyStmt =
5075 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5076 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5077 } else {
5078 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5079 // @synchronized.
5080 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005081 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005082
5083 if (Info.SwitchBlock)
5084 CGF.EmitBlock(Info.SwitchBlock);
5085 if (Info.EndBlock)
5086 CGF.EmitBlock(Info.EndBlock);
5087
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005088 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005089 CGF.EmitBranch(FinallyEnd);
5090
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005091 CGF.EmitBlock(FinallyRethrow);
5092 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn,
5093 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005094 CGF.Builder.CreateUnreachable();
5095
5096 CGF.EmitBlock(FinallyEnd);
5097}
5098
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005099/// EmitThrowStmt - Generate code for a throw statement.
5100void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5101 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005102 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005103 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005104 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005105 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005106 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5107 "Unexpected rethrow outside @catch block.");
5108 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005109 }
5110
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005111 llvm::Value *ExceptionAsObject =
5112 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5113 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5114 if (InvokeDest) {
5115 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5116 CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn,
5117 Cont, InvokeDest,
5118 &ExceptionAsObject, &ExceptionAsObject + 1);
5119 CGF.EmitBlock(Cont);
5120 } else
5121 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
5122 CGF.Builder.CreateUnreachable();
5123
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005124 // Clear the insertion point to indicate we are in unreachable code.
5125 CGF.Builder.ClearInsertionPoint();
5126}
Daniel Dunbare588b992009-03-01 04:46:24 +00005127
5128llvm::Value *
5129CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceType *IT) {
5130 const ObjCInterfaceDecl *ID = IT->getDecl();
5131 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
5132 if (Entry)
5133 return Entry;
5134
5135 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
5136 std::string VTableName = "objc_ehtype_vtable";
5137 llvm::GlobalVariable *VTableGV =
5138 CGM.getModule().getGlobalVariable(VTableName);
5139 if (!VTableGV)
5140 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5141 llvm::GlobalValue::ExternalLinkage,
5142 0, VTableName, &CGM.getModule());
5143
5144 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5145
5146 std::vector<llvm::Constant*> Values(3);
5147 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5148 Values[1] = GetClassName(ID->getIdentifier());
5149 Values[2] = GetClassGlobal(ClassName);
5150 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5151
5152 Entry =
5153 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00005154 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbare588b992009-03-01 04:46:24 +00005155 Init,
5156 (std::string("OBJC_EHTYPE_$_") +
5157 ID->getIdentifier()->getName()),
5158 &CGM.getModule());
5159
5160 return Entry;
5161}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005162
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005163/* *** */
5164
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005165CodeGen::CGObjCRuntime *
5166CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005167 return new CGObjCMac(CGM);
5168}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005169
5170CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005171CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005172 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005173}