blob: f8393742429fea3576751b605a54988bd306e2b2 [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:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +000044 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000045 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
Chris Lattner74391b42009-03-22 21:03:39 +000087 llvm::Constant *GetPropertyFn, *SetPropertyFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000088
Chris Lattner74391b42009-03-22 21:03:39 +000089 llvm::Constant *EnumerationMutationFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000090
91 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner74391b42009-03-22 21:03:39 +000092 llvm::Constant *GcReadWeakFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000093
94 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner74391b42009-03-22 21:03:39 +000095 llvm::Constant *GcAssignWeakFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000096
97 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner74391b42009-03-22 21:03:39 +000098 llvm::Constant *GcAssignGlobalFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +000099
100 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner74391b42009-03-22 21:03:39 +0000101 llvm::Constant *GcAssignIvarFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000102
103 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner74391b42009-03-22 21:03:39 +0000104 llvm::Constant *GcAssignStrongCastFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000105
106 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner74391b42009-03-22 21:03:39 +0000107 llvm::Constant *ExceptionThrowFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000108
Daniel Dunbar1c566672009-02-24 01:43:46 +0000109 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattner74391b42009-03-22 21:03:39 +0000110 llvm::Constant *SyncEnterFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000111
112 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000113 llvm::Constant *SyncExitFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000114
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
Chris Lattner74391b42009-03-22 21:03:39 +0000124 llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
125 llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn,
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000126 *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.
Chris Lattner74391b42009-03-22 21:03:39 +0000184 llvm::Constant *ExceptionTryEnterFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000185
186 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000187 llvm::Constant *ExceptionTryExitFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000188
189 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner74391b42009-03-22 21:03:39 +0000190 llvm::Constant *ExceptionExtractFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000191
192 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner74391b42009-03-22 21:03:39 +0000193 llvm::Constant *ExceptionMatchFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000194
195 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner74391b42009-03-22 21:03:39 +0000196 llvm::Constant *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
Chris Lattner74391b42009-03-22 21:03:39 +0000203 llvm::Constant *getSendFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000204 return IsSuper ? MessageSendSuperFn : MessageSendFn;
205 }
206
Chris Lattner74391b42009-03-22 21:03:39 +0000207 llvm::Constant *getSendStretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000208 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
209 }
210
Chris Lattner74391b42009-03-22 21:03:39 +0000211 llvm::Constant *getSendFpretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000212 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:
Chris Lattner74391b42009-03-22 21:03:39 +0000220 llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000221 *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
Chris Lattner74391b42009-03-22 21:03:39 +0000300 llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn;
Daniel Dunbare588b992009-03-01 04:46:24 +0000301
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 {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000310public:
311 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000312 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000313 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000314 unsigned int ivar_bytepos;
315 unsigned int ivar_size;
316 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
317 };
318
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000319 class SKIP_SCAN {
320 public:
321 unsigned int skip;
322 unsigned int scan;
323 SKIP_SCAN() : skip(0), scan(0) {}
324 };
325
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000326protected:
327 CodeGen::CodeGenModule &CGM;
328 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000329 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000330
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000331 // gc ivar layout bitmap calculation helper caches.
332 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
333 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
334 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000335
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000336 /// LazySymbols - Symbols to generate a lazy reference for. See
337 /// DefinedSymbols and FinishModule().
338 std::set<IdentifierInfo*> LazySymbols;
339
340 /// DefinedSymbols - External symbols which are defined by this
341 /// module. The symbols in this list and LazySymbols are used to add
342 /// special linker symbols which ensure that Objective-C modules are
343 /// linked properly.
344 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000345
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000346 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000347 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000348
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000349 /// MethodVarNames - uniqued method variable names.
350 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000351
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000352 /// MethodVarTypes - uniqued method type signatures. We have to use
353 /// a StringMap here because have no other unique reference.
354 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000355
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000356 /// MethodDefinitions - map of methods which have been defined in
357 /// this translation unit.
358 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000359
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000360 /// PropertyNames - uniqued method variable names.
361 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000362
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000363 /// ClassReferences - uniqued class references.
364 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000365
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000366 /// SelectorReferences - uniqued selector references.
367 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000368
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000369 /// Protocols - Protocols for which an objc_protocol structure has
370 /// been emitted. Forward declarations are handled by creating an
371 /// empty structure whose initializer is filled in when/if defined.
372 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000373
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000374 /// DefinedProtocols - Protocols which have actually been
375 /// defined. We should not need this, see FIXME in GenerateProtocol.
376 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000377
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000378 /// DefinedClasses - List of defined classes.
379 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000380
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000381 /// DefinedCategories - List of defined categories.
382 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000383
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000384 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000385 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000386 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000387
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000388 /// GetNameForMethod - Return a name for the given method.
389 /// \param[out] NameOut - The return value.
390 void GetNameForMethod(const ObjCMethodDecl *OMD,
391 const ObjCContainerDecl *CD,
392 std::string &NameOut);
393
394 /// GetMethodVarName - Return a unique constant for the given
395 /// selector's name. The return value has type char *.
396 llvm::Constant *GetMethodVarName(Selector Sel);
397 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
398 llvm::Constant *GetMethodVarName(const std::string &Name);
399
400 /// GetMethodVarType - Return a unique constant for the given
401 /// selector's name. The return value has type char *.
402
403 // FIXME: This is a horrible name.
404 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Devang Patel7794bb82009-03-04 18:21:39 +0000405 llvm::Constant *GetMethodVarType(FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000406
407 /// GetPropertyName - Return a unique constant for the given
408 /// name. The return value has type char *.
409 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
410
411 // FIXME: This can be dropped once string functions are unified.
412 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
413 const Decl *Container);
414
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000415 /// GetClassName - Return a unique constant for the given selector's
416 /// name. The return value has type char *.
417 llvm::Constant *GetClassName(IdentifierInfo *Ident);
418
Fariborz Jahanian21e6f172009-03-11 21:42:00 +0000419 /// GetInterfaceDeclStructLayout - Get layout for ivars of given
420 /// interface declaration.
421 const llvm::StructLayout *GetInterfaceDeclStructLayout(
422 const ObjCInterfaceDecl *ID) const;
423
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000424 /// BuildIvarLayout - Builds ivar layout bitmap for the class
425 /// implementation for the __strong or __weak case.
426 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000427 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
428 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000429
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000430 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
431 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000432 const RecordDecl *RD,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000433 const std::vector<FieldDecl*>& RecFields,
434 unsigned int BytePos, bool ForStrongLayout,
435 int &Index, int &SkIndex, bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000436
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000437 /// GetIvarLayoutName - Returns a unique constant for the given
438 /// ivar layout bitmap.
439 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
440 const ObjCCommonTypesHelper &ObjCTypes);
441
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000442 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
443 RecordDecl::field_iterator &FIV,
444 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000445 /// EmitPropertyList - Emit the given property list. The return
446 /// value has type PropertyListPtrTy.
447 llvm::Constant *EmitPropertyList(const std::string &Name,
448 const Decl *Container,
449 const ObjCContainerDecl *OCD,
450 const ObjCCommonTypesHelper &ObjCTypes);
451
Fariborz Jahanianda320092009-01-29 19:24:30 +0000452 /// GetProtocolRef - Return a reference to the internal protocol
453 /// description, creating an empty one if it has not been
454 /// defined. The return value has type ProtocolPtrTy.
455 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000456
457 /// GetIvarBaseOffset - returns ivars byte offset.
458 uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout,
459 FieldDecl *Field);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000460
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000461 /// GetFieldBaseOffset - return's field byt offset.
462 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
463 const llvm::StructLayout *Layout,
464 FieldDecl *Field);
465
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000466 /// CreateMetadataVar - Create a global variable with internal
467 /// linkage for use by the Objective-C runtime.
468 ///
469 /// This is a convenience wrapper which not only creates the
470 /// variable, but also sets the section and alignment and adds the
471 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000472 ///
473 /// \param Name - The variable name.
474 /// \param Init - The variable initializer; this is also used to
475 /// define the type of the variable.
476 /// \param Section - The section the variable should go into, or 0.
477 /// \param Align - The alignment for the variable, or 0.
478 /// \param AddToUsed - Whether the variable should be added to
479 /// llvm.
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000480 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
481 llvm::Constant *Init,
482 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000483 unsigned Align,
484 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000485
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000486public:
487 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
488 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000489
490 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000491
492 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
493 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000494
495 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
496
497 /// GetOrEmitProtocol - Get the protocol object for the given
498 /// declaration, emitting it if necessary. The return value has type
499 /// ProtocolPtrTy.
500 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
501
502 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
503 /// object for the given declaration, emitting it if needed. These
504 /// forward references will be filled in with empty bodies if no
505 /// definition is seen. The return value has type ProtocolPtrTy.
506 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000507};
508
509class CGObjCMac : public CGObjCCommonMac {
510private:
511 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000512 /// EmitImageInfo - Emit the image info marker used to encode some module
513 /// level information.
514 void EmitImageInfo();
515
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000516 /// EmitModuleInfo - Another marker encoding module level
517 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000518 void EmitModuleInfo();
519
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000520 /// EmitModuleSymols - Emit module symbols, the list of defined
521 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000522 llvm::Constant *EmitModuleSymbols();
523
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000524 /// FinishModule - Write out global data structures at the end of
525 /// processing a translation unit.
526 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000527
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 /// EmitClassExtension - Generate the class extension structure used
529 /// to store the weak ivar layout and properties. The return value
530 /// has type ClassExtensionPtrTy.
531 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
532
533 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
534 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000535 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000536 const ObjCInterfaceDecl *ID);
537
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000538 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000539 QualType ResultType,
540 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000541 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000542 QualType Arg0Ty,
543 bool IsSuper,
544 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000545
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000546 /// EmitIvarList - Emit the ivar list for the given
547 /// implementation. If ForClass is true the list of class ivars
548 /// (i.e. metaclass ivars) is emitted, otherwise the list of
549 /// interface ivars will be emitted. The return value has type
550 /// IvarListPtrTy.
551 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000552 bool ForClass);
553
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000554 /// EmitMetaClass - Emit a forward reference to the class structure
555 /// for the metaclass of the given interface. The return value has
556 /// type ClassPtrTy.
557 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
558
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000559 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000560 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000561 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
562 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000563 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000564 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000565
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000566 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000567
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000568 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000569
570 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000571 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000572 llvm::Constant *EmitMethodList(const std::string &Name,
573 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000574 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000575
576 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000577 /// method declarations.
578 /// - TypeName: The name for the type containing the methods.
579 /// - IsProtocol: True iff these methods are for a protocol.
580 /// - ClassMethds: True iff these are class methods.
581 /// - Required: When true, only "required" methods are
582 /// listed. Similarly, when false only "optional" methods are
583 /// listed. For classes this should always be true.
584 /// - begin, end: The method list to output.
585 ///
586 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000587 llvm::Constant *EmitMethodDescList(const std::string &Name,
588 const char *Section,
589 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000590
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000591 /// GetOrEmitProtocol - Get the protocol object for the given
592 /// declaration, emitting it if necessary. The return value has type
593 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000594 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000595
596 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
597 /// object for the given declaration, emitting it if needed. These
598 /// forward references will be filled in with empty bodies if no
599 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000600 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000601
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000602 /// EmitProtocolExtension - Generate the protocol extension
603 /// structure used to store optional instance and class methods, and
604 /// protocol properties. The return value has type
605 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000606 llvm::Constant *
607 EmitProtocolExtension(const ObjCProtocolDecl *PD,
608 const ConstantVector &OptInstanceMethods,
609 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000610
611 /// EmitProtocolList - Generate the list of referenced
612 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000613 llvm::Constant *EmitProtocolList(const std::string &Name,
614 ObjCProtocolDecl::protocol_iterator begin,
615 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000616
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000617 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
618 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000619 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000620
Fariborz Jahanianda320092009-01-29 19:24:30 +0000621 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000622 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000623
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000624 virtual llvm::Function *ModuleInitFunction();
625
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000626 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000627 QualType ResultType,
628 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000629 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000630 bool IsClassMessage,
631 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000632
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000633 virtual CodeGen::RValue
634 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000635 QualType ResultType,
636 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000637 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000638 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000639 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000640 bool IsClassMessage,
641 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000642
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000643 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000644 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000645
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000646 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000647
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000648 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000649
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000650 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000651
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000652 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000653 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000654
Chris Lattner74391b42009-03-22 21:03:39 +0000655 virtual llvm::Constant *GetPropertyGetFunction();
656 virtual llvm::Constant *GetPropertySetFunction();
657 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000658
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000659 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
660 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000661 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
662 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000663 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000664 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000665 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
666 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000667 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
668 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000669 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
670 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000671 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
672 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000673
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000674 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
675 QualType ObjectTy,
676 llvm::Value *BaseValue,
677 const ObjCIvarDecl *Ivar,
678 const FieldDecl *Field,
679 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000680 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
681 ObjCInterfaceDecl *Interface,
682 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000683};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000684
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000685class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000686private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000687 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000688 llvm::GlobalVariable* ObjCEmptyCacheVar;
689 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000690
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000691 /// MetaClassReferences - uniqued meta class references.
692 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +0000693
694 /// EHTypeReferences - uniqued class ehtype references.
695 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000696
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000697 /// FinishNonFragileABIModule - Write out global data structures at the end of
698 /// processing a translation unit.
699 void FinishNonFragileABIModule();
700
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000701 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
702 unsigned InstanceStart,
703 unsigned InstanceSize,
704 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000705 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
706 llvm::Constant *IsAGV,
707 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000708 llvm::Constant *ClassRoGV,
709 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000710
711 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
712
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000713 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
714
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000715 /// EmitMethodList - Emit the method list for the given
716 /// implementation. The return value has type MethodListnfABITy.
717 llvm::Constant *EmitMethodList(const std::string &Name,
718 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000719 const ConstantVector &Methods);
720 /// EmitIvarList - Emit the ivar list for the given
721 /// implementation. If ForClass is true the list of class ivars
722 /// (i.e. metaclass ivars) is emitted, otherwise the list of
723 /// interface ivars will be emitted. The return value has type
724 /// IvarListnfABIPtrTy.
725 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000726
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000727 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000728 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000729 unsigned long int offset);
730
Fariborz Jahanianda320092009-01-29 19:24:30 +0000731 /// GetOrEmitProtocol - Get the protocol object for the given
732 /// declaration, emitting it if necessary. The return value has type
733 /// ProtocolPtrTy.
734 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
735
736 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
737 /// object for the given declaration, emitting it if needed. These
738 /// forward references will be filled in with empty bodies if no
739 /// definition is seen. The return value has type ProtocolPtrTy.
740 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
741
742 /// EmitProtocolList - Generate the list of referenced
743 /// protocols. The return value has type ProtocolListPtrTy.
744 llvm::Constant *EmitProtocolList(const std::string &Name,
745 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000746 ObjCProtocolDecl::protocol_iterator end);
747
748 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
749 QualType ResultType,
750 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000751 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000752 QualType Arg0Ty,
753 bool IsSuper,
754 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +0000755
756 /// GetClassGlobal - Return the global variable for the Objective-C
757 /// class of the given name.
758 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000759
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000760 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
761 /// for the given class.
762 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000763 const ObjCInterfaceDecl *ID,
764 bool IsSuper = false);
765
766 /// EmitMetaClassRef - Return a Value * of the address of _class_t
767 /// meta-data
768 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
769 const ObjCInterfaceDecl *ID);
770
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000771 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
772 /// the given ivar.
773 ///
774 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000775 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000776 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000777
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000778 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
779 /// for the given selector.
780 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +0000781
782 /// GetInterfaceEHType - Get the ehtype for the given Objective-C
783 /// interface. The return value has type EHTypePtrTy.
784 llvm::Value *GetInterfaceEHType(const ObjCInterfaceType *IT);
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000785
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000786public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000787 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000788 // FIXME. All stubs for now!
789 virtual llvm::Function *ModuleInitFunction();
790
791 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
792 QualType ResultType,
793 Selector Sel,
794 llvm::Value *Receiver,
795 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000796 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000797
798 virtual CodeGen::RValue
799 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
800 QualType ResultType,
801 Selector Sel,
802 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000803 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000804 llvm::Value *Receiver,
805 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000806 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000807
808 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000809 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000810
811 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000812 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000813
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000814 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000815
816 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000817 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000818 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000819
Chris Lattner74391b42009-03-22 21:03:39 +0000820 virtual llvm::Constant *GetPropertyGetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000821 return ObjCTypes.GetPropertyFn;
822 }
Chris Lattner74391b42009-03-22 21:03:39 +0000823 virtual llvm::Constant *GetPropertySetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000824 return ObjCTypes.SetPropertyFn;
825 }
Chris Lattner74391b42009-03-22 21:03:39 +0000826 virtual llvm::Constant *EnumerationMutationFunction() {
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000827 return ObjCTypes.EnumerationMutationFn;
828 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000829
830 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000831 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000832 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000833 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000834 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000835 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000836 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000837 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000838 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000839 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000840 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000841 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000842 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000843 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000844 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
845 QualType ObjectTy,
846 llvm::Value *BaseValue,
847 const ObjCIvarDecl *Ivar,
848 const FieldDecl *Field,
849 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000850 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
851 ObjCInterfaceDecl *Interface,
852 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000853};
854
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000855} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000856
857/* *** Helper Functions *** */
858
859/// getConstantGEP() - Help routine to construct simple GEPs.
860static llvm::Constant *getConstantGEP(llvm::Constant *C,
861 unsigned idx0,
862 unsigned idx1) {
863 llvm::Value *Idxs[] = {
864 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
865 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
866 };
867 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
868}
869
870/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000871
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000872CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
873 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000874{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000875 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000876 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000877}
878
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000879/// GetClass - Return a reference to the class for the given interface
880/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000881llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 const ObjCInterfaceDecl *ID) {
883 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000884}
885
886/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000887llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000888 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000889}
890
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000891/// Generate a constant CFString object.
892/*
893 struct __builtin_CFString {
894 const int *isa; // point to __CFConstantStringClassReference
895 int flags;
896 const char *str;
897 long length;
898 };
899*/
900
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000901llvm::Constant *CGObjCCommonMac::GenerateConstantString(
902 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000903 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000904}
905
906/// Generates a message send where the super is the receiver. This is
907/// a message send to self with special delivery semantics indicating
908/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000909CodeGen::RValue
910CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000911 QualType ResultType,
912 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000913 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000914 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000915 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000916 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000917 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000918 // Create and init a super structure; this is a (receiver, class)
919 // pair we will pass to objc_msgSendSuper.
920 llvm::Value *ObjCSuper =
921 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
922 llvm::Value *ReceiverAsObject =
923 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
924 CGF.Builder.CreateStore(ReceiverAsObject,
925 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000926
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000927 // If this is a class message the metaclass is passed as the target.
928 llvm::Value *Target;
929 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000930 if (isCategoryImpl) {
931 // Message sent to 'super' in a class method defined in a category
932 // implementation requires an odd treatment.
933 // If we are in a class method, we must retrieve the
934 // _metaclass_ for the current class, pointed at by
935 // the class's "isa" pointer. The following assumes that
936 // isa" is the first ivar in a class (which it must be).
937 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
938 Target = CGF.Builder.CreateStructGEP(Target, 0);
939 Target = CGF.Builder.CreateLoad(Target);
940 }
941 else {
942 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
943 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
944 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
945 Target = Super;
946 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000947 } else {
948 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
949 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000950 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
951 // and ObjCTypes types.
952 const llvm::Type *ClassTy =
953 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000954 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000955 CGF.Builder.CreateStore(Target,
956 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
957
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000958 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000959 ObjCSuper, ObjCTypes.SuperPtrCTy,
960 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000961}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000962
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000963/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000964CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000965 QualType ResultType,
966 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000967 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000968 bool IsClassMessage,
969 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000970 llvm::Value *Arg0 =
971 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000972 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000973 Arg0, CGF.getContext().getObjCIdType(),
974 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000975}
976
977CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000978 QualType ResultType,
979 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000980 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000981 QualType Arg0Ty,
982 bool IsSuper,
983 const CallArgList &CallArgs) {
984 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000985 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
986 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
987 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000988 CGF.getContext().getObjCSelType()));
989 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000990
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000991 CodeGenTypes &Types = CGM.getTypes();
992 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
993 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000994
995 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000996 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000997 Fn = ObjCTypes.getSendStretFn(IsSuper);
998 } else if (ResultType->isFloatingType()) {
999 // FIXME: Sadly, this is wrong. This actually depends on the
1000 // architecture. This happens to be right for x86-32 though.
1001 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1002 } else {
1003 Fn = ObjCTypes.getSendFn(IsSuper);
1004 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001005 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001006 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001007}
1008
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001009llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001010 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001011 // FIXME: I don't understand why gcc generates this, or where it is
1012 // resolved. Investigate. Its also wasteful to look this up over and
1013 // over.
1014 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1015
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1017 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001018}
1019
Fariborz Jahanianda320092009-01-29 19:24:30 +00001020void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001021 // FIXME: We shouldn't need this, the protocol decl should contain
1022 // enough information to tell us whether this was a declaration or a
1023 // definition.
1024 DefinedProtocols.insert(PD->getIdentifier());
1025
1026 // If we have generated a forward reference to this protocol, emit
1027 // it now. Otherwise do nothing, the protocol objects are lazily
1028 // emitted.
1029 if (Protocols.count(PD->getIdentifier()))
1030 GetOrEmitProtocol(PD);
1031}
1032
Fariborz Jahanianda320092009-01-29 19:24:30 +00001033llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001034 if (DefinedProtocols.count(PD->getIdentifier()))
1035 return GetOrEmitProtocol(PD);
1036 return GetOrEmitProtocolRef(PD);
1037}
1038
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039/*
1040 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1041 struct _objc_protocol {
1042 struct _objc_protocol_extension *isa;
1043 char *protocol_name;
1044 struct _objc_protocol_list *protocol_list;
1045 struct _objc__method_prototype_list *instance_methods;
1046 struct _objc__method_prototype_list *class_methods
1047 };
1048
1049 See EmitProtocolExtension().
1050*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001051llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1052 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1053
1054 // Early exit if a defining object has already been generated.
1055 if (Entry && Entry->hasInitializer())
1056 return Entry;
1057
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001058 // FIXME: I don't understand why gcc generates this, or where it is
1059 // resolved. Investigate. Its also wasteful to look this up over and
1060 // over.
1061 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1062
Chris Lattner8ec03f52008-11-24 03:54:41 +00001063 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064
1065 // Construct method lists.
1066 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1067 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
1068 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
1069 e = PD->instmeth_end(); i != e; ++i) {
1070 ObjCMethodDecl *MD = *i;
1071 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1072 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1073 OptInstanceMethods.push_back(C);
1074 } else {
1075 InstanceMethods.push_back(C);
1076 }
1077 }
1078
1079 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
1080 e = PD->classmeth_end(); i != e; ++i) {
1081 ObjCMethodDecl *MD = *i;
1082 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1083 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1084 OptClassMethods.push_back(C);
1085 } else {
1086 ClassMethods.push_back(C);
1087 }
1088 }
1089
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001090 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001091 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001093 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001094 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001095 PD->protocol_begin(),
1096 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001097 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001098 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1099 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001100 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1101 InstanceMethods);
1102 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001103 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1104 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001105 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1106 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1108 Values);
1109
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001110 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001111 // Already created, fix the linkage and update the initializer.
1112 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001113 Entry->setInitializer(Init);
1114 } else {
1115 Entry =
1116 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1117 llvm::GlobalValue::InternalLinkage,
1118 Init,
1119 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1120 &CGM.getModule());
1121 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001122 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001123 UsedGlobals.push_back(Entry);
1124 // FIXME: Is this necessary? Why only for protocol?
1125 Entry->setAlignment(4);
1126 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001127
1128 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001129}
1130
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001131llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001132 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1133
1134 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001135 // We use the initializer as a marker of whether this is a forward
1136 // reference or not. At module finalization we add the empty
1137 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138 Entry =
1139 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001140 llvm::GlobalValue::ExternalLinkage,
1141 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001142 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001143 &CGM.getModule());
1144 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001145 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146 UsedGlobals.push_back(Entry);
1147 // FIXME: Is this necessary? Why only for protocol?
1148 Entry->setAlignment(4);
1149 }
1150
1151 return Entry;
1152}
1153
1154/*
1155 struct _objc_protocol_extension {
1156 uint32_t size;
1157 struct objc_method_description_list *optional_instance_methods;
1158 struct objc_method_description_list *optional_class_methods;
1159 struct objc_property_list *instance_properties;
1160 };
1161*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001162llvm::Constant *
1163CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1164 const ConstantVector &OptInstanceMethods,
1165 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001166 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001167 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001168 std::vector<llvm::Constant*> Values(4);
1169 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001170 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001171 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1172 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001173 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1174 OptInstanceMethods);
1175 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001176 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1177 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001178 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1179 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001180 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1181 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001182 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001183
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001184 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001185 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1186 Values[3]->isNullValue())
1187 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1188
1189 llvm::Constant *Init =
1190 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001192 // No special section, but goes in llvm.used
1193 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1194 Init,
1195 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001196}
1197
1198/*
1199 struct objc_protocol_list {
1200 struct objc_protocol_list *next;
1201 long count;
1202 Protocol *list[];
1203 };
1204*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001205llvm::Constant *
1206CGObjCMac::EmitProtocolList(const std::string &Name,
1207 ObjCProtocolDecl::protocol_iterator begin,
1208 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001209 std::vector<llvm::Constant*> ProtocolRefs;
1210
Daniel Dunbardbc933702008-08-21 21:57:41 +00001211 for (; begin != end; ++begin)
1212 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001213
1214 // Just return null for empty protocol lists
1215 if (ProtocolRefs.empty())
1216 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1217
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001218 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001219 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1220
1221 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001222 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001223 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1224 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1225 Values[2] =
1226 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1227 ProtocolRefs.size()),
1228 ProtocolRefs);
1229
1230 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1231 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001232 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001233 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001234 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1235}
1236
1237/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001238 struct _objc_property {
1239 const char * const name;
1240 const char * const attributes;
1241 };
1242
1243 struct _objc_property_list {
1244 uint32_t entsize; // sizeof (struct _objc_property)
1245 uint32_t prop_count;
1246 struct _objc_property[prop_count];
1247 };
1248*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001249llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1250 const Decl *Container,
1251 const ObjCContainerDecl *OCD,
1252 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001253 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001254 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1255 E = OCD->prop_end(); I != E; ++I) {
1256 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001257 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001258 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001259 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1260 Prop));
1261 }
1262
1263 // Return null for empty list.
1264 if (Properties.empty())
1265 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1266
1267 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001268 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001269 std::vector<llvm::Constant*> Values(3);
1270 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1271 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1272 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1273 Properties.size());
1274 Values[2] = llvm::ConstantArray::get(AT, Properties);
1275 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1276
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001277 // No special section on property lists?
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001278 llvm::GlobalVariable *GV =
1279 CreateMetadataVar(Name, Init, (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
1280 0, true);
1281 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001282}
1283
1284/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001285 struct objc_method_description_list {
1286 int count;
1287 struct objc_method_description list[];
1288 };
1289*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001290llvm::Constant *
1291CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1292 std::vector<llvm::Constant*> Desc(2);
1293 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1294 ObjCTypes.SelectorPtrTy);
1295 Desc[1] = GetMethodVarType(MD);
1296 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1297 Desc);
1298}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001299
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001300llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1301 const char *Section,
1302 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001303 // Return null for empty list.
1304 if (Methods.empty())
1305 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1306
1307 std::vector<llvm::Constant*> Values(2);
1308 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1309 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1310 Methods.size());
1311 Values[1] = llvm::ConstantArray::get(AT, Methods);
1312 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1313
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001314 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001315 return llvm::ConstantExpr::getBitCast(GV,
1316 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001317}
1318
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001319/*
1320 struct _objc_category {
1321 char *category_name;
1322 char *class_name;
1323 struct _objc_method_list *instance_methods;
1324 struct _objc_method_list *class_methods;
1325 struct _objc_protocol_list *protocols;
1326 uint32_t size; // <rdar://4585769>
1327 struct _objc_property_list *instance_properties;
1328 };
1329 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001330void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001331 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001332
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001333 // FIXME: This is poor design, the OCD should have a pointer to the
1334 // category decl. Additionally, note that Category can be null for
1335 // the @implementation w/o an @interface case. Sema should just
1336 // create one for us as it does for @implementation so everyone else
1337 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001338 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001339 const ObjCCategoryDecl *Category =
1340 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001341 std::string ExtName(Interface->getNameAsString() + "_" +
1342 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001343
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001344 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1345 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1346 e = OCD->instmeth_end(); i != e; ++i) {
1347 // Instance methods should always be defined.
1348 InstanceMethods.push_back(GetMethodConstant(*i));
1349 }
1350 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1351 e = OCD->classmeth_end(); i != e; ++i) {
1352 // Class methods should always be defined.
1353 ClassMethods.push_back(GetMethodConstant(*i));
1354 }
1355
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001356 std::vector<llvm::Constant*> Values(7);
1357 Values[0] = GetClassName(OCD->getIdentifier());
1358 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001359 Values[2] =
1360 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1361 ExtName,
1362 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001363 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001364 Values[3] =
1365 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1366 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001367 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001368 if (Category) {
1369 Values[4] =
1370 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1371 Category->protocol_begin(),
1372 Category->protocol_end());
1373 } else {
1374 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1375 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001376 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001377
1378 // If there is no category @interface then there can be no properties.
1379 if (Category) {
1380 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001381 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001382 } else {
1383 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1384 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001385
1386 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1387 Values);
1388
1389 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001390 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1391 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001392 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001393 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001394}
1395
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001396// FIXME: Get from somewhere?
1397enum ClassFlags {
1398 eClassFlags_Factory = 0x00001,
1399 eClassFlags_Meta = 0x00002,
1400 // <rdr://5142207>
1401 eClassFlags_HasCXXStructors = 0x02000,
1402 eClassFlags_Hidden = 0x20000,
1403 eClassFlags_ABI2_Hidden = 0x00010,
1404 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1405};
1406
1407// <rdr://5142207&4705298&4843145>
1408static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1409 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1410 // FIXME: Support -fvisibility
1411 switch (attr->getVisibility()) {
1412 default:
1413 assert(0 && "Unknown visibility");
1414 return false;
1415 case VisibilityAttr::DefaultVisibility:
1416 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1417 return false;
1418 case VisibilityAttr::HiddenVisibility:
1419 return true;
1420 }
1421 } else {
1422 return false; // FIXME: Support -fvisibility
1423 }
1424}
1425
1426/*
1427 struct _objc_class {
1428 Class isa;
1429 Class super_class;
1430 const char *name;
1431 long version;
1432 long info;
1433 long instance_size;
1434 struct _objc_ivar_list *ivars;
1435 struct _objc_method_list *methods;
1436 struct _objc_cache *cache;
1437 struct _objc_protocol_list *protocols;
1438 // Objective-C 1.0 extensions (<rdr://4585769>)
1439 const char *ivar_layout;
1440 struct _objc_class_ext *ext;
1441 };
1442
1443 See EmitClassExtension();
1444 */
1445void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001446 DefinedSymbols.insert(ID->getIdentifier());
1447
Chris Lattner8ec03f52008-11-24 03:54:41 +00001448 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001449 // FIXME: Gross
1450 ObjCInterfaceDecl *Interface =
1451 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001452 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001453 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001454 Interface->protocol_begin(),
1455 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001456 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001457 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001458 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001459 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001460
1461 // FIXME: Set CXX-structors flag.
1462 if (IsClassHidden(ID->getClassInterface()))
1463 Flags |= eClassFlags_Hidden;
1464
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001465 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1466 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1467 e = ID->instmeth_end(); i != e; ++i) {
1468 // Instance methods should always be defined.
1469 InstanceMethods.push_back(GetMethodConstant(*i));
1470 }
1471 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1472 e = ID->classmeth_end(); i != e; ++i) {
1473 // Class methods should always be defined.
1474 ClassMethods.push_back(GetMethodConstant(*i));
1475 }
1476
1477 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1478 e = ID->propimpl_end(); i != e; ++i) {
1479 ObjCPropertyImplDecl *PID = *i;
1480
1481 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1482 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1483
1484 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1485 if (llvm::Constant *C = GetMethodConstant(MD))
1486 InstanceMethods.push_back(C);
1487 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1488 if (llvm::Constant *C = GetMethodConstant(MD))
1489 InstanceMethods.push_back(C);
1490 }
1491 }
1492
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001493 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001494 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001496 // Record a reference to the super class.
1497 LazySymbols.insert(Super->getIdentifier());
1498
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001499 Values[ 1] =
1500 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1501 ObjCTypes.ClassPtrTy);
1502 } else {
1503 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1504 }
1505 Values[ 2] = GetClassName(ID->getIdentifier());
1506 // Version is always 0.
1507 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1508 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1509 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001510 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001511 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001512 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001513 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001514 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001515 // cache is always NULL.
1516 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1517 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001518 // FIXME: Set ivar_layout
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001519 // Values[10] = BuildIvarLayout(ID, true);
1520 Values[10] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001521 Values[11] = EmitClassExtension(ID);
1522 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1523 Values);
1524
1525 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001526 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1527 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001528 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529 DefinedClasses.push_back(GV);
1530}
1531
1532llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1533 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001534 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001535 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001536 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001537 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001538
1539 if (IsClassHidden(ID->getClassInterface()))
1540 Flags |= eClassFlags_Hidden;
1541
1542 std::vector<llvm::Constant*> Values(12);
1543 // The isa for the metaclass is the root of the hierarchy.
1544 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1545 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1546 Root = Super;
1547 Values[ 0] =
1548 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1549 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001550 // The super class for the metaclass is emitted as the name of the
1551 // super class. The runtime fixes this up to point to the
1552 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001553 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1554 Values[ 1] =
1555 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1556 ObjCTypes.ClassPtrTy);
1557 } else {
1558 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1559 }
1560 Values[ 2] = GetClassName(ID->getIdentifier());
1561 // Version is always 0.
1562 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1563 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1564 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001565 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001566 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001567 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001568 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001569 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570 // cache is always NULL.
1571 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1572 Values[ 9] = Protocols;
1573 // ivar_layout for metaclass is always NULL.
1574 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1575 // The class extension is always unused for metaclasses.
1576 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1577 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1578 Values);
1579
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001580 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001581 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001582
1583 // Check for a forward reference.
1584 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1585 if (GV) {
1586 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1587 "Forward metaclass reference has incorrect type.");
1588 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1589 GV->setInitializer(Init);
1590 } else {
1591 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1592 llvm::GlobalValue::InternalLinkage,
1593 Init, Name,
1594 &CGM.getModule());
1595 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001596 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001597 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001598 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001599
1600 return GV;
1601}
1602
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001603llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001604 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001605
1606 // FIXME: Should we look these up somewhere other than the
1607 // module. Its a bit silly since we only generate these while
1608 // processing an implementation, so exactly one pointer would work
1609 // if know when we entered/exitted an implementation block.
1610
1611 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001612 // Previously, metaclass with internal linkage may have been defined.
1613 // pass 'true' as 2nd argument so it is returned.
1614 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001615 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1616 "Forward metaclass reference has incorrect type.");
1617 return GV;
1618 } else {
1619 // Generate as an external reference to keep a consistent
1620 // module. This will be patched up when we emit the metaclass.
1621 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1622 llvm::GlobalValue::ExternalLinkage,
1623 0,
1624 Name,
1625 &CGM.getModule());
1626 }
1627}
1628
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001629/*
1630 struct objc_class_ext {
1631 uint32_t size;
1632 const char *weak_ivar_layout;
1633 struct _objc_property_list *properties;
1634 };
1635*/
1636llvm::Constant *
1637CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1638 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001639 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001640
1641 std::vector<llvm::Constant*> Values(3);
1642 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001643 // FIXME: Output weak_ivar_layout string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001644 // Values[1] = BuildIvarLayout(ID, false);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001645 Values[1] = GetIvarLayoutName(0, ObjCTypes);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001646 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001647 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001648
1649 // Return null if no extension bits are used.
1650 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1651 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1652
1653 llvm::Constant *Init =
1654 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001655 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
1656 Init, 0, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001657}
1658
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001659/// countInheritedIvars - count number of ivars in class and its super class(s)
1660///
1661static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1662 int count = 0;
1663 if (!OI)
1664 return 0;
1665 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1666 if (SuperClass)
1667 count += countInheritedIvars(SuperClass);
1668 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1669 E = OI->ivar_end(); I != E; ++I)
1670 ++count;
1671 return count;
1672}
1673
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001674/// getInterfaceDeclForIvar - Get the interface declaration node where
1675/// this ivar is declared in.
1676/// FIXME. Ideally, this info should be in the ivar node. But currently
1677/// it is not and prevailing wisdom is that ASTs should not have more
1678/// info than is absolutely needed, even though this info reflects the
1679/// source language.
1680///
1681static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1682 const ObjCInterfaceDecl *OI,
1683 const ObjCIvarDecl *IVD) {
1684 if (!OI)
1685 return 0;
1686 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1687 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1688 E = OI->ivar_end(); I != E; ++I)
1689 if ((*I)->getIdentifier() == IVD->getIdentifier())
1690 return OI;
1691 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1692}
1693
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001694/*
1695 struct objc_ivar {
1696 char *ivar_name;
1697 char *ivar_type;
1698 int ivar_offset;
1699 };
1700
1701 struct objc_ivar_list {
1702 int ivar_count;
1703 struct objc_ivar list[count];
1704 };
1705 */
1706llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001707 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001708 std::vector<llvm::Constant*> Ivars, Ivar(3);
1709
1710 // When emitting the root class GCC emits ivar entries for the
1711 // actual class structure. It is not clear if we need to follow this
1712 // behavior; for now lets try and get away with not doing it. If so,
1713 // the cleanest solution would be to make up an ObjCInterfaceDecl
1714 // for the class.
1715 if (ForClass)
1716 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001717
1718 ObjCInterfaceDecl *OID =
1719 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00001720 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001721
1722 RecordDecl::field_iterator ifield, pfield;
1723 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001724 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1725 FieldDecl *Field = *ifield;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001726 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001727 if (Field->getIdentifier())
1728 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1729 else
1730 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00001731 Ivar[1] = GetMethodVarType(Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001732 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001733 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001734 }
1735
1736 // Return null for empty list.
1737 if (Ivars.empty())
1738 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1739
1740 std::vector<llvm::Constant*> Values(2);
1741 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1742 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1743 Ivars.size());
1744 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1745 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1746
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001747 llvm::GlobalVariable *GV;
1748 if (ForClass)
1749 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00001750 Init, "__OBJC,__class_vars,regular,no_dead_strip",
1751 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001752 else
1753 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
1754 + ID->getNameAsString(),
1755 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
1756 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001757 return llvm::ConstantExpr::getBitCast(GV,
1758 ObjCTypes.IvarListPtrTy);
1759}
1760
1761/*
1762 struct objc_method {
1763 SEL method_name;
1764 char *method_types;
1765 void *method;
1766 };
1767
1768 struct objc_method_list {
1769 struct objc_method_list *obsolete;
1770 int count;
1771 struct objc_method methods_list[count];
1772 };
1773*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001774
1775/// GetMethodConstant - Return a struct objc_method constant for the
1776/// given method if it has been defined. The result is null if the
1777/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001778llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001779 // FIXME: Use DenseMap::lookup
1780 llvm::Function *Fn = MethodDefinitions[MD];
1781 if (!Fn)
1782 return 0;
1783
1784 std::vector<llvm::Constant*> Method(3);
1785 Method[0] =
1786 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1787 ObjCTypes.SelectorPtrTy);
1788 Method[1] = GetMethodVarType(MD);
1789 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1790 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1791}
1792
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001793llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1794 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001795 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001796 // Return null for empty list.
1797 if (Methods.empty())
1798 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1799
1800 std::vector<llvm::Constant*> Values(3);
1801 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1802 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1803 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1804 Methods.size());
1805 Values[2] = llvm::ConstantArray::get(AT, Methods);
1806 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1807
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001808 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001809 return llvm::ConstantExpr::getBitCast(GV,
1810 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001811}
1812
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001813llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001814 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001815 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001816 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001817
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001818 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001819 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001820 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001821 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001822 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001823 llvm::GlobalValue::InternalLinkage,
1824 Name,
1825 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001826 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001827
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001828 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001829}
1830
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001831uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
1832 FieldDecl *Field) {
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00001833 if (!Field->isBitField())
1834 return Layout->getElementOffset(
1835 CGM.getTypes().getLLVMFieldNo(Field));
1836 // FIXME. Must be a better way of getting a bitfield base offset.
1837 uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field);
1838 const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
1839 uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
1840 offset = (offset*size)/8;
1841 return offset;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001842}
1843
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001844/// GetFieldBaseOffset - return's field byt offset.
1845uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
1846 const llvm::StructLayout *Layout,
1847 FieldDecl *Field) {
1848 const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
1849 Field = const_cast<ObjCInterfaceDecl*>(OI)->lookupFieldDeclForIvar(
1850 CGM.getContext(), Ivar);
1851 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
1852 return Offset;
1853}
1854
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001855llvm::GlobalVariable *
1856CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
1857 llvm::Constant *Init,
1858 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001859 unsigned Align,
1860 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001861 const llvm::Type *Ty = Init->getType();
1862 llvm::GlobalVariable *GV =
1863 new llvm::GlobalVariable(Ty, false,
1864 llvm::GlobalValue::InternalLinkage,
1865 Init,
1866 Name,
1867 &CGM.getModule());
1868 if (Section)
1869 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001870 if (Align)
1871 GV->setAlignment(Align);
1872 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001873 UsedGlobals.push_back(GV);
1874 return GV;
1875}
1876
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001877llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001878 // Abuse this interface function as a place to finalize.
1879 FinishModule();
1880
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001881 return NULL;
1882}
1883
Chris Lattner74391b42009-03-22 21:03:39 +00001884llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001885 return ObjCTypes.GetPropertyFn;
1886}
1887
Chris Lattner74391b42009-03-22 21:03:39 +00001888llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001889 return ObjCTypes.SetPropertyFn;
1890}
1891
Chris Lattner74391b42009-03-22 21:03:39 +00001892llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001893 return ObjCTypes.EnumerationMutationFn;
1894}
1895
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001896/*
1897
1898Objective-C setjmp-longjmp (sjlj) Exception Handling
1899--
1900
1901The basic framework for a @try-catch-finally is as follows:
1902{
1903 objc_exception_data d;
1904 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001905 bool _call_try_exit = true;
1906
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001907 objc_exception_try_enter(&d);
1908 if (!setjmp(d.jmp_buf)) {
1909 ... try body ...
1910 } else {
1911 // exception path
1912 id _caught = objc_exception_extract(&d);
1913
1914 // enter new try scope for handlers
1915 if (!setjmp(d.jmp_buf)) {
1916 ... match exception and execute catch blocks ...
1917
1918 // fell off end, rethrow.
1919 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001920 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001921 } else {
1922 // exception in catch block
1923 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001924 _call_try_exit = false;
1925 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001926 }
1927 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001928 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001929
1930finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001931 if (_call_try_exit)
1932 objc_exception_try_exit(&d);
1933
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001934 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001935 ... dispatch to finally destination ...
1936
1937finally_rethrow:
1938 objc_exception_throw(_rethrow);
1939
1940finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001941}
1942
1943This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001944uses _rethrow to determine if objc_exception_try_exit should be called
1945and if the object should be rethrown. This breaks in the face of
1946throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001947
1948We specialize this framework for a few particular circumstances:
1949
1950 - If there are no catch blocks, then we avoid emitting the second
1951 exception handling context.
1952
1953 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1954 e)) we avoid emitting the code to rethrow an uncaught exception.
1955
1956 - FIXME: If there is no @finally block we can do a few more
1957 simplifications.
1958
1959Rethrows and Jumps-Through-Finally
1960--
1961
1962Support for implicit rethrows and jumping through the finally block is
1963handled by storing the current exception-handling context in
1964ObjCEHStack.
1965
Daniel Dunbar898d5082008-09-30 01:06:03 +00001966In order to implement proper @finally semantics, we support one basic
1967mechanism for jumping through the finally block to an arbitrary
1968destination. Constructs which generate exits from a @try or @catch
1969block use this mechanism to implement the proper semantics by chaining
1970jumps, as necessary.
1971
1972This mechanism works like the one used for indirect goto: we
1973arbitrarily assign an ID to each destination and store the ID for the
1974destination in a variable prior to entering the finally block. At the
1975end of the finally block we simply create a switch to the proper
1976destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001977
1978Code gen for @synchronized(expr) stmt;
1979Effectively generating code for:
1980objc_sync_enter(expr);
1981@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001982*/
1983
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001984void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1985 const Stmt &S) {
1986 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001987 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001988 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001989 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001990 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1991 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1992 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00001993
1994 // For @synchronized, call objc_sync_enter(sync.expr). The
1995 // evaluation of the expression must occur before we enter the
1996 // @synchronized. We can safely avoid a temp here because jumps into
1997 // @synchronized are illegal & this will dominate uses.
1998 llvm::Value *SyncArg = 0;
1999 if (!isTry) {
2000 SyncArg =
2001 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2002 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
2003 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
2004 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002005
2006 // Push an EH context entry, used for handling rethrows and jumps
2007 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002008 CGF.PushCleanupBlock(FinallyBlock);
2009
Anders Carlsson273558f2009-02-07 21:37:21 +00002010 CGF.ObjCEHValueStack.push_back(0);
2011
Daniel Dunbar898d5082008-09-30 01:06:03 +00002012 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002013 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2014 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002015 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2016 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002017 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2018 "_call_try_exit");
2019 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2020
Anders Carlsson80f25672008-09-09 17:59:25 +00002021 // Enter a new try block and call setjmp.
2022 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
2023 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2024 "jmpbufarray");
2025 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
2026 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2027 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002028
Daniel Dunbar55e87422008-11-11 02:29:29 +00002029 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2030 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002031 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002032 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002033
2034 // Emit the @try block.
2035 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002036 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2037 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002038 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002039
2040 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002041 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002042
2043 // Retrieve the exception object. We may emit multiple blocks but
2044 // nothing can cross this so the value is already in SSA form.
2045 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2046 ExceptionData,
2047 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002048 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002049 if (!isTry)
2050 {
2051 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002052 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002053 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002054 }
2055 else if (const ObjCAtCatchStmt* CatchStmt =
2056 cast<ObjCAtTryStmt>(S).getCatchStmts())
2057 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002058 // Enter a new exception try block (in case a @catch block throws
2059 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00002060 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002061
Anders Carlsson80f25672008-09-09 17:59:25 +00002062 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2063 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002064 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002065
Daniel Dunbar55e87422008-11-11 02:29:29 +00002066 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2067 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002068 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002069
2070 CGF.EmitBlock(CatchBlock);
2071
Daniel Dunbar55e40722008-09-27 07:03:52 +00002072 // Handle catch list. As a special case we check if everything is
2073 // matched and avoid generating code for falling off the end if
2074 // so.
2075 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002076 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002077 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002078
Steve Naroff7ba138a2009-03-03 19:52:17 +00002079 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002080 const PointerType *PT = 0;
2081
Anders Carlsson80f25672008-09-09 17:59:25 +00002082 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002083 if (!CatchParam) {
2084 AllMatched = true;
2085 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002086 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002087
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002088 // catch(id e) always matches.
2089 // FIXME: For the time being we also match id<X>; this should
2090 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002091 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002092 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002093 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002094 }
2095
Daniel Dunbar55e40722008-09-27 07:03:52 +00002096 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002097 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002098 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002099 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002100 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002101 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002102
Anders Carlssondde0a942008-09-11 09:15:33 +00002103 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002104 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002105 break;
2106 }
2107
Daniel Dunbar129271a2008-09-27 07:36:24 +00002108 assert(PT && "Unexpected non-pointer type in @catch");
2109 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002110 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002111 assert(ObjCType && "Catch parameter must have Objective-C type!");
2112
2113 // Check if the @catch block matches the exception object.
2114 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2115
Anders Carlsson80f25672008-09-09 17:59:25 +00002116 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2117 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002118
Daniel Dunbar55e87422008-11-11 02:29:29 +00002119 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002120
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002121 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002122 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002123
2124 // Emit the @catch block.
2125 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002126 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002127 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002128
2129 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002130 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002131 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002132 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002133
2134 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002135 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002136
2137 CGF.EmitBlock(NextCatchBlock);
2138 }
2139
Daniel Dunbar55e40722008-09-27 07:03:52 +00002140 if (!AllMatched) {
2141 // None of the handlers caught the exception, so store it to be
2142 // rethrown at the end of the @finally block.
2143 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002144 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002145 }
2146
2147 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002148 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002149 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2150 ExceptionData),
2151 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002152 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002153 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002154 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002155 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002156 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002157 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002158 }
2159
Daniel Dunbar898d5082008-09-30 01:06:03 +00002160 // Pop the exception-handling stack entry. It is important to do
2161 // this now, because the code in the @finally block is not in this
2162 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002163 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2164
Anders Carlsson273558f2009-02-07 21:37:21 +00002165 CGF.ObjCEHValueStack.pop_back();
2166
Anders Carlsson80f25672008-09-09 17:59:25 +00002167 // Emit the @finally block.
2168 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002169 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2170
2171 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2172
2173 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002174 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002175
2176 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002177 if (isTry) {
2178 if (const ObjCAtFinallyStmt* FinallyStmt =
2179 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2180 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002181 } else {
2182 // Emit objc_sync_exit(expr); as finally's sole statement for
2183 // @synchronized.
2184 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002185 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002186
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002187 // Emit the switch block
2188 if (Info.SwitchBlock)
2189 CGF.EmitBlock(Info.SwitchBlock);
2190 if (Info.EndBlock)
2191 CGF.EmitBlock(Info.EndBlock);
2192
Daniel Dunbar898d5082008-09-30 01:06:03 +00002193 CGF.EmitBlock(FinallyRethrow);
2194 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2195 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002196 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002197
2198 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002199}
2200
2201void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002202 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002203 llvm::Value *ExceptionAsObject;
2204
2205 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2206 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2207 ExceptionAsObject =
2208 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2209 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002210 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002211 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002212 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002213 }
2214
2215 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002216 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002217
2218 // Clear the insertion point to indicate we are in unreachable code.
2219 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002220}
2221
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002222/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002223/// object: objc_read_weak (id *src)
2224///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002225llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002226 llvm::Value *AddrWeakObj)
2227{
Eli Friedman8339b352009-03-07 03:57:15 +00002228 const llvm::Type* DestTy =
2229 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002230 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002231 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002232 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002233 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002234 return read_weak;
2235}
2236
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002237/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2238/// objc_assign_weak (id src, id *dst)
2239///
2240void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2241 llvm::Value *src, llvm::Value *dst)
2242{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002243 const llvm::Type * SrcTy = src->getType();
2244 if (!isa<llvm::PointerType>(SrcTy)) {
2245 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2246 assert(Size <= 8 && "does not support size > 8");
2247 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2248 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002249 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2250 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002251 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2252 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002253 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2254 src, dst, "weakassign");
2255 return;
2256}
2257
Fariborz Jahanian58626502008-11-19 00:59:10 +00002258/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2259/// objc_assign_global (id src, id *dst)
2260///
2261void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2262 llvm::Value *src, llvm::Value *dst)
2263{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002264 const llvm::Type * SrcTy = src->getType();
2265 if (!isa<llvm::PointerType>(SrcTy)) {
2266 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2267 assert(Size <= 8 && "does not support size > 8");
2268 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2269 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002270 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2271 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002272 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2273 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002274 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2275 src, dst, "globalassign");
2276 return;
2277}
2278
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002279/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2280/// objc_assign_ivar (id src, id *dst)
2281///
2282void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2283 llvm::Value *src, llvm::Value *dst)
2284{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002285 const llvm::Type * SrcTy = src->getType();
2286 if (!isa<llvm::PointerType>(SrcTy)) {
2287 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2288 assert(Size <= 8 && "does not support size > 8");
2289 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2290 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002291 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2292 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002293 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2294 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2295 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2296 src, dst, "assignivar");
2297 return;
2298}
2299
Fariborz Jahanian58626502008-11-19 00:59:10 +00002300/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2301/// objc_assign_strongCast (id src, id *dst)
2302///
2303void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2304 llvm::Value *src, llvm::Value *dst)
2305{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002306 const llvm::Type * SrcTy = src->getType();
2307 if (!isa<llvm::PointerType>(SrcTy)) {
2308 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2309 assert(Size <= 8 && "does not support size > 8");
2310 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2311 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002312 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2313 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002314 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2315 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002316 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2317 src, dst, "weakassign");
2318 return;
2319}
2320
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002321/// EmitObjCValueForIvar - Code Gen for ivar reference.
2322///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002323LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2324 QualType ObjectTy,
2325 llvm::Value *BaseValue,
2326 const ObjCIvarDecl *Ivar,
2327 const FieldDecl *Field,
2328 unsigned CVRQualifiers) {
2329 if (Ivar->isBitField())
2330 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2331 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002332 // TODO: Add a special case for isa (index 0)
2333 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2334 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002335 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002336 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2337 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002338 LValue::SetObjCIvar(LV, true);
2339 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002340}
2341
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002342llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2343 ObjCInterfaceDecl *Interface,
2344 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002345 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002346 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00002347 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002348 return llvm::ConstantInt::get(
2349 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2350 Offset);
2351}
2352
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002353/* *** Private Interface *** */
2354
2355/// EmitImageInfo - Emit the image info marker used to encode some module
2356/// level information.
2357///
2358/// See: <rdr://4810609&4810587&4810587>
2359/// struct IMAGE_INFO {
2360/// unsigned version;
2361/// unsigned flags;
2362/// };
2363enum ImageInfoFlags {
2364 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2365 eImageInfo_GarbageCollected = (1 << 1),
2366 eImageInfo_GCOnly = (1 << 2)
2367};
2368
2369void CGObjCMac::EmitImageInfo() {
2370 unsigned version = 0; // Version is unused?
2371 unsigned flags = 0;
2372
2373 // FIXME: Fix and continue?
2374 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2375 flags |= eImageInfo_GarbageCollected;
2376 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2377 flags |= eImageInfo_GCOnly;
2378
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002379 // Emitted as int[2];
2380 llvm::Constant *values[2] = {
2381 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2382 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2383 };
2384 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002385
2386 const char *Section;
2387 if (ObjCABI == 1)
2388 Section = "__OBJC, __image_info,regular";
2389 else
2390 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002391 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002392 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2393 llvm::ConstantArray::get(AT, values, 2),
2394 Section,
2395 0,
2396 true);
2397 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002398}
2399
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002400
2401// struct objc_module {
2402// unsigned long version;
2403// unsigned long size;
2404// const char *name;
2405// Symtab symtab;
2406// };
2407
2408// FIXME: Get from somewhere
2409static const int ModuleVersion = 7;
2410
2411void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002412 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002413
2414 std::vector<llvm::Constant*> Values(4);
2415 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2416 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002417 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002418 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002419 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002420 CreateMetadataVar("\01L_OBJC_MODULES",
2421 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2422 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002423 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002424}
2425
2426llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427 unsigned NumClasses = DefinedClasses.size();
2428 unsigned NumCategories = DefinedCategories.size();
2429
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002430 // Return null if no symbols were defined.
2431 if (!NumClasses && !NumCategories)
2432 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2433
2434 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002435 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2436 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2437 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2438 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2439
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002440 // The runtime expects exactly the list of defined classes followed
2441 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002442 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002443 for (unsigned i=0; i<NumClasses; i++)
2444 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2445 ObjCTypes.Int8PtrTy);
2446 for (unsigned i=0; i<NumCategories; i++)
2447 Symbols[NumClasses + i] =
2448 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2449 ObjCTypes.Int8PtrTy);
2450
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002452 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002453 NumClasses + NumCategories),
2454 Symbols);
2455
2456 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2457
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002458 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002459 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2460 "__OBJC,__symbols,regular,no_dead_strip",
2461 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2463}
2464
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002465llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002466 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002467 LazySymbols.insert(ID->getIdentifier());
2468
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002469 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2470
2471 if (!Entry) {
2472 llvm::Constant *Casted =
2473 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2474 ObjCTypes.ClassPtrTy);
2475 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002476 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2477 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
2478 0, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 }
2480
2481 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002482}
2483
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002484llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002485 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2486
2487 if (!Entry) {
2488 llvm::Constant *Casted =
2489 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2490 ObjCTypes.SelectorPtrTy);
2491 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002492 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2493 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
2494 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002495 }
2496
2497 return Builder.CreateLoad(Entry, false, "tmp");
2498}
2499
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002500llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002501 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002502
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002503 if (!Entry)
2504 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2505 llvm::ConstantArray::get(Ident->getName()),
2506 "__TEXT,__cstring,cstring_literals",
2507 0, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002508
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002509 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002510}
2511
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002512/// GetInterfaceDeclStructLayout - Get layout for ivars of given
2513/// interface declaration.
2514const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout(
2515 const ObjCInterfaceDecl *OID) const {
2516 const llvm::Type *InterfaceTy =
2517 CGM.getTypes().ConvertType(
2518 CGM.getContext().getObjCInterfaceType(
2519 const_cast<ObjCInterfaceDecl*>(OID)));
2520 const llvm::StructLayout *Layout =
2521 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
2522 return Layout;
2523}
2524
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002525/// GetIvarLayoutName - Returns a unique constant for the given
2526/// ivar layout bitmap.
2527llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2528 const ObjCCommonTypesHelper &ObjCTypes) {
2529 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2530}
2531
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002532void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2533 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002534 const RecordDecl *RD,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002535 const std::vector<FieldDecl*>& RecFields,
2536 unsigned int BytePos, bool ForStrongLayout,
2537 int &Index, int &SkIndex, bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002538 bool IsUnion = (RD && RD->isUnion());
2539 uint64_t MaxUnionIvarSize = 0;
2540 uint64_t MaxSkippedUnionIvarSize = 0;
2541 FieldDecl *MaxField = 0;
2542 FieldDecl *MaxSkippedField = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002543 unsigned int base = 0;
2544 if (RecFields.empty())
2545 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002546 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002547 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
2548 unsigned WordSizeInBits = CGM.getContext().getTypeSize(
2549 CGM.getContext().VoidPtrTy);
2550 unsigned ByteSizeInBits = CGM.getContext().getTypeSize(
2551 CGM.getContext().CharTy);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002552 for (unsigned i = 0; i < RecFields.size(); i++) {
2553 FieldDecl *Field = RecFields[i];
2554 // Skip over unnamed or bitfields
2555 if (!Field->getIdentifier() || Field->isBitField())
2556 continue;
2557 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002558 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002559 std::vector<FieldDecl*> NestedRecFields;
2560 if (FQT->isUnionType())
2561 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002562 else
2563 assert(FQT->isRecordType() &&
2564 "only union/record is supported for ivar layout bitmap");
2565
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002566 const RecordType *RT = FQT->getAsRecordType();
2567 const RecordDecl *RD = RT->getDecl();
2568 // FIXME - Find a more efficiant way of passing records down.
2569 unsigned j = 0;
2570 for (RecordDecl::field_iterator i = RD->field_begin(),
2571 e = RD->field_end(); i != e; ++i)
2572 NestedRecFields[j++] = (*i);
2573 // FIXME - Is Layout correct?
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002574 BuildAggrIvarLayout(OI, Layout, RD, NestedRecFields,
2575 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002576 ForStrongLayout, Index, SkIndex,
2577 HasUnion);
2578 continue;
2579 }
2580 else if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002581 const ConstantArrayType *CArray =
2582 dyn_cast_or_null<ConstantArrayType>(Array);
2583 assert(CArray && "only array with know element size is supported");
2584 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002585 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2586 const ConstantArrayType *CArray =
2587 dyn_cast_or_null<ConstantArrayType>(Array);
2588 FQT = CArray->getElementType();
2589 }
2590
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002591 assert(!FQT->isUnionType() &&
2592 "layout for array of unions not supported");
2593 if (FQT->isRecordType()) {
2594 uint64_t ElCount = CArray->getSize().getZExtValue();
2595 int OldIndex = Index;
2596 int OldSkIndex = SkIndex;
2597
2598 std::vector<FieldDecl*> ElementRecFields;
2599 // FIXME - Use a common routine with the above!
2600 const RecordType *RT = FQT->getAsRecordType();
2601 const RecordDecl *RD = RT->getDecl();
2602 // FIXME - Find a more efficiant way of passing records down.
2603 unsigned j = 0;
2604 for (RecordDecl::field_iterator i = RD->field_begin(),
2605 e = RD->field_end(); i != e; ++i)
2606 ElementRecFields[j++] = (*i);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002607 BuildAggrIvarLayout(OI, Layout, RD,
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002608 ElementRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002609 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002610 ForStrongLayout, Index, SkIndex,
2611 HasUnion);
2612 // Replicate layout information for each array element. Note that
2613 // one element is already done.
2614 uint64_t ElIx = 1;
2615 for (int FirstIndex = Index, FirstSkIndex = SkIndex;
2616 ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002617 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002618 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2619 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002620 GC_IVAR gcivar;
2621 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
2622 gcivar.ivar_size = IvarsInfo[i].ivar_size;
2623 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002624 }
2625
2626 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
2627 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002628 GC_IVAR skivar;
2629 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
2630 skivar.ivar_size = SkipIvars[i].ivar_size;
2631 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002632 }
2633 }
2634 continue;
2635 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002636 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002637 // At this point, we are done with Record/Union and array there of.
2638 // For other arrays we are down to its element type.
2639 QualType::GCAttrTypes GCAttr = QualType::GCNone;
2640 do {
2641 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
2642 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
2643 break;
2644 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002645 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002646 GCAttr = QualType::Strong;
2647 break;
2648 }
2649 else if (const PointerType *PT = FQT->getAsPointerType()) {
2650 FQT = PT->getPointeeType();
2651 }
2652 else {
2653 break;
2654 }
2655 } while (true);
2656 if ((ForStrongLayout && GCAttr == QualType::Strong)
2657 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
2658 if (IsUnion)
2659 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002660 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
2661 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002662 if (UnionIvarSize > MaxUnionIvarSize)
2663 {
2664 MaxUnionIvarSize = UnionIvarSize;
2665 MaxField = Field;
2666 }
2667 }
2668 else
2669 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002670 GC_IVAR gcivar;
2671 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2672 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2673 WordSizeInBits;
2674 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002675 }
2676 }
2677 else if ((ForStrongLayout &&
2678 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
2679 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
2680 if (IsUnion)
2681 {
2682 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
2683 if (UnionIvarSize > MaxSkippedUnionIvarSize)
2684 {
2685 MaxSkippedUnionIvarSize = UnionIvarSize;
2686 MaxSkippedField = Field;
2687 }
2688 }
2689 else
2690 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002691 GC_IVAR skivar;
2692 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2693 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2694 WordSizeInBits;
2695 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002696 }
2697 }
2698 }
2699 if (MaxField)
2700 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002701 GC_IVAR gcivar;
2702 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
2703 gcivar.ivar_size = MaxUnionIvarSize;
2704 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002705 }
2706 if (MaxSkippedField)
2707 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002708 GC_IVAR skivar;
2709 skivar.ivar_bytepos = BytePos +
2710 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
2711 skivar.ivar_size = MaxSkippedUnionIvarSize;
2712 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002713 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002714 return;
2715}
2716
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002717static int
2718IvarBytePosCompare (const void *a, const void *b)
2719{
2720 unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos;
2721 unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos;
2722
2723 if (sa < sb)
2724 return -1;
2725 if (sa > sb)
2726 return 1;
2727 return 0;
2728}
2729
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002730/// BuildIvarLayout - Builds ivar layout bitmap for the class
2731/// implementation for the __strong or __weak case.
2732/// The layout map displays which words in ivar list must be skipped
2733/// and which must be scanned by GC (see below). String is built of bytes.
2734/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
2735/// of words to skip and right nibble is count of words to scan. So, each
2736/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
2737/// represented by a 0x00 byte which also ends the string.
2738/// 1. when ForStrongLayout is true, following ivars are scanned:
2739/// - id, Class
2740/// - object *
2741/// - __strong anything
2742///
2743/// 2. When ForStrongLayout is false, following ivars are scanned:
2744/// - __weak anything
2745///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002746llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002747 const ObjCImplementationDecl *OMD,
2748 bool ForStrongLayout) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002749 int Index = -1;
2750 int SkIndex = -1;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002751 bool hasUnion = false;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002752 int SkipScan;
2753 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002754 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2755 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
2756 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002757
2758 std::vector<FieldDecl*> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002759 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002760 CGM.getContext().CollectObjCIvars(OI, RecFields);
2761 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002762 return llvm::Constant::getNullValue(PtrTy);
2763 SkipIvars.clear();
2764 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002765
2766 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002767 BuildAggrIvarLayout (OI, Layout, 0, RecFields, 0, ForStrongLayout,
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002768 Index, SkIndex, hasUnion);
2769 if (Index == -1)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002770 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002771
2772 // Sort on byte position in case we encounterred a union nested in
2773 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002774 if (hasUnion && !IvarsInfo.empty())
2775 qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2776 if (hasUnion && !SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002777 qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2778
2779 // Build the string of skip/scan nibbles
2780 SkipScan = -1;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002781 SkipScanIvars.clear();
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002782 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002783 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002784 if (IvarsInfo[0].ivar_bytepos == 0) {
2785 WordsToSkip = 0;
2786 WordsToScan = IvarsInfo[0].ivar_size;
2787 }
2788 else {
2789 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
2790 WordsToScan = IvarsInfo[0].ivar_size;
2791 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002792 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002793 {
2794 unsigned int TailPrevGCObjC =
2795 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
2796 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
2797 {
2798 // consecutive 'scanned' object pointers.
2799 WordsToScan += IvarsInfo[i].ivar_size;
2800 }
2801 else
2802 {
2803 // Skip over 'gc'able object pointer which lay over each other.
2804 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
2805 continue;
2806 // Must skip over 1 or more words. We save current skip/scan values
2807 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002808 SKIP_SCAN SkScan;
2809 SkScan.skip = WordsToSkip;
2810 SkScan.scan = WordsToScan;
2811 SkipScanIvars.push_back(SkScan); ++SkipScan;
2812
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002813 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002814 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
2815 SkScan.scan = 0;
2816 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002817 WordsToSkip = 0;
2818 WordsToScan = IvarsInfo[i].ivar_size;
2819 }
2820 }
2821 if (WordsToScan > 0)
2822 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002823 SKIP_SCAN SkScan;
2824 SkScan.skip = WordsToSkip;
2825 SkScan.scan = WordsToScan;
2826 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002827 }
2828
2829 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002830 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002831 {
2832 int LastByteSkipped =
2833 SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size;
2834 int LastByteScanned =
2835 IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize;
2836 BytesSkipped = (LastByteSkipped > LastByteScanned);
2837 // Compute number of bytes to skip at the tail end of the last ivar scanned.
2838 if (BytesSkipped)
2839 {
2840 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002841 SKIP_SCAN SkScan;
2842 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
2843 SkScan.scan = 0;
2844 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002845 }
2846 }
2847 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
2848 // as 0xMN.
2849 for (int i = 0; i <= SkipScan; i++)
2850 {
2851 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
2852 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
2853 // 0xM0 followed by 0x0N detected.
2854 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
2855 for (int j = i+1; j < SkipScan; j++)
2856 SkipScanIvars[j] = SkipScanIvars[j+1];
2857 --SkipScan;
2858 }
2859 }
2860
2861 // Generate the string.
2862 std::string BitMap;
2863 for (int i = 0; i <= SkipScan; i++)
2864 {
2865 unsigned char byte;
2866 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
2867 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
2868 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
2869 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
2870
2871 if (skip_small > 0 || skip_big > 0)
2872 BytesSkipped = true;
2873 // first skip big.
2874 for (unsigned int ix = 0; ix < skip_big; ix++)
2875 BitMap += (unsigned char)(0xf0);
2876
2877 // next (skip small, scan)
2878 if (skip_small)
2879 {
2880 byte = skip_small << 4;
2881 if (scan_big > 0)
2882 {
2883 byte |= 0xf;
2884 --scan_big;
2885 }
2886 else if (scan_small)
2887 {
2888 byte |= scan_small;
2889 scan_small = 0;
2890 }
2891 BitMap += byte;
2892 }
2893 // next scan big
2894 for (unsigned int ix = 0; ix < scan_big; ix++)
2895 BitMap += (unsigned char)(0x0f);
2896 // last scan small
2897 if (scan_small)
2898 {
2899 byte = scan_small;
2900 BitMap += byte;
2901 }
2902 }
2903 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002904 unsigned char zero = 0;
2905 BitMap += zero;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002906 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
2907 // final layout.
2908 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002909 return llvm::Constant::getNullValue(PtrTy);
2910 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2911 llvm::ConstantArray::get(BitMap.c_str()),
2912 "__TEXT,__cstring,cstring_literals",
2913 0, true);
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002914 // FIXME. Need a commomand-line option for this eventually.
2915 if (ForStrongLayout)
2916 printf("\nstrong ivar layout: ");
2917 else
2918 printf("\nweak ivar layout: ");
2919 const unsigned char *s = (unsigned char*)BitMap.c_str();
2920 for (unsigned i = 0; i < BitMap.size(); i++)
2921 printf("0x%x ", s[i]);
2922 printf("\n");
2923
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002924 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002925}
2926
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002927llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002928 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2929
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002930 // FIXME: Avoid std::string copying.
2931 if (!Entry)
2932 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
2933 llvm::ConstantArray::get(Sel.getAsString()),
2934 "__TEXT,__cstring,cstring_literals",
2935 0, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002936
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002937 return getConstantGEP(Entry, 0, 0);
2938}
2939
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002940// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002941llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002942 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2943}
2944
2945// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002946llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002947 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2948}
2949
Devang Patel7794bb82009-03-04 18:21:39 +00002950llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) {
2951 std::string TypeStr;
2952 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
2953
2954 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002955
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002956 if (!Entry)
2957 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
2958 llvm::ConstantArray::get(TypeStr),
2959 "__TEXT,__cstring,cstring_literals",
2960 0, true);
2961
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002962 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002963}
2964
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002965llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002966 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002967 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2968 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00002969
2970 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
2971
2972 if (!Entry) {
2973 llvm::Constant *C = llvm::ConstantArray::get(TypeStr);
2974 Entry =
2975 new llvm::GlobalVariable(C->getType(), false,
2976 llvm::GlobalValue::InternalLinkage,
2977 C, "\01L_OBJC_METH_VAR_TYPE_",
2978 &CGM.getModule());
2979 Entry->setSection("__TEXT,__cstring,cstring_literals");
2980 UsedGlobals.push_back(Entry);
2981 }
2982
2983 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002984}
2985
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002986// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002987llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002988 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2989
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002990 if (!Entry)
2991 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
2992 llvm::ConstantArray::get(Ident->getName()),
2993 "__TEXT,__cstring,cstring_literals",
2994 0, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002995
2996 return getConstantGEP(Entry, 0, 0);
2997}
2998
2999// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003000// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003001llvm::Constant *
3002 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3003 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003004 std::string TypeStr;
3005 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003006 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3007}
3008
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003009void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3010 const ObjCContainerDecl *CD,
3011 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003012 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00003013 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00003014 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003015 assert (CD && "Missing container decl in GetNameForMethod");
3016 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00003017 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
3018 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00003019 NameOut += ' ';
3020 NameOut += D->getSelector().getAsString();
3021 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003022}
3023
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003024/// GetFirstIvarInRecord - This routine returns the record for the
3025/// implementation of the fiven class OID. It also returns field
3026/// corresponding to the first ivar in the class in FIV. It also
3027/// returns the one before the first ivar.
3028///
3029const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
3030 const ObjCInterfaceDecl *OID,
3031 RecordDecl::field_iterator &FIV,
3032 RecordDecl::field_iterator &PIV) {
3033 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
3034 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
3035 RecordDecl::field_iterator ifield = RD->field_begin();
3036 RecordDecl::field_iterator pfield = RD->field_end();
3037 while (countSuperClassIvars-- > 0) {
3038 pfield = ifield;
3039 ++ifield;
3040 }
3041 FIV = ifield;
3042 PIV = pfield;
3043 return RD;
3044}
3045
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003046void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003047 EmitModuleInfo();
3048
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003049 // Emit the dummy bodies for any protocols which were referenced but
3050 // never defined.
3051 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3052 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3053 if (i->second->hasInitializer())
3054 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003055
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003056 std::vector<llvm::Constant*> Values(5);
3057 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3058 Values[1] = GetClassName(i->first);
3059 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3060 Values[3] = Values[4] =
3061 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3062 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3063 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3064 Values));
3065 }
3066
3067 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003068 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003069 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003070 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003071 }
3072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003073 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003074 llvm::GlobalValue *GV =
3075 new llvm::GlobalVariable(AT, false,
3076 llvm::GlobalValue::AppendingLinkage,
3077 llvm::ConstantArray::get(AT, Used),
3078 "llvm.used",
3079 &CGM.getModule());
3080
3081 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003082
3083 // Add assembler directives to add lazy undefined symbol references
3084 // for classes which are referenced but not defined. This is
3085 // important for correct linker interaction.
3086
3087 // FIXME: Uh, this isn't particularly portable.
3088 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003089
3090 if (!CGM.getModule().getModuleInlineAsm().empty())
3091 s << "\n";
3092
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003093 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3094 e = LazySymbols.end(); i != e; ++i) {
3095 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3096 }
3097 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3098 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003099 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003100 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3101 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003102
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003103 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003104}
3105
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003106CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003107 : CGObjCCommonMac(cgm),
3108 ObjCTypes(cgm)
3109{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003110 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003111 ObjCABI = 2;
3112}
3113
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003114/* *** */
3115
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003116ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3117: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003118{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003119 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3120 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003121
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003122 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003123 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003124 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003125 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003126 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3127
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003128 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003129 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003130 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003131
3132 // FIXME: It would be nice to unify this with the opaque type, so
3133 // that the IR comes out a bit cleaner.
3134 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3135 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003136
3137 // I'm not sure I like this. The implicit coordination is a bit
3138 // gross. We should solve this in a reasonable fashion because this
3139 // is a pretty common task (match some runtime data structure with
3140 // an LLVM data structure).
3141
3142 // FIXME: This is leaked.
3143 // FIXME: Merge with rewriter code?
3144
3145 // struct _objc_super {
3146 // id self;
3147 // Class cls;
3148 // }
3149 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3150 SourceLocation(),
3151 &Ctx.Idents.get("_objc_super"));
3152 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3153 Ctx.getObjCIdType(), 0, false));
3154 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3155 Ctx.getObjCClassType(), 0, false));
3156 RD->completeDefinition(Ctx);
3157
3158 SuperCTy = Ctx.getTagDeclType(RD);
3159 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3160
3161 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003162 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3163
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003164 // struct _prop_t {
3165 // char *name;
3166 // char *attributes;
3167 // }
3168 PropertyTy = llvm::StructType::get(Int8PtrTy,
3169 Int8PtrTy,
3170 NULL);
3171 CGM.getModule().addTypeName("struct._prop_t",
3172 PropertyTy);
3173
3174 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003175 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003176 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003177 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003178 // }
3179 PropertyListTy = llvm::StructType::get(IntTy,
3180 IntTy,
3181 llvm::ArrayType::get(PropertyTy, 0),
3182 NULL);
3183 CGM.getModule().addTypeName("struct._prop_list_t",
3184 PropertyListTy);
3185 // struct _prop_list_t *
3186 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3187
3188 // struct _objc_method {
3189 // SEL _cmd;
3190 // char *method_type;
3191 // char *_imp;
3192 // }
3193 MethodTy = llvm::StructType::get(SelectorPtrTy,
3194 Int8PtrTy,
3195 Int8PtrTy,
3196 NULL);
3197 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003198
3199 // struct _objc_cache *
3200 CacheTy = llvm::OpaqueType::get();
3201 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3202 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003203
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003204 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003205
3206 QualType IdType = Ctx.getObjCIdType();
3207 QualType SelType = Ctx.getObjCSelType();
3208 llvm::SmallVector<QualType,16> Params;
3209 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003210
3211 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003212 Params.push_back(IdType);
3213 Params.push_back(SelType);
3214 Params.push_back(Ctx.LongTy);
3215 Params.push_back(Ctx.BoolTy);
3216 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
3217 false);
3218 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003219
3220 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
3221 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003222 Params.push_back(IdType);
3223 Params.push_back(SelType);
3224 Params.push_back(Ctx.LongTy);
3225 Params.push_back(IdType);
3226 Params.push_back(Ctx.BoolTy);
3227 Params.push_back(Ctx.BoolTy);
3228 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3229 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
3230
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003231 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003232
3233 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003234 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003235 Params.push_back(IdType);
3236 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3237 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
3238 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003239
3240 // gc's API
3241 // id objc_read_weak (id *)
3242 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003243 Params.push_back(Ctx.getPointerType(IdType));
3244 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3245 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
3246
3247 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003248 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003249 Params.push_back(IdType);
3250 Params.push_back(Ctx.getPointerType(IdType));
3251
3252 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3253 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
3254 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
3255 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
3256 GcAssignStrongCastFn =
3257 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003258
3259 // void objc_exception_throw(id)
3260 Params.clear();
3261 Params.push_back(IdType);
3262
3263 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003264 ExceptionThrowFn =
3265 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00003266
3267 // synchronized APIs
3268 // void objc_sync_enter (id)
3269 // void objc_sync_exit (id)
3270 Params.clear();
3271 Params.push_back(IdType);
3272
3273 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3274 SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
3275 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003276}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003277
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003278ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3279 : ObjCCommonTypesHelper(cgm)
3280{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003281 // struct _objc_method_description {
3282 // SEL name;
3283 // char *types;
3284 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003285 MethodDescriptionTy =
3286 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003287 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003288 NULL);
3289 CGM.getModule().addTypeName("struct._objc_method_description",
3290 MethodDescriptionTy);
3291
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003292 // struct _objc_method_description_list {
3293 // int count;
3294 // struct _objc_method_description[1];
3295 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003296 MethodDescriptionListTy =
3297 llvm::StructType::get(IntTy,
3298 llvm::ArrayType::get(MethodDescriptionTy, 0),
3299 NULL);
3300 CGM.getModule().addTypeName("struct._objc_method_description_list",
3301 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003302
3303 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003304 MethodDescriptionListPtrTy =
3305 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3306
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003307 // Protocol description structures
3308
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003309 // struct _objc_protocol_extension {
3310 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3311 // struct _objc_method_description_list *optional_instance_methods;
3312 // struct _objc_method_description_list *optional_class_methods;
3313 // struct _objc_property_list *instance_properties;
3314 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003315 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003316 llvm::StructType::get(IntTy,
3317 MethodDescriptionListPtrTy,
3318 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003319 PropertyListPtrTy,
3320 NULL);
3321 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3322 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003323
3324 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003325 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3326
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003327 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003328
3329 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3330 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3331
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003332 const llvm::Type *T =
3333 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3334 LongTy,
3335 llvm::ArrayType::get(ProtocolTyHolder, 0),
3336 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003337 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3338
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003339 // struct _objc_protocol {
3340 // struct _objc_protocol_extension *isa;
3341 // char *protocol_name;
3342 // struct _objc_protocol **_objc_protocol_list;
3343 // struct _objc_method_description_list *instance_methods;
3344 // struct _objc_method_description_list *class_methods;
3345 // }
3346 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003347 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003348 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3349 MethodDescriptionListPtrTy,
3350 MethodDescriptionListPtrTy,
3351 NULL);
3352 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3353
3354 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3355 CGM.getModule().addTypeName("struct._objc_protocol_list",
3356 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003357 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003358 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3359
3360 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003361 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003362 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003363
3364 // Class description structures
3365
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003366 // struct _objc_ivar {
3367 // char *ivar_name;
3368 // char *ivar_type;
3369 // int ivar_offset;
3370 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003371 IvarTy = llvm::StructType::get(Int8PtrTy,
3372 Int8PtrTy,
3373 IntTy,
3374 NULL);
3375 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3376
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003377 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003378 IvarListTy = llvm::OpaqueType::get();
3379 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3380 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3381
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003382 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003383 MethodListTy = llvm::OpaqueType::get();
3384 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3385 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3386
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003387 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003388 ClassExtensionTy =
3389 llvm::StructType::get(IntTy,
3390 Int8PtrTy,
3391 PropertyListPtrTy,
3392 NULL);
3393 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3394 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3395
3396 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3397
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003398 // struct _objc_class {
3399 // Class isa;
3400 // Class super_class;
3401 // char *name;
3402 // long version;
3403 // long info;
3404 // long instance_size;
3405 // struct _objc_ivar_list *ivars;
3406 // struct _objc_method_list *methods;
3407 // struct _objc_cache *cache;
3408 // struct _objc_protocol_list *protocols;
3409 // char *ivar_layout;
3410 // struct _objc_class_ext *ext;
3411 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003412 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3413 llvm::PointerType::getUnqual(ClassTyHolder),
3414 Int8PtrTy,
3415 LongTy,
3416 LongTy,
3417 LongTy,
3418 IvarListPtrTy,
3419 MethodListPtrTy,
3420 CachePtrTy,
3421 ProtocolListPtrTy,
3422 Int8PtrTy,
3423 ClassExtensionPtrTy,
3424 NULL);
3425 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3426
3427 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3428 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3429 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3430
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003431 // struct _objc_category {
3432 // char *category_name;
3433 // char *class_name;
3434 // struct _objc_method_list *instance_method;
3435 // struct _objc_method_list *class_method;
3436 // uint32_t size; // sizeof(struct _objc_category)
3437 // struct _objc_property_list *instance_properties;// category's @property
3438 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003439 CategoryTy = llvm::StructType::get(Int8PtrTy,
3440 Int8PtrTy,
3441 MethodListPtrTy,
3442 MethodListPtrTy,
3443 ProtocolListPtrTy,
3444 IntTy,
3445 PropertyListPtrTy,
3446 NULL);
3447 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3448
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003449 // Global metadata structures
3450
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003451 // struct _objc_symtab {
3452 // long sel_ref_cnt;
3453 // SEL *refs;
3454 // short cls_def_cnt;
3455 // short cat_def_cnt;
3456 // char *defs[cls_def_cnt + cat_def_cnt];
3457 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003458 SymtabTy = llvm::StructType::get(LongTy,
3459 SelectorPtrTy,
3460 ShortTy,
3461 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003462 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003463 NULL);
3464 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3465 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3466
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003467 // struct _objc_module {
3468 // long version;
3469 // long size; // sizeof(struct _objc_module)
3470 // char *name;
3471 // struct _objc_symtab* symtab;
3472 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003473 ModuleTy =
3474 llvm::StructType::get(LongTy,
3475 LongTy,
3476 Int8PtrTy,
3477 SymtabPtrTy,
3478 NULL);
3479 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003480
Daniel Dunbar49f66022008-09-24 03:38:44 +00003481 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003482
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003483 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003484 std::vector<const llvm::Type*> Params;
3485 Params.push_back(ObjectPtrTy);
3486 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003487 MessageSendFn =
3488 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3489 Params,
3490 true),
3491 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003492
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003493 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003494 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003495 Params.push_back(ObjectPtrTy);
3496 Params.push_back(SelectorPtrTy);
3497 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003498 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3499 Params,
3500 true),
3501 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003502
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003503 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003504 Params.clear();
3505 Params.push_back(ObjectPtrTy);
3506 Params.push_back(SelectorPtrTy);
3507 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003508 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003509 MessageSendFpretFn =
3510 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3511 Params,
3512 true),
3513 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003514
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003515 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003516 Params.clear();
3517 Params.push_back(SuperPtrTy);
3518 Params.push_back(SelectorPtrTy);
3519 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003520 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3521 Params,
3522 true),
3523 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003524
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003525 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3526 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003527 Params.clear();
3528 Params.push_back(Int8PtrTy);
3529 Params.push_back(SuperPtrTy);
3530 Params.push_back(SelectorPtrTy);
3531 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003532 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3533 Params,
3534 true),
3535 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003536
3537 // There is no objc_msgSendSuper_fpret? How can that work?
3538 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003539
Anders Carlsson124526b2008-09-09 10:10:21 +00003540 // FIXME: This is the size of the setjmp buffer and should be
3541 // target specific. 18 is what's used on 32-bit X86.
3542 uint64_t SetJmpBufferSize = 18;
3543
3544 // Exceptions
3545 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003546 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003547
3548 ExceptionDataTy =
3549 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3550 SetJmpBufferSize),
3551 StackPtrTy, NULL);
3552 CGM.getModule().addTypeName("struct._objc_exception_data",
3553 ExceptionDataTy);
3554
3555 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003556 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3557 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003558 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3559 Params,
3560 false),
3561 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003562 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003563 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3564 Params,
3565 false),
3566 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003567 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003568 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3569 Params,
3570 false),
3571 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003572
3573 Params.clear();
3574 Params.push_back(ClassPtrTy);
3575 Params.push_back(ObjectPtrTy);
3576 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003577 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3578 Params,
3579 false),
3580 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003581
Anders Carlsson124526b2008-09-09 10:10:21 +00003582 Params.clear();
3583 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3584 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003585 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3586 Params,
3587 false),
3588 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003589
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003590}
3591
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003592ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003593: ObjCCommonTypesHelper(cgm)
3594{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003595 // struct _method_list_t {
3596 // uint32_t entsize; // sizeof(struct _objc_method)
3597 // uint32_t method_count;
3598 // struct _objc_method method_list[method_count];
3599 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003600 MethodListnfABITy = llvm::StructType::get(IntTy,
3601 IntTy,
3602 llvm::ArrayType::get(MethodTy, 0),
3603 NULL);
3604 CGM.getModule().addTypeName("struct.__method_list_t",
3605 MethodListnfABITy);
3606 // struct method_list_t *
3607 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003608
3609 // struct _protocol_t {
3610 // id isa; // NULL
3611 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003612 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003613 // const struct method_list_t * const instance_methods;
3614 // const struct method_list_t * const class_methods;
3615 // const struct method_list_t *optionalInstanceMethods;
3616 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003617 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003618 // const uint32_t size; // sizeof(struct _protocol_t)
3619 // const uint32_t flags; // = 0
3620 // }
3621
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003622 // Holder for struct _protocol_list_t *
3623 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3624
3625 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3626 Int8PtrTy,
3627 llvm::PointerType::getUnqual(
3628 ProtocolListTyHolder),
3629 MethodListnfABIPtrTy,
3630 MethodListnfABIPtrTy,
3631 MethodListnfABIPtrTy,
3632 MethodListnfABIPtrTy,
3633 PropertyListPtrTy,
3634 IntTy,
3635 IntTy,
3636 NULL);
3637 CGM.getModule().addTypeName("struct._protocol_t",
3638 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003639
3640 // struct _protocol_t*
3641 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003642
Fariborz Jahanianda320092009-01-29 19:24:30 +00003643 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003644 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003645 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003646 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003647 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3648 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003649 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003650 NULL);
3651 CGM.getModule().addTypeName("struct._objc_protocol_list",
3652 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003653 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3654 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003655
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003656 // struct _objc_protocol_list*
3657 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003658
3659 // struct _ivar_t {
3660 // unsigned long int *offset; // pointer to ivar offset location
3661 // char *name;
3662 // char *type;
3663 // uint32_t alignment;
3664 // uint32_t size;
3665 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003666 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3667 Int8PtrTy,
3668 Int8PtrTy,
3669 IntTy,
3670 IntTy,
3671 NULL);
3672 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3673
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003674 // struct _ivar_list_t {
3675 // uint32 entsize; // sizeof(struct _ivar_t)
3676 // uint32 count;
3677 // struct _iver_t list[count];
3678 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003679 IvarListnfABITy = llvm::StructType::get(IntTy,
3680 IntTy,
3681 llvm::ArrayType::get(
3682 IvarnfABITy, 0),
3683 NULL);
3684 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3685
3686 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003687
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003688 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003689 // uint32_t const flags;
3690 // uint32_t const instanceStart;
3691 // uint32_t const instanceSize;
3692 // uint32_t const reserved; // only when building for 64bit targets
3693 // const uint8_t * const ivarLayout;
3694 // const char *const name;
3695 // const struct _method_list_t * const baseMethods;
3696 // const struct _objc_protocol_list *const baseProtocols;
3697 // const struct _ivar_list_t *const ivars;
3698 // const uint8_t * const weakIvarLayout;
3699 // const struct _prop_list_t * const properties;
3700 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003701
3702 // FIXME. Add 'reserved' field in 64bit abi mode!
3703 ClassRonfABITy = llvm::StructType::get(IntTy,
3704 IntTy,
3705 IntTy,
3706 Int8PtrTy,
3707 Int8PtrTy,
3708 MethodListnfABIPtrTy,
3709 ProtocolListnfABIPtrTy,
3710 IvarListnfABIPtrTy,
3711 Int8PtrTy,
3712 PropertyListPtrTy,
3713 NULL);
3714 CGM.getModule().addTypeName("struct._class_ro_t",
3715 ClassRonfABITy);
3716
3717 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3718 std::vector<const llvm::Type*> Params;
3719 Params.push_back(ObjectPtrTy);
3720 Params.push_back(SelectorPtrTy);
3721 ImpnfABITy = llvm::PointerType::getUnqual(
3722 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3723
3724 // struct _class_t {
3725 // struct _class_t *isa;
3726 // struct _class_t * const superclass;
3727 // void *cache;
3728 // IMP *vtable;
3729 // struct class_ro_t *ro;
3730 // }
3731
3732 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3733 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3734 llvm::PointerType::getUnqual(ClassTyHolder),
3735 CachePtrTy,
3736 llvm::PointerType::getUnqual(ImpnfABITy),
3737 llvm::PointerType::getUnqual(
3738 ClassRonfABITy),
3739 NULL);
3740 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3741
3742 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3743 ClassnfABITy);
3744
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003745 // LLVM for struct _class_t *
3746 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3747
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003748 // struct _category_t {
3749 // const char * const name;
3750 // struct _class_t *const cls;
3751 // const struct _method_list_t * const instance_methods;
3752 // const struct _method_list_t * const class_methods;
3753 // const struct _protocol_list_t * const protocols;
3754 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003755 // }
3756 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003757 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003758 MethodListnfABIPtrTy,
3759 MethodListnfABIPtrTy,
3760 ProtocolListnfABIPtrTy,
3761 PropertyListPtrTy,
3762 NULL);
3763 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003764
3765 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003766 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3767 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003768
3769 // MessageRefTy - LLVM for:
3770 // struct _message_ref_t {
3771 // IMP messenger;
3772 // SEL name;
3773 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003774
3775 // First the clang type for struct _message_ref_t
3776 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3777 SourceLocation(),
3778 &Ctx.Idents.get("_message_ref_t"));
3779 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3780 Ctx.VoidPtrTy, 0, false));
3781 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3782 Ctx.getObjCSelType(), 0, false));
3783 RD->completeDefinition(Ctx);
3784
3785 MessageRefCTy = Ctx.getTagDeclType(RD);
3786 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3787 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003788
3789 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3790 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3791
3792 // SuperMessageRefTy - LLVM for:
3793 // struct _super_message_ref_t {
3794 // SUPER_IMP messenger;
3795 // SEL name;
3796 // };
3797 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3798 SelectorPtrTy,
3799 NULL);
3800 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3801
3802 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3803 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3804
3805 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3806 Params.clear();
3807 Params.push_back(ObjectPtrTy);
3808 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003809 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3810 Params,
3811 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003812 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003813 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003814 "objc_msgSend_fixup");
3815
3816 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3817 MessageSendFpretFixupFn =
3818 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3819 Params,
3820 true),
3821 "objc_msgSend_fpret_fixup");
3822
3823 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3824 MessageSendStretFixupFn =
3825 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3826 Params,
3827 true),
3828 "objc_msgSend_stret_fixup");
3829
3830 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3831 MessageSendIdFixupFn =
3832 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3833 Params,
3834 true),
3835 "objc_msgSendId_fixup");
3836
3837
3838 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3839 MessageSendIdStretFixupFn =
3840 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3841 Params,
3842 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003843 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003844
3845 // id objc_msgSendSuper2_fixup (struct objc_super *,
3846 // struct _super_message_ref_t*, ...)
3847 Params.clear();
3848 Params.push_back(SuperPtrTy);
3849 Params.push_back(SuperMessageRefPtrTy);
3850 MessageSendSuper2FixupFn =
3851 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3852 Params,
3853 true),
3854 "objc_msgSendSuper2_fixup");
3855
3856
3857 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3858 // struct _super_message_ref_t*, ...)
3859 MessageSendSuper2StretFixupFn =
3860 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3861 Params,
3862 true),
3863 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003864
3865 Params.clear();
3866 llvm::Constant *Personality =
3867 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3868 Params,
3869 true),
3870 "__objc_personality_v0");
3871 EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
3872
3873 Params.clear();
3874 Params.push_back(Int8PtrTy);
3875 UnwindResumeOrRethrowFn =
3876 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3877 Params,
3878 false),
3879 "_Unwind_Resume_or_Rethrow");
Daniel Dunbare588b992009-03-01 04:46:24 +00003880 ObjCBeginCatchFn =
3881 CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
3882 Params,
3883 false),
3884 "objc_begin_catch");
3885
3886 Params.clear();
3887 ObjCEndCatchFn =
3888 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3889 Params,
3890 false),
3891 "objc_end_catch");
3892
3893 // struct objc_typeinfo {
3894 // const void** vtable; // objc_ehtype_vtable + 2
3895 // const char* name; // c++ typeinfo string
3896 // Class cls;
3897 // };
3898 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3899 Int8PtrTy,
3900 ClassnfABIPtrTy,
3901 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003902 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003903 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003904}
3905
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003906llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3907 FinishNonFragileABIModule();
3908
3909 return NULL;
3910}
3911
3912void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3913 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003914
3915 // Build list of all implemented classe addresses in array
3916 // L_OBJC_LABEL_CLASS_$.
3917 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3918 // list of 'nonlazy' implementations (defined as those with a +load{}
3919 // method!!).
3920 unsigned NumClasses = DefinedClasses.size();
3921 if (NumClasses) {
3922 std::vector<llvm::Constant*> Symbols(NumClasses);
3923 for (unsigned i=0; i<NumClasses; i++)
3924 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3925 ObjCTypes.Int8PtrTy);
3926 llvm::Constant* Init =
3927 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3928 NumClasses),
3929 Symbols);
3930
3931 llvm::GlobalVariable *GV =
3932 new llvm::GlobalVariable(Init->getType(), false,
3933 llvm::GlobalValue::InternalLinkage,
3934 Init,
3935 "\01L_OBJC_LABEL_CLASS_$",
3936 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003937 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003938 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3939 UsedGlobals.push_back(GV);
3940 }
3941
3942 // Build list of all implemented category addresses in array
3943 // L_OBJC_LABEL_CATEGORY_$.
3944 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3945 // list of 'nonlazy' category implementations (defined as those with a +load{}
3946 // method!!).
3947 unsigned NumCategory = DefinedCategories.size();
3948 if (NumCategory) {
3949 std::vector<llvm::Constant*> Symbols(NumCategory);
3950 for (unsigned i=0; i<NumCategory; i++)
3951 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3952 ObjCTypes.Int8PtrTy);
3953 llvm::Constant* Init =
3954 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3955 NumCategory),
3956 Symbols);
3957
3958 llvm::GlobalVariable *GV =
3959 new llvm::GlobalVariable(Init->getType(), false,
3960 llvm::GlobalValue::InternalLinkage,
3961 Init,
3962 "\01L_OBJC_LABEL_CATEGORY_$",
3963 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003964 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003965 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3966 UsedGlobals.push_back(GV);
3967 }
3968
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003969 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3970 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3971 std::vector<llvm::Constant*> Values(2);
3972 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003973 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00003974 // FIXME: Fix and continue?
3975 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3976 flags |= eImageInfo_GarbageCollected;
3977 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3978 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00003979 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003980 llvm::Constant* Init = llvm::ConstantArray::get(
3981 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3982 Values);
3983 llvm::GlobalVariable *IMGV =
3984 new llvm::GlobalVariable(Init->getType(), false,
3985 llvm::GlobalValue::InternalLinkage,
3986 Init,
3987 "\01L_OBJC_IMAGE_INFO",
3988 &CGM.getModule());
3989 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3990 UsedGlobals.push_back(IMGV);
3991
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003992 std::vector<llvm::Constant*> Used;
3993 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3994 e = UsedGlobals.end(); i != e; ++i) {
3995 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3996 }
3997
3998 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3999 llvm::GlobalValue *GV =
4000 new llvm::GlobalVariable(AT, false,
4001 llvm::GlobalValue::AppendingLinkage,
4002 llvm::ConstantArray::get(AT, Used),
4003 "llvm.used",
4004 &CGM.getModule());
4005
4006 GV->setSection("llvm.metadata");
4007
4008}
4009
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004010// Metadata flags
4011enum MetaDataDlags {
4012 CLS = 0x0,
4013 CLS_META = 0x1,
4014 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004015 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004016 CLS_EXCEPTION = 0x20
4017};
4018/// BuildClassRoTInitializer - generate meta-data for:
4019/// struct _class_ro_t {
4020/// uint32_t const flags;
4021/// uint32_t const instanceStart;
4022/// uint32_t const instanceSize;
4023/// uint32_t const reserved; // only when building for 64bit targets
4024/// const uint8_t * const ivarLayout;
4025/// const char *const name;
4026/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004027/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004028/// const struct _ivar_list_t *const ivars;
4029/// const uint8_t * const weakIvarLayout;
4030/// const struct _prop_list_t * const properties;
4031/// }
4032///
4033llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4034 unsigned flags,
4035 unsigned InstanceStart,
4036 unsigned InstanceSize,
4037 const ObjCImplementationDecl *ID) {
4038 std::string ClassName = ID->getNameAsString();
4039 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4040 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4041 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4042 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4043 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00004044 // FIXME. ivarLayout is currently null!
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004045 Values[ 3] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004046 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004047 // const struct _method_list_t * const baseMethods;
4048 std::vector<llvm::Constant*> Methods;
4049 std::string MethodListName("\01l_OBJC_$_");
4050 if (flags & CLS_META) {
4051 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4052 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
4053 e = ID->classmeth_end(); i != e; ++i) {
4054 // Class methods should always be defined.
4055 Methods.push_back(GetMethodConstant(*i));
4056 }
4057 } else {
4058 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4059 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
4060 e = ID->instmeth_end(); i != e; ++i) {
4061 // Instance methods should always be defined.
4062 Methods.push_back(GetMethodConstant(*i));
4063 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004064 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
4065 e = ID->propimpl_end(); i != e; ++i) {
4066 ObjCPropertyImplDecl *PID = *i;
4067
4068 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4069 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4070
4071 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4072 if (llvm::Constant *C = GetMethodConstant(MD))
4073 Methods.push_back(C);
4074 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4075 if (llvm::Constant *C = GetMethodConstant(MD))
4076 Methods.push_back(C);
4077 }
4078 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004079 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004080 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004081 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004082
4083 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4084 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4085 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4086 + OID->getNameAsString(),
4087 OID->protocol_begin(),
4088 OID->protocol_end());
4089
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004090 if (flags & CLS_META)
4091 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4092 else
4093 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004094 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004095 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004096 if (flags & CLS_META)
4097 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4098 else
4099 Values[ 9] =
4100 EmitPropertyList(
4101 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4102 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4104 Values);
4105 llvm::GlobalVariable *CLASS_RO_GV =
4106 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4107 llvm::GlobalValue::InternalLinkage,
4108 Init,
4109 (flags & CLS_META) ?
4110 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4111 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4112 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004113 CLASS_RO_GV->setAlignment(
4114 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004115 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004116 UsedGlobals.push_back(CLASS_RO_GV);
4117 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004118
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004119}
4120
4121/// BuildClassMetaData - This routine defines that to-level meta-data
4122/// for the given ClassName for:
4123/// struct _class_t {
4124/// struct _class_t *isa;
4125/// struct _class_t * const superclass;
4126/// void *cache;
4127/// IMP *vtable;
4128/// struct class_ro_t *ro;
4129/// }
4130///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004131llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4132 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004133 llvm::Constant *IsAGV,
4134 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004135 llvm::Constant *ClassRoGV,
4136 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004137 std::vector<llvm::Constant*> Values(5);
4138 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004139 Values[1] = SuperClassGV
4140 ? SuperClassGV
4141 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004142 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4143 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4144 Values[4] = ClassRoGV; // &CLASS_RO_GV
4145 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4146 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004147 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4148 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004149 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004150 GV->setAlignment(
4151 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004152 if (HiddenVisibility)
4153 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004154 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004155 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004156}
4157
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004158void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4159 std::string ClassName = ID->getNameAsString();
4160 if (!ObjCEmptyCacheVar) {
4161 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004162 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004163 false,
4164 llvm::GlobalValue::ExternalLinkage,
4165 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00004166 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004167 &CGM.getModule());
4168 UsedGlobals.push_back(ObjCEmptyCacheVar);
4169
4170 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004171 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004172 false,
4173 llvm::GlobalValue::ExternalLinkage,
4174 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00004175 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004176 &CGM.getModule());
4177 UsedGlobals.push_back(ObjCEmptyVtableVar);
4178 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004179 assert(ID->getClassInterface() &&
4180 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004181 uint32_t InstanceStart =
4182 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4183 uint32_t InstanceSize = InstanceStart;
4184 uint32_t flags = CLS_META;
4185 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
4186 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004187
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004188 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004189
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004190 bool classIsHidden = IsClassHidden(ID->getClassInterface());
4191 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004192 flags |= OBJC2_CLS_HIDDEN;
4193 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004194 // class is root
4195 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004196 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
4197 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004198 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004199 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004200 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4201 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4202 Root = Super;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004203 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004204 // work on super class metadata symbol.
4205 std::string SuperClassName =
4206 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004207 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004208 }
4209 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4210 InstanceStart,
4211 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004212 std::string TClassName = ObjCMetaClassName + ClassName;
4213 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004214 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4215 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004216
4217 // Metadata for the class
4218 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004219 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004220 flags |= OBJC2_CLS_HIDDEN;
4221 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004222 flags |= CLS_ROOT;
4223 SuperClassGV = 0;
4224 }
4225 else {
4226 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004227 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004228 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004229 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004230 }
Fariborz Jahanianebf9ed32009-03-20 20:48:19 +00004231 // FIXME: Gross
4232 ObjCInterfaceDecl *Interface =
4233 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
4234 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004235 InstanceStart = InstanceSize = 0;
4236 if (ObjCInterfaceDecl *OID =
4237 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
4238 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004239 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004240
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004241 RecordDecl::field_iterator firstField, lastField;
4242 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004243
4244 for (RecordDecl::field_iterator e = RD->field_end(),
4245 ifield = firstField; ifield != e; ++ifield)
4246 lastField = ifield;
4247
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004248 if (lastField != RD->field_end()) {
4249 FieldDecl *Field = *lastField;
4250 const llvm::Type *FieldTy =
4251 CGM.getTypes().ConvertTypeForMem(Field->getType());
4252 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004253 InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004254 if (firstField == RD->field_end())
4255 InstanceStart = InstanceSize;
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004256 else {
4257 Field = *firstField;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004258 InstanceStart = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian11894a42009-03-07 19:43:20 +00004259 }
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004260 }
4261 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004262 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004263 InstanceStart,
4264 InstanceSize,
4265 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004266
4267 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004268 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004269 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4270 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004271 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004272}
4273
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004274/// GenerateProtocolRef - This routine is called to generate code for
4275/// a protocol reference expression; as in:
4276/// @code
4277/// @protocol(Proto1);
4278/// @endcode
4279/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4280/// which will hold address of the protocol meta-data.
4281///
4282llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4283 const ObjCProtocolDecl *PD) {
4284
4285 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
4286 ObjCTypes.ExternalProtocolPtrTy);
4287
4288 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4289 ProtocolName += PD->getNameAsCString();
4290
4291 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4292 if (PTGV)
4293 return Builder.CreateLoad(PTGV, false, "tmp");
4294 PTGV = new llvm::GlobalVariable(
4295 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004296 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004297 Init,
4298 ProtocolName,
4299 &CGM.getModule());
4300 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4301 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4302 UsedGlobals.push_back(PTGV);
4303 return Builder.CreateLoad(PTGV, false, "tmp");
4304}
4305
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004306/// GenerateCategory - Build metadata for a category implementation.
4307/// struct _category_t {
4308/// const char * const name;
4309/// struct _class_t *const cls;
4310/// const struct _method_list_t * const instance_methods;
4311/// const struct _method_list_t * const class_methods;
4312/// const struct _protocol_list_t * const protocols;
4313/// const struct _prop_list_t * const properties;
4314/// }
4315///
4316void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4317{
4318 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004319 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4320 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004321 "_$_" + OCD->getNameAsString());
4322 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
4323
4324 std::vector<llvm::Constant*> Values(6);
4325 Values[0] = GetClassName(OCD->getIdentifier());
4326 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004327 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004328 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004329 std::vector<llvm::Constant*> Methods;
4330 std::string MethodListName(Prefix);
4331 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4332 "_$_" + OCD->getNameAsString();
4333
4334 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
4335 e = OCD->instmeth_end(); i != e; ++i) {
4336 // Instance methods should always be defined.
4337 Methods.push_back(GetMethodConstant(*i));
4338 }
4339
4340 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004341 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004342 Methods);
4343
4344 MethodListName = Prefix;
4345 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4346 OCD->getNameAsString();
4347 Methods.clear();
4348 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
4349 e = OCD->classmeth_end(); i != e; ++i) {
4350 // Class methods should always be defined.
4351 Methods.push_back(GetMethodConstant(*i));
4352 }
4353
4354 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004355 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004356 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004357 const ObjCCategoryDecl *Category =
4358 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004359 if (Category) {
4360 std::string ExtName(Interface->getNameAsString() + "_$_" +
4361 OCD->getNameAsString());
4362 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4363 + Interface->getNameAsString() + "_$_"
4364 + Category->getNameAsString(),
4365 Category->protocol_begin(),
4366 Category->protocol_end());
4367 Values[5] =
4368 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4369 OCD, Category, ObjCTypes);
4370 }
4371 else {
4372 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4373 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4374 }
4375
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004376 llvm::Constant *Init =
4377 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4378 Values);
4379 llvm::GlobalVariable *GCATV
4380 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4381 false,
4382 llvm::GlobalValue::InternalLinkage,
4383 Init,
4384 ExtCatName,
4385 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004386 GCATV->setAlignment(
4387 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004388 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004389 UsedGlobals.push_back(GCATV);
4390 DefinedCategories.push_back(GCATV);
4391}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004392
4393/// GetMethodConstant - Return a struct objc_method constant for the
4394/// given method if it has been defined. The result is null if the
4395/// method has not been defined. The return value has type MethodPtrTy.
4396llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4397 const ObjCMethodDecl *MD) {
4398 // FIXME: Use DenseMap::lookup
4399 llvm::Function *Fn = MethodDefinitions[MD];
4400 if (!Fn)
4401 return 0;
4402
4403 std::vector<llvm::Constant*> Method(3);
4404 Method[0] =
4405 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4406 ObjCTypes.SelectorPtrTy);
4407 Method[1] = GetMethodVarType(MD);
4408 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4409 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4410}
4411
4412/// EmitMethodList - Build meta-data for method declarations
4413/// struct _method_list_t {
4414/// uint32_t entsize; // sizeof(struct _objc_method)
4415/// uint32_t method_count;
4416/// struct _objc_method method_list[method_count];
4417/// }
4418///
4419llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4420 const std::string &Name,
4421 const char *Section,
4422 const ConstantVector &Methods) {
4423 // Return null for empty list.
4424 if (Methods.empty())
4425 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4426
4427 std::vector<llvm::Constant*> Values(3);
4428 // sizeof(struct _objc_method)
4429 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4430 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4431 // method_count
4432 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4433 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4434 Methods.size());
4435 Values[2] = llvm::ConstantArray::get(AT, Methods);
4436 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4437
4438 llvm::GlobalVariable *GV =
4439 new llvm::GlobalVariable(Init->getType(), false,
4440 llvm::GlobalValue::InternalLinkage,
4441 Init,
4442 Name,
4443 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004444 GV->setAlignment(
4445 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004446 GV->setSection(Section);
4447 UsedGlobals.push_back(GV);
4448 return llvm::ConstantExpr::getBitCast(GV,
4449 ObjCTypes.MethodListnfABIPtrTy);
4450}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004451
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004452/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4453/// the given ivar.
4454///
4455llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
4456 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004457 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004458 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004459 Name += "\01_OBJC_IVAR_$_" +
4460 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004461 + Ivar->getNameAsString();
4462 llvm::GlobalVariable *IvarOffsetGV =
4463 CGM.getModule().getGlobalVariable(Name);
4464 if (!IvarOffsetGV)
4465 IvarOffsetGV =
4466 new llvm::GlobalVariable(ObjCTypes.LongTy,
4467 false,
4468 llvm::GlobalValue::ExternalLinkage,
4469 0,
4470 Name,
4471 &CGM.getModule());
4472 return IvarOffsetGV;
4473}
4474
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004475llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004476 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004477 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004478 unsigned long int Offset) {
4479
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004480 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004481 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004482 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004483 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004484
4485 llvm::GlobalVariable *IvarOffsetGV =
4486 CGM.getModule().getGlobalVariable(ExternalName);
4487 if (IvarOffsetGV) {
4488 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004489 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004490 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004491 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004492 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004493 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004494 return IvarOffsetGV;
4495 }
4496
4497 IvarOffsetGV =
4498 new llvm::GlobalVariable(Init->getType(),
4499 false,
4500 llvm::GlobalValue::ExternalLinkage,
4501 Init,
4502 ExternalName,
4503 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004504 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004505 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004506 // @private and @package have hidden visibility.
4507 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4508 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4509 if (!globalVisibility)
4510 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004511 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004512 if (IsClassHidden(ID))
4513 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004514
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004515 IvarOffsetGV->setSection("__DATA, __objc_const");
4516 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004517 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004518}
4519
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004520/// EmitIvarList - Emit the ivar list for the given
4521/// implementation. If ForClass is true the list of class ivars
4522/// (i.e. metaclass ivars) is emitted, otherwise the list of
4523/// interface ivars will be emitted. The return value has type
4524/// IvarListnfABIPtrTy.
4525/// struct _ivar_t {
4526/// unsigned long int *offset; // pointer to ivar offset location
4527/// char *name;
4528/// char *type;
4529/// uint32_t alignment;
4530/// uint32_t size;
4531/// }
4532/// struct _ivar_list_t {
4533/// uint32 entsize; // sizeof(struct _ivar_t)
4534/// uint32 count;
4535/// struct _iver_t list[count];
4536/// }
4537///
4538llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4539 const ObjCImplementationDecl *ID) {
4540
4541 std::vector<llvm::Constant*> Ivars, Ivar(5);
4542
4543 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4544 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4545
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004546 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004547 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004548
4549 RecordDecl::field_iterator i,p;
4550 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004551 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4552
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004553 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004554 FieldDecl *Field = *i;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00004555 uint64_t offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004556 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004557 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004558 if (Field->getIdentifier())
4559 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4560 else
4561 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Devang Patel7794bb82009-03-04 18:21:39 +00004562 Ivar[2] = GetMethodVarType(Field);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004563 const llvm::Type *FieldTy =
4564 CGM.getTypes().ConvertTypeForMem(Field->getType());
4565 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4566 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4567 Field->getType().getTypePtr()) >> 3;
4568 Align = llvm::Log2_32(Align);
4569 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004570 // NOTE. Size of a bitfield does not match gcc's, because of the way
4571 // bitfields are treated special in each. But I am told that 'size'
4572 // for bitfield ivars is ignored by the runtime so it does not matter.
4573 // (even if it matters, some day, there is enough info. to get the bitfield
4574 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004575 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4576 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4577 }
4578 // Return null for empty list.
4579 if (Ivars.empty())
4580 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4581 std::vector<llvm::Constant*> Values(3);
4582 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4583 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4584 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4585 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4586 Ivars.size());
4587 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4588 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4589 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4590 llvm::GlobalVariable *GV =
4591 new llvm::GlobalVariable(Init->getType(), false,
4592 llvm::GlobalValue::InternalLinkage,
4593 Init,
4594 Prefix + OID->getNameAsString(),
4595 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004596 GV->setAlignment(
4597 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004598 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004599
4600 UsedGlobals.push_back(GV);
4601 return llvm::ConstantExpr::getBitCast(GV,
4602 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004603}
4604
4605llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4606 const ObjCProtocolDecl *PD) {
4607 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4608
4609 if (!Entry) {
4610 // We use the initializer as a marker of whether this is a forward
4611 // reference or not. At module finalization we add the empty
4612 // contents for protocols which were referenced but never defined.
4613 Entry =
4614 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4615 llvm::GlobalValue::ExternalLinkage,
4616 0,
4617 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4618 &CGM.getModule());
4619 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4620 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004621 }
4622
4623 return Entry;
4624}
4625
4626/// GetOrEmitProtocol - Generate the protocol meta-data:
4627/// @code
4628/// struct _protocol_t {
4629/// id isa; // NULL
4630/// const char * const protocol_name;
4631/// const struct _protocol_list_t * protocol_list; // super protocols
4632/// const struct method_list_t * const instance_methods;
4633/// const struct method_list_t * const class_methods;
4634/// const struct method_list_t *optionalInstanceMethods;
4635/// const struct method_list_t *optionalClassMethods;
4636/// const struct _prop_list_t * properties;
4637/// const uint32_t size; // sizeof(struct _protocol_t)
4638/// const uint32_t flags; // = 0
4639/// }
4640/// @endcode
4641///
4642
4643llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4644 const ObjCProtocolDecl *PD) {
4645 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4646
4647 // Early exit if a defining object has already been generated.
4648 if (Entry && Entry->hasInitializer())
4649 return Entry;
4650
4651 const char *ProtocolName = PD->getNameAsCString();
4652
4653 // Construct method lists.
4654 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4655 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4656 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4657 e = PD->instmeth_end(); i != e; ++i) {
4658 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004659 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004660 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4661 OptInstanceMethods.push_back(C);
4662 } else {
4663 InstanceMethods.push_back(C);
4664 }
4665 }
4666
4667 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4668 e = PD->classmeth_end(); i != e; ++i) {
4669 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004670 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004671 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4672 OptClassMethods.push_back(C);
4673 } else {
4674 ClassMethods.push_back(C);
4675 }
4676 }
4677
4678 std::vector<llvm::Constant*> Values(10);
4679 // isa is NULL
4680 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4681 Values[1] = GetClassName(PD->getIdentifier());
4682 Values[2] = EmitProtocolList(
4683 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4684 PD->protocol_begin(),
4685 PD->protocol_end());
4686
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004687 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004688 + PD->getNameAsString(),
4689 "__DATA, __objc_const",
4690 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004691 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004692 + PD->getNameAsString(),
4693 "__DATA, __objc_const",
4694 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004695 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004696 + PD->getNameAsString(),
4697 "__DATA, __objc_const",
4698 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004699 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004700 + PD->getNameAsString(),
4701 "__DATA, __objc_const",
4702 OptClassMethods);
4703 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4704 0, PD, ObjCTypes);
4705 uint32_t Size =
4706 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4707 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4708 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4709 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4710 Values);
4711
4712 if (Entry) {
4713 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004714 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004715 Entry->setInitializer(Init);
4716 } else {
4717 Entry =
4718 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004719 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004720 Init,
4721 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4722 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004723 Entry->setAlignment(
4724 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004725 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004726 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004727 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4728
4729 // Use this protocol meta-data to build protocol list table in section
4730 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004731 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004732 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004733 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004734 Entry,
4735 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4736 +ProtocolName,
4737 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004738 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004739 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004740 PTGV->setSection("__DATA, __objc_protolist");
4741 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4742 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004743 return Entry;
4744}
4745
4746/// EmitProtocolList - Generate protocol list meta-data:
4747/// @code
4748/// struct _protocol_list_t {
4749/// long protocol_count; // Note, this is 32/64 bit
4750/// struct _protocol_t[protocol_count];
4751/// }
4752/// @endcode
4753///
4754llvm::Constant *
4755CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4756 ObjCProtocolDecl::protocol_iterator begin,
4757 ObjCProtocolDecl::protocol_iterator end) {
4758 std::vector<llvm::Constant*> ProtocolRefs;
4759
Fariborz Jahanianda320092009-01-29 19:24:30 +00004760 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004761 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004762 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4763
Daniel Dunbar948e2582009-02-15 07:36:20 +00004764 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004765 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4766 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004767 return llvm::ConstantExpr::getBitCast(GV,
4768 ObjCTypes.ProtocolListnfABIPtrTy);
4769
4770 for (; begin != end; ++begin)
4771 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4772
Fariborz Jahanianda320092009-01-29 19:24:30 +00004773 // This list is null terminated.
4774 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004775 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004776
4777 std::vector<llvm::Constant*> Values(2);
4778 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4779 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004780 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004781 ProtocolRefs.size()),
4782 ProtocolRefs);
4783
4784 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4785 GV = new llvm::GlobalVariable(Init->getType(), false,
4786 llvm::GlobalValue::InternalLinkage,
4787 Init,
4788 Name,
4789 &CGM.getModule());
4790 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004791 GV->setAlignment(
4792 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004793 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004794 return llvm::ConstantExpr::getBitCast(GV,
4795 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004796}
4797
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004798/// GetMethodDescriptionConstant - This routine build following meta-data:
4799/// struct _objc_method {
4800/// SEL _cmd;
4801/// char *method_type;
4802/// char *_imp;
4803/// }
4804
4805llvm::Constant *
4806CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4807 std::vector<llvm::Constant*> Desc(3);
4808 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4809 ObjCTypes.SelectorPtrTy);
4810 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004811 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004812 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4813 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4814}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004815
4816/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4817/// This code gen. amounts to generating code for:
4818/// @code
4819/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4820/// @encode
4821///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004822LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004823 CodeGen::CodeGenFunction &CGF,
4824 QualType ObjectTy,
4825 llvm::Value *BaseValue,
4826 const ObjCIvarDecl *Ivar,
4827 const FieldDecl *Field,
4828 unsigned CVRQualifiers) {
4829 assert(ObjectTy->isObjCInterfaceType() &&
4830 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004831 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004832 std::string ExternalName;
4833 llvm::GlobalVariable *IvarOffsetGV =
4834 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004835
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004836 // (char *) BaseValue
4837 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4838 ObjCTypes.Int8PtrTy);
4839 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4840 // (char*)BaseValue + Offset_symbol
4841 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4842 // (type *)((char*)BaseValue + Offset_symbol)
4843 const llvm::Type *IvarTy =
4844 CGM.getTypes().ConvertType(Ivar->getType());
4845 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4846 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004847
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004848 if (Ivar->isBitField()) {
4849 CodeGenTypes::BitFieldInfo bitFieldInfo =
4850 CGM.getTypes().getBitFieldInfo(Field);
4851 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
4852 Field->getType()->isSignedIntegerType(),
4853 Field->getType().getCVRQualifiers()|CVRQualifiers);
4854 }
4855
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004856 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004857 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4858 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004859 LValue::SetObjCIvar(LV, true);
4860 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004861}
4862
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004863llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4864 CodeGen::CodeGenFunction &CGF,
4865 ObjCInterfaceDecl *Interface,
4866 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004867 std::string ExternalName;
4868 llvm::GlobalVariable *IvarOffsetGV =
4869 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4870
4871 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004872}
4873
Fariborz Jahanian46551122009-02-04 00:22:57 +00004874CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4875 CodeGen::CodeGenFunction &CGF,
4876 QualType ResultType,
4877 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004878 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004879 QualType Arg0Ty,
4880 bool IsSuper,
4881 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004882 // FIXME. Even though IsSuper is passes. This function doese not
4883 // handle calls to 'super' receivers.
4884 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004885 llvm::Value *Arg0 = Receiver;
4886 if (!IsSuper)
4887 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004888
4889 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004890 // FIXME. This is too much work to get the ABI-specific result type
4891 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004892 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4893 llvm::SmallVector<QualType, 16>());
4894 llvm::Constant *Fn;
4895 std::string Name("\01l_");
4896 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004897#if 0
4898 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004899 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4900 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4901 // FIXME. Is there a better way of getting these names.
4902 // They are available in RuntimeFunctions vector pair.
4903 Name += "objc_msgSendId_stret_fixup";
4904 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004905 else
4906#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004907 if (IsSuper) {
4908 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4909 Name += "objc_msgSendSuper2_stret_fixup";
4910 }
4911 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004912 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004913 Fn = ObjCTypes.MessageSendStretFixupFn;
4914 Name += "objc_msgSend_stret_fixup";
4915 }
4916 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004917 else if (ResultType->isFloatingType() &&
4918 // Selection of frret API only happens in 32bit nonfragile ABI.
4919 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004920 Fn = ObjCTypes.MessageSendFpretFixupFn;
4921 Name += "objc_msgSend_fpret_fixup";
4922 }
4923 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004924#if 0
4925// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004926 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4927 Fn = ObjCTypes.MessageSendIdFixupFn;
4928 Name += "objc_msgSendId_fixup";
4929 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004930 else
4931#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004932 if (IsSuper) {
4933 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4934 Name += "objc_msgSendSuper2_fixup";
4935 }
4936 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004937 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004938 Fn = ObjCTypes.MessageSendFixupFn;
4939 Name += "objc_msgSend_fixup";
4940 }
4941 }
4942 Name += '_';
4943 std::string SelName(Sel.getAsString());
4944 // Replace all ':' in selector name with '_' ouch!
4945 for(unsigned i = 0; i < SelName.size(); i++)
4946 if (SelName[i] == ':')
4947 SelName[i] = '_';
4948 Name += SelName;
4949 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4950 if (!GV) {
4951 // Build messafe ref table entry.
4952 std::vector<llvm::Constant*> Values(2);
4953 Values[0] = Fn;
4954 Values[1] = GetMethodVarName(Sel);
4955 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4956 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004957 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004958 Init,
4959 Name,
4960 &CGM.getModule());
4961 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4962 GV->setAlignment(
4963 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4964 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4965 UsedGlobals.push_back(GV);
4966 }
4967 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004968
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004969 CallArgList ActualArgs;
4970 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4971 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4972 ObjCTypes.MessageRefCPtrTy));
4973 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004974 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4975 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4976 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004977 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004978 Callee = CGF.Builder.CreateBitCast(Callee,
4979 llvm::PointerType::getUnqual(FTy));
4980 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004981}
4982
4983/// Generate code for a message send expression in the nonfragile abi.
4984CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4985 CodeGen::CodeGenFunction &CGF,
4986 QualType ResultType,
4987 Selector Sel,
4988 llvm::Value *Receiver,
4989 bool IsClassMessage,
4990 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004991 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004992 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004993 false, CallArgs);
4994}
4995
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004996llvm::GlobalVariable *
4997CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
4998 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4999
Daniel Dunbardfff2302009-03-02 05:18:14 +00005000 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005001 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5002 llvm::GlobalValue::ExternalLinkage,
5003 0, Name, &CGM.getModule());
5004 UsedGlobals.push_back(GV);
5005 }
5006
5007 return GV;
5008}
5009
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005010llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005011 const ObjCInterfaceDecl *ID,
5012 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005013
5014 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5015
5016 if (!Entry) {
5017 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005018 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005019 Entry =
5020 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5021 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005022 ClassGV,
5023 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
5024 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005025 &CGM.getModule());
5026 Entry->setAlignment(
5027 CGM.getTargetData().getPrefTypeAlignment(
5028 ObjCTypes.ClassnfABIPtrTy));
5029
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005030 if (IsSuper)
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005031 Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005032 else
Fariborz Jahanian21228b72009-02-26 22:30:39 +00005033 Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005034 UsedGlobals.push_back(Entry);
5035 }
5036
5037 return Builder.CreateLoad(Entry, false, "tmp");
5038}
5039
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005040/// EmitMetaClassRef - Return a Value * of the address of _class_t
5041/// meta-data
5042///
5043llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5044 const ObjCInterfaceDecl *ID) {
5045 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5046 if (Entry)
5047 return Builder.CreateLoad(Entry, false, "tmp");
5048
5049 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
Daniel Dunbar8def7992009-03-01 04:51:18 +00005050 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005051 Entry =
5052 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5053 llvm::GlobalValue::InternalLinkage,
5054 MetaClassGV,
5055 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5056 &CGM.getModule());
5057 Entry->setAlignment(
5058 CGM.getTargetData().getPrefTypeAlignment(
5059 ObjCTypes.ClassnfABIPtrTy));
5060
5061 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
5062 UsedGlobals.push_back(Entry);
5063
5064 return Builder.CreateLoad(Entry, false, "tmp");
5065}
5066
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005067/// GetClass - Return a reference to the class for the given interface
5068/// decl.
5069llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5070 const ObjCInterfaceDecl *ID) {
5071 return EmitClassRef(Builder, ID);
5072}
5073
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005074/// Generates a message send where the super is the receiver. This is
5075/// a message send to self with special delivery semantics indicating
5076/// which class's method should be called.
5077CodeGen::RValue
5078CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5079 QualType ResultType,
5080 Selector Sel,
5081 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005082 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005083 llvm::Value *Receiver,
5084 bool IsClassMessage,
5085 const CodeGen::CallArgList &CallArgs) {
5086 // ...
5087 // Create and init a super structure; this is a (receiver, class)
5088 // pair we will pass to objc_msgSendSuper.
5089 llvm::Value *ObjCSuper =
5090 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5091
5092 llvm::Value *ReceiverAsObject =
5093 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5094 CGF.Builder.CreateStore(ReceiverAsObject,
5095 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5096
5097 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005098 llvm::Value *Target;
5099 if (IsClassMessage) {
5100 if (isCategoryImpl) {
5101 // Message sent to "super' in a class method defined in
5102 // a category implementation.
5103 Target = EmitClassRef(CGF.Builder, Class, false);
5104 Target = CGF.Builder.CreateStructGEP(Target, 0);
5105 Target = CGF.Builder.CreateLoad(Target);
5106 }
5107 else
5108 Target = EmitMetaClassRef(CGF.Builder, Class);
5109 }
5110 else
5111 Target = EmitClassRef(CGF.Builder, Class, true);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005112
5113 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5114 // and ObjCTypes types.
5115 const llvm::Type *ClassTy =
5116 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5117 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5118 CGF.Builder.CreateStore(Target,
5119 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5120
5121 return EmitMessageSend(CGF, ResultType, Sel,
5122 ObjCSuper, ObjCTypes.SuperPtrCTy,
5123 true, CallArgs);
5124}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005125
5126llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5127 Selector Sel) {
5128 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5129
5130 if (!Entry) {
5131 llvm::Constant *Casted =
5132 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5133 ObjCTypes.SelectorPtrTy);
5134 Entry =
5135 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5136 llvm::GlobalValue::InternalLinkage,
5137 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5138 &CGM.getModule());
5139 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5140 UsedGlobals.push_back(Entry);
5141 }
5142
5143 return Builder.CreateLoad(Entry, false, "tmp");
5144}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005145/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5146/// objc_assign_ivar (id src, id *dst)
5147///
5148void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5149 llvm::Value *src, llvm::Value *dst)
5150{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005151 const llvm::Type * SrcTy = src->getType();
5152 if (!isa<llvm::PointerType>(SrcTy)) {
5153 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5154 assert(Size <= 8 && "does not support size > 8");
5155 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5156 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005157 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5158 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005159 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5160 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5161 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
5162 src, dst, "assignivar");
5163 return;
5164}
5165
5166/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5167/// objc_assign_strongCast (id src, id *dst)
5168///
5169void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5170 CodeGen::CodeGenFunction &CGF,
5171 llvm::Value *src, llvm::Value *dst)
5172{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005173 const llvm::Type * SrcTy = src->getType();
5174 if (!isa<llvm::PointerType>(SrcTy)) {
5175 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5176 assert(Size <= 8 && "does not support size > 8");
5177 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5178 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005179 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5180 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005181 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5182 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5183 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
5184 src, dst, "weakassign");
5185 return;
5186}
5187
5188/// EmitObjCWeakRead - Code gen for loading value of a __weak
5189/// object: objc_read_weak (id *src)
5190///
5191llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5192 CodeGen::CodeGenFunction &CGF,
5193 llvm::Value *AddrWeakObj)
5194{
Eli Friedman8339b352009-03-07 03:57:15 +00005195 const llvm::Type* DestTy =
5196 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005197 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
5198 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
5199 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005200 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005201 return read_weak;
5202}
5203
5204/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5205/// objc_assign_weak (id src, id *dst)
5206///
5207void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5208 llvm::Value *src, llvm::Value *dst)
5209{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005210 const llvm::Type * SrcTy = src->getType();
5211 if (!isa<llvm::PointerType>(SrcTy)) {
5212 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5213 assert(Size <= 8 && "does not support size > 8");
5214 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5215 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005216 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5217 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005218 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5219 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5220 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
5221 src, dst, "weakassign");
5222 return;
5223}
5224
5225/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5226/// objc_assign_global (id src, id *dst)
5227///
5228void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5229 llvm::Value *src, llvm::Value *dst)
5230{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005231 const llvm::Type * SrcTy = src->getType();
5232 if (!isa<llvm::PointerType>(SrcTy)) {
5233 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5234 assert(Size <= 8 && "does not support size > 8");
5235 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5236 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005237 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5238 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005239 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5240 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5241 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
5242 src, dst, "globalassign");
5243 return;
5244}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005245
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005246void
5247CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5248 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005249 bool isTry = isa<ObjCAtTryStmt>(S);
5250 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5251 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005252 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005253 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005254 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005255 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5256
5257 // For @synchronized, call objc_sync_enter(sync.expr). The
5258 // evaluation of the expression must occur before we enter the
5259 // @synchronized. We can safely avoid a temp here because jumps into
5260 // @synchronized are illegal & this will dominate uses.
5261 llvm::Value *SyncArg = 0;
5262 if (!isTry) {
5263 SyncArg =
5264 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5265 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5266 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg);
5267 }
5268
5269 // Push an EH context entry, used for handling rethrows and jumps
5270 // through finally.
5271 CGF.PushCleanupBlock(FinallyBlock);
5272
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005273 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005274
5275 CGF.EmitBlock(TryBlock);
5276 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5277 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5278 CGF.EmitBranchThroughCleanup(FinallyEnd);
5279
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005280 // Emit the exception handler.
5281
5282 CGF.EmitBlock(TryHandler);
5283
5284 llvm::Value *llvm_eh_exception =
5285 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5286 llvm::Value *llvm_eh_selector_i64 =
5287 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5288 llvm::Value *llvm_eh_typeid_for_i64 =
5289 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5290 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5291 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5292
5293 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5294 SelectorArgs.push_back(Exc);
5295 SelectorArgs.push_back(ObjCTypes.EHPersonalityPtr);
5296
5297 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005298 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005299 bool HasCatchAll = false;
5300 if (isTry) {
5301 if (const ObjCAtCatchStmt* CatchStmt =
5302 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5303 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005304 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005305 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005306
5307 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005308 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005309 // Use i8* null here to signal this is a catch all, not a cleanup.
5310 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5311 SelectorArgs.push_back(Null);
5312 HasCatchAll = true;
5313 break;
5314 }
5315
Daniel Dunbarede8de92009-03-06 00:01:21 +00005316 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5317 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005318 llvm::Value *IDEHType =
5319 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5320 if (!IDEHType)
5321 IDEHType =
5322 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5323 llvm::GlobalValue::ExternalLinkage,
5324 0, "OBJC_EHTYPE_id", &CGM.getModule());
5325 SelectorArgs.push_back(IDEHType);
5326 HasCatchAll = true;
5327 break;
5328 }
5329
5330 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005331 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005332 assert(PT && "Invalid @catch type.");
5333 const ObjCInterfaceType *IT =
5334 PT->getPointeeType()->getAsObjCInterfaceType();
5335 assert(IT && "Invalid @catch type.");
5336 llvm::Value *EHType = GetInterfaceEHType(IT);
5337 SelectorArgs.push_back(EHType);
5338 }
5339 }
5340 }
5341
5342 // We use a cleanup unless there was already a catch all.
5343 if (!HasCatchAll) {
5344 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005345 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005346 }
5347
5348 llvm::Value *Selector =
5349 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5350 SelectorArgs.begin(), SelectorArgs.end(),
5351 "selector");
5352 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005353 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005354 const Stmt *CatchBody = Handlers[i].second;
5355
5356 llvm::BasicBlock *Next = 0;
5357
5358 // The last handler always matches.
5359 if (i + 1 != e) {
5360 assert(CatchParam && "Only last handler can be a catch all.");
5361
5362 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5363 Next = CGF.createBasicBlock("catch.next");
5364 llvm::Value *Id =
5365 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5366 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5367 ObjCTypes.Int8PtrTy));
5368 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5369 Match, Next);
5370
5371 CGF.EmitBlock(Match);
5372 }
5373
5374 if (CatchBody) {
5375 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5376 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5377
5378 // Cleanups must call objc_end_catch.
5379 //
5380 // FIXME: It seems incorrect for objc_begin_catch to be inside
5381 // this context, but this matches gcc.
5382 CGF.PushCleanupBlock(MatchEnd);
5383 CGF.setInvokeDest(MatchHandler);
5384
5385 llvm::Value *ExcObject =
5386 CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc);
5387
5388 // Bind the catch parameter if it exists.
5389 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005390 ExcObject =
5391 CGF.Builder.CreateBitCast(ExcObject,
5392 CGF.ConvertType(CatchParam->getType()));
5393 // CatchParam is a ParmVarDecl because of the grammar
5394 // construction used to handle this, but for codegen purposes
5395 // we treat this as a local decl.
5396 CGF.EmitLocalBlockVarDecl(*CatchParam);
5397 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005398 }
5399
5400 CGF.ObjCEHValueStack.push_back(ExcObject);
5401 CGF.EmitStmt(CatchBody);
5402 CGF.ObjCEHValueStack.pop_back();
5403
5404 CGF.EmitBranchThroughCleanup(FinallyEnd);
5405
5406 CGF.EmitBlock(MatchHandler);
5407
5408 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5409 // We are required to emit this call to satisfy LLVM, even
5410 // though we don't use the result.
5411 llvm::SmallVector<llvm::Value*, 8> Args;
5412 Args.push_back(Exc);
5413 Args.push_back(ObjCTypes.EHPersonalityPtr);
5414 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5415 0));
5416 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5417 CGF.Builder.CreateStore(Exc, RethrowPtr);
5418 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5419
5420 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5421
5422 CGF.EmitBlock(MatchEnd);
5423
5424 // Unfortunately, we also have to generate another EH frame here
5425 // in case this throws.
5426 llvm::BasicBlock *MatchEndHandler =
5427 CGF.createBasicBlock("match.end.handler");
5428 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5429 CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn,
5430 Cont, MatchEndHandler,
5431 Args.begin(), Args.begin());
5432
5433 CGF.EmitBlock(Cont);
5434 if (Info.SwitchBlock)
5435 CGF.EmitBlock(Info.SwitchBlock);
5436 if (Info.EndBlock)
5437 CGF.EmitBlock(Info.EndBlock);
5438
5439 CGF.EmitBlock(MatchEndHandler);
5440 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5441 // We are required to emit this call to satisfy LLVM, even
5442 // though we don't use the result.
5443 Args.clear();
5444 Args.push_back(Exc);
5445 Args.push_back(ObjCTypes.EHPersonalityPtr);
5446 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5447 0));
5448 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5449 CGF.Builder.CreateStore(Exc, RethrowPtr);
5450 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5451
5452 if (Next)
5453 CGF.EmitBlock(Next);
5454 } else {
5455 assert(!Next && "catchup should be last handler.");
5456
5457 CGF.Builder.CreateStore(Exc, RethrowPtr);
5458 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5459 }
5460 }
5461
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005462 // Pop the cleanup entry, the @finally is outside this cleanup
5463 // scope.
5464 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5465 CGF.setInvokeDest(PrevLandingPad);
5466
5467 CGF.EmitBlock(FinallyBlock);
5468
5469 if (isTry) {
5470 if (const ObjCAtFinallyStmt* FinallyStmt =
5471 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5472 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5473 } else {
5474 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5475 // @synchronized.
5476 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005477 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005478
5479 if (Info.SwitchBlock)
5480 CGF.EmitBlock(Info.SwitchBlock);
5481 if (Info.EndBlock)
5482 CGF.EmitBlock(Info.EndBlock);
5483
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005484 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005485 CGF.EmitBranch(FinallyEnd);
5486
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005487 CGF.EmitBlock(FinallyRethrow);
5488 CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn,
5489 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005490 CGF.Builder.CreateUnreachable();
5491
5492 CGF.EmitBlock(FinallyEnd);
5493}
5494
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005495/// EmitThrowStmt - Generate code for a throw statement.
5496void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5497 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005498 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005499 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005500 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005501 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005502 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5503 "Unexpected rethrow outside @catch block.");
5504 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005505 }
5506
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005507 llvm::Value *ExceptionAsObject =
5508 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5509 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5510 if (InvokeDest) {
5511 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5512 CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn,
5513 Cont, InvokeDest,
5514 &ExceptionAsObject, &ExceptionAsObject + 1);
5515 CGF.EmitBlock(Cont);
5516 } else
5517 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
5518 CGF.Builder.CreateUnreachable();
5519
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005520 // Clear the insertion point to indicate we are in unreachable code.
5521 CGF.Builder.ClearInsertionPoint();
5522}
Daniel Dunbare588b992009-03-01 04:46:24 +00005523
5524llvm::Value *
5525CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceType *IT) {
5526 const ObjCInterfaceDecl *ID = IT->getDecl();
5527 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
5528 if (Entry)
5529 return Entry;
5530
5531 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
5532 std::string VTableName = "objc_ehtype_vtable";
5533 llvm::GlobalVariable *VTableGV =
5534 CGM.getModule().getGlobalVariable(VTableName);
5535 if (!VTableGV)
5536 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5537 llvm::GlobalValue::ExternalLinkage,
5538 0, VTableName, &CGM.getModule());
5539
5540 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5541
5542 std::vector<llvm::Constant*> Values(3);
5543 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5544 Values[1] = GetClassName(ID->getIdentifier());
5545 Values[2] = GetClassGlobal(ClassName);
5546 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5547
5548 Entry =
5549 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00005550 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbare588b992009-03-01 04:46:24 +00005551 Init,
5552 (std::string("OBJC_EHTYPE_$_") +
5553 ID->getIdentifier()->getName()),
5554 &CGM.getModule());
5555
5556 return Entry;
5557}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005558
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005559/* *** */
5560
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005561CodeGen::CGObjCRuntime *
5562CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005563 return new CGObjCMac(CGM);
5564}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005565
5566CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005567CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005568 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005569}