blob: e987261daa6aac618329cd1c23880e34dfe44794 [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 Lattner96508e12009-04-17 22:12:36 +000095 llvm::Constant *getGcAssignWeakFn() {
96 // id objc_assign_weak (id, id *)
97 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
98 Args.push_back(ObjectPtrTy->getPointerTo());
99 llvm::FunctionType *FTy =
100 llvm::FunctionType::get(ObjectPtrTy, Args, false);
101 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
102 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000103
104 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner74391b42009-03-22 21:03:39 +0000105 llvm::Constant *GcAssignGlobalFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000106
107 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner74391b42009-03-22 21:03:39 +0000108 llvm::Constant *GcAssignIvarFn;
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000109
110 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner74391b42009-03-22 21:03:39 +0000111 llvm::Constant *GcAssignStrongCastFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000112
113 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner74391b42009-03-22 21:03:39 +0000114 llvm::Constant *ExceptionThrowFn;
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000115
Daniel Dunbar1c566672009-02-24 01:43:46 +0000116 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000117 llvm::Constant *getSyncEnterFn() {
118 // void objc_sync_enter (id)
119 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
120 llvm::FunctionType *FTy =
121 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
122 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
123 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000124
125 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000126 llvm::Constant *SyncExitFn;
Daniel Dunbar1c566672009-02-24 01:43:46 +0000127
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000128 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
129 ~ObjCCommonTypesHelper(){}
130};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000131
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000132/// ObjCTypesHelper - Helper class that encapsulates lazy
133/// construction of varies types used during ObjC generation.
134class ObjCTypesHelper : public ObjCCommonTypesHelper {
135private:
136
Chris Lattner74391b42009-03-22 21:03:39 +0000137 llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
138 llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn,
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000139 *MessageSendSuperFpretFn;
140
141public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000142 /// SymtabTy - LLVM type for struct objc_symtab.
143 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000144 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
145 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000146 /// ModuleTy - LLVM type for struct objc_module.
147 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000148
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000149 /// ProtocolTy - LLVM type for struct objc_protocol.
150 const llvm::StructType *ProtocolTy;
151 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
152 const llvm::Type *ProtocolPtrTy;
153 /// ProtocolExtensionTy - LLVM type for struct
154 /// objc_protocol_extension.
155 const llvm::StructType *ProtocolExtensionTy;
156 /// ProtocolExtensionTy - LLVM type for struct
157 /// objc_protocol_extension *.
158 const llvm::Type *ProtocolExtensionPtrTy;
159 /// MethodDescriptionTy - LLVM type for struct
160 /// objc_method_description.
161 const llvm::StructType *MethodDescriptionTy;
162 /// MethodDescriptionListTy - LLVM type for struct
163 /// objc_method_description_list.
164 const llvm::StructType *MethodDescriptionListTy;
165 /// MethodDescriptionListPtrTy - LLVM type for struct
166 /// objc_method_description_list *.
167 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000168 /// ProtocolListTy - LLVM type for struct objc_property_list.
169 const llvm::Type *ProtocolListTy;
170 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
171 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000172 /// CategoryTy - LLVM type for struct objc_category.
173 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000174 /// ClassTy - LLVM type for struct objc_class.
175 const llvm::StructType *ClassTy;
176 /// ClassPtrTy - LLVM type for struct objc_class *.
177 const llvm::Type *ClassPtrTy;
178 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
179 const llvm::StructType *ClassExtensionTy;
180 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
181 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000182 // IvarTy - LLVM type for struct objc_ivar.
183 const llvm::StructType *IvarTy;
184 /// IvarListTy - LLVM type for struct objc_ivar_list.
185 const llvm::Type *IvarListTy;
186 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
187 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000188 /// MethodListTy - LLVM type for struct objc_method_list.
189 const llvm::Type *MethodListTy;
190 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
191 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000192
193 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
194 const llvm::Type *ExceptionDataTy;
195
Anders Carlsson124526b2008-09-09 10:10:21 +0000196 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner74391b42009-03-22 21:03:39 +0000197 llvm::Constant *ExceptionTryEnterFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000198
199 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner74391b42009-03-22 21:03:39 +0000200 llvm::Constant *ExceptionTryExitFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000201
202 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner74391b42009-03-22 21:03:39 +0000203 llvm::Constant *ExceptionExtractFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000204
205 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner74391b42009-03-22 21:03:39 +0000206 llvm::Constant *ExceptionMatchFn;
Anders Carlsson124526b2008-09-09 10:10:21 +0000207
208 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner74391b42009-03-22 21:03:39 +0000209 llvm::Constant *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000210
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000211public:
212 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000213 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000214
215
Chris Lattner74391b42009-03-22 21:03:39 +0000216 llvm::Constant *getSendFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000217 return IsSuper ? MessageSendSuperFn : MessageSendFn;
218 }
219
Chris Lattner74391b42009-03-22 21:03:39 +0000220 llvm::Constant *getSendStretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000221 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
222 }
223
Chris Lattner74391b42009-03-22 21:03:39 +0000224 llvm::Constant *getSendFpretFn(bool IsSuper) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000225 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
226 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000227};
228
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000229/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000230/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000231class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000232public:
Chris Lattner74391b42009-03-22 21:03:39 +0000233 llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000234 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
235 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
236 *MessageSendSuper2StretFixupFn;
237
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000238 // MethodListnfABITy - LLVM for struct _method_list_t
239 const llvm::StructType *MethodListnfABITy;
240
241 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
242 const llvm::Type *MethodListnfABIPtrTy;
243
244 // ProtocolnfABITy = LLVM for struct _protocol_t
245 const llvm::StructType *ProtocolnfABITy;
246
Daniel Dunbar948e2582009-02-15 07:36:20 +0000247 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
248 const llvm::Type *ProtocolnfABIPtrTy;
249
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000250 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
251 const llvm::StructType *ProtocolListnfABITy;
252
253 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
254 const llvm::Type *ProtocolListnfABIPtrTy;
255
256 // ClassnfABITy - LLVM for struct _class_t
257 const llvm::StructType *ClassnfABITy;
258
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000259 // ClassnfABIPtrTy - LLVM for struct _class_t*
260 const llvm::Type *ClassnfABIPtrTy;
261
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000262 // IvarnfABITy - LLVM for struct _ivar_t
263 const llvm::StructType *IvarnfABITy;
264
265 // IvarListnfABITy - LLVM for struct _ivar_list_t
266 const llvm::StructType *IvarListnfABITy;
267
268 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
269 const llvm::Type *IvarListnfABIPtrTy;
270
271 // ClassRonfABITy - LLVM for struct _class_ro_t
272 const llvm::StructType *ClassRonfABITy;
273
274 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
275 const llvm::Type *ImpnfABITy;
276
277 // CategorynfABITy - LLVM for struct _category_t
278 const llvm::StructType *CategorynfABITy;
279
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000280 // New types for nonfragile abi messaging.
281
282 // MessageRefTy - LLVM for:
283 // struct _message_ref_t {
284 // IMP messenger;
285 // SEL name;
286 // };
287 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000288 // MessageRefCTy - clang type for struct _message_ref_t
289 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000290
291 // MessageRefPtrTy - LLVM for struct _message_ref_t*
292 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000293 // MessageRefCPtrTy - clang type for struct _message_ref_t*
294 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000295
Fariborz Jahanianef163782009-02-05 01:13:09 +0000296 // MessengerTy - Type of the messenger (shown as IMP above)
297 const llvm::FunctionType *MessengerTy;
298
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000299 // SuperMessageRefTy - LLVM for:
300 // struct _super_message_ref_t {
301 // SUPER_IMP messenger;
302 // SEL name;
303 // };
304 const llvm::StructType *SuperMessageRefTy;
305
306 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
307 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000308
309 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
310 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000311 llvm::Value *getEHPersonalityPtr() {
312 llvm::Constant *Personality =
313 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
314 std::vector<const llvm::Type*>(),
315 true),
316 "__objc_personality_v0");
317 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
318 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000319
Chris Lattner8a569112009-04-22 02:15:23 +0000320 llvm::Constant *getUnwindResumeOrRethrowFn() {
321 std::vector<const llvm::Type*> Params;
322 Params.push_back(Int8PtrTy);
323 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
324 Params, false),
325 "_Unwind_Resume_or_Rethrow");
326 }
327
328 llvm::Constant *getObjCEndCatchFn() {
329 std::vector<const llvm::Type*> Params;
330 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
331 Params, false),
332 "objc_end_catch");
333
334 }
335
336 llvm::Constant *getObjCBeginCatchFn() {
337 std::vector<const llvm::Type*> Params;
338 Params.push_back(Int8PtrTy);
339 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
340 Params, false),
341 "objc_begin_catch");
342 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000343
344 const llvm::StructType *EHTypeTy;
345 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000346
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000347 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
348 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349};
350
351class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000352public:
353 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000354 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000355 public:
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000356 unsigned int ivar_bytepos;
357 unsigned int ivar_size;
358 GC_IVAR() : ivar_bytepos(0), ivar_size(0) {}
359 };
360
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000361 class SKIP_SCAN {
362 public:
363 unsigned int skip;
364 unsigned int scan;
365 SKIP_SCAN() : skip(0), scan(0) {}
366 };
367
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000368protected:
369 CodeGen::CodeGenModule &CGM;
370 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000371 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000372
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000373 // gc ivar layout bitmap calculation helper caches.
374 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
375 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
376 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000377
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000378 /// LazySymbols - Symbols to generate a lazy reference for. See
379 /// DefinedSymbols and FinishModule().
380 std::set<IdentifierInfo*> LazySymbols;
381
382 /// DefinedSymbols - External symbols which are defined by this
383 /// module. The symbols in this list and LazySymbols are used to add
384 /// special linker symbols which ensure that Objective-C modules are
385 /// linked properly.
386 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000387
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000388 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000389 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000390
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000391 /// MethodVarNames - uniqued method variable names.
392 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000393
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000394 /// MethodVarTypes - uniqued method type signatures. We have to use
395 /// a StringMap here because have no other unique reference.
396 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000397
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000398 /// MethodDefinitions - map of methods which have been defined in
399 /// this translation unit.
400 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000401
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000402 /// PropertyNames - uniqued method variable names.
403 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000404
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000405 /// ClassReferences - uniqued class references.
406 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000407
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000408 /// SelectorReferences - uniqued selector references.
409 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000410
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000411 /// Protocols - Protocols for which an objc_protocol structure has
412 /// been emitted. Forward declarations are handled by creating an
413 /// empty structure whose initializer is filled in when/if defined.
414 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000415
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000416 /// DefinedProtocols - Protocols which have actually been
417 /// defined. We should not need this, see FIXME in GenerateProtocol.
418 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000419
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000420 /// DefinedClasses - List of defined classes.
421 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000422
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000423 /// DefinedCategories - List of defined categories.
424 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000425
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000426 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000427 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000428 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000429
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000430 /// GetNameForMethod - Return a name for the given method.
431 /// \param[out] NameOut - The return value.
432 void GetNameForMethod(const ObjCMethodDecl *OMD,
433 const ObjCContainerDecl *CD,
434 std::string &NameOut);
435
436 /// GetMethodVarName - Return a unique constant for the given
437 /// selector's name. The return value has type char *.
438 llvm::Constant *GetMethodVarName(Selector Sel);
439 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
440 llvm::Constant *GetMethodVarName(const std::string &Name);
441
442 /// GetMethodVarType - Return a unique constant for the given
443 /// selector's name. The return value has type char *.
444
445 // FIXME: This is a horrible name.
446 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000447 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000448
449 /// GetPropertyName - Return a unique constant for the given
450 /// name. The return value has type char *.
451 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
452
453 // FIXME: This can be dropped once string functions are unified.
454 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
455 const Decl *Container);
456
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000457 /// GetClassName - Return a unique constant for the given selector's
458 /// name. The return value has type char *.
459 llvm::Constant *GetClassName(IdentifierInfo *Ident);
460
Fariborz Jahanian21e6f172009-03-11 21:42:00 +0000461 /// GetInterfaceDeclStructLayout - Get layout for ivars of given
462 /// interface declaration.
463 const llvm::StructLayout *GetInterfaceDeclStructLayout(
464 const ObjCInterfaceDecl *ID) const;
465
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000466 /// BuildIvarLayout - Builds ivar layout bitmap for the class
467 /// implementation for the __strong or __weak case.
468 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000469 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
470 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000471
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000472 void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
473 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000474 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000475 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000476 unsigned int BytePos, bool ForStrongLayout,
477 int &Index, int &SkIndex, bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000478
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000479 /// GetIvarLayoutName - Returns a unique constant for the given
480 /// ivar layout bitmap.
481 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
482 const ObjCCommonTypesHelper &ObjCTypes);
483
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000484 /// EmitPropertyList - Emit the given property list. The return
485 /// value has type PropertyListPtrTy.
486 llvm::Constant *EmitPropertyList(const std::string &Name,
487 const Decl *Container,
488 const ObjCContainerDecl *OCD,
489 const ObjCCommonTypesHelper &ObjCTypes);
490
Fariborz Jahanianda320092009-01-29 19:24:30 +0000491 /// GetProtocolRef - Return a reference to the internal protocol
492 /// description, creating an empty one if it has not been
493 /// defined. The return value has type ProtocolPtrTy.
494 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000495
496 /// GetIvarBaseOffset - returns ivars byte offset.
497 uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000498 const FieldDecl *Field);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000499
Chris Lattnercd0ee142009-03-31 08:33:16 +0000500 /// GetFieldBaseOffset - return's field byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000501 uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
502 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +0000503 const FieldDecl *Field);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000504
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000505 /// CreateMetadataVar - Create a global variable with internal
506 /// linkage for use by the Objective-C runtime.
507 ///
508 /// This is a convenience wrapper which not only creates the
509 /// variable, but also sets the section and alignment and adds the
510 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000511 ///
512 /// \param Name - The variable name.
513 /// \param Init - The variable initializer; this is also used to
514 /// define the type of the variable.
515 /// \param Section - The section the variable should go into, or 0.
516 /// \param Align - The alignment for the variable, or 0.
517 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000518 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000519 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
520 llvm::Constant *Init,
521 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000522 unsigned Align,
523 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000524
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000525 /// GetNamedIvarList - Return the list of ivars in the interface
526 /// itself (not including super classes and not including unnamed
527 /// bitfields).
528 ///
529 /// For the non-fragile ABI, this also includes synthesized property
530 /// ivars.
531 void GetNamedIvarList(const ObjCInterfaceDecl *OID,
532 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const;
533
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000534public:
535 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
536 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000537
Steve Naroff33fdb732009-03-31 16:53:37 +0000538 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000539
540 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
541 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000542
543 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
544
545 /// GetOrEmitProtocol - Get the protocol object for the given
546 /// declaration, emitting it if necessary. The return value has type
547 /// ProtocolPtrTy.
548 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
549
550 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
551 /// object for the given declaration, emitting it if needed. These
552 /// forward references will be filled in with empty bodies if no
553 /// definition is seen. The return value has type ProtocolPtrTy.
554 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000555};
556
557class CGObjCMac : public CGObjCCommonMac {
558private:
559 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000560 /// EmitImageInfo - Emit the image info marker used to encode some module
561 /// level information.
562 void EmitImageInfo();
563
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000564 /// EmitModuleInfo - Another marker encoding module level
565 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000566 void EmitModuleInfo();
567
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000568 /// EmitModuleSymols - Emit module symbols, the list of defined
569 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000570 llvm::Constant *EmitModuleSymbols();
571
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000572 /// FinishModule - Write out global data structures at the end of
573 /// processing a translation unit.
574 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000575
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000576 /// EmitClassExtension - Generate the class extension structure used
577 /// to store the weak ivar layout and properties. The return value
578 /// has type ClassExtensionPtrTy.
579 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
580
581 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
582 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000583 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000584 const ObjCInterfaceDecl *ID);
585
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000586 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000587 QualType ResultType,
588 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000589 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000590 QualType Arg0Ty,
591 bool IsSuper,
592 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000593
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000594 /// EmitIvarList - Emit the ivar list for the given
595 /// implementation. If ForClass is true the list of class ivars
596 /// (i.e. metaclass ivars) is emitted, otherwise the list of
597 /// interface ivars will be emitted. The return value has type
598 /// IvarListPtrTy.
599 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000600 bool ForClass);
601
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000602 /// EmitMetaClass - Emit a forward reference to the class structure
603 /// for the metaclass of the given interface. The return value has
604 /// type ClassPtrTy.
605 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
606
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000607 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000608 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000609 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
610 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000611 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000612 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000613
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000614 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000615
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000616 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000617
618 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000619 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000620 llvm::Constant *EmitMethodList(const std::string &Name,
621 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000622 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000623
624 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000625 /// method declarations.
626 /// - TypeName: The name for the type containing the methods.
627 /// - IsProtocol: True iff these methods are for a protocol.
628 /// - ClassMethds: True iff these are class methods.
629 /// - Required: When true, only "required" methods are
630 /// listed. Similarly, when false only "optional" methods are
631 /// listed. For classes this should always be true.
632 /// - begin, end: The method list to output.
633 ///
634 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000635 llvm::Constant *EmitMethodDescList(const std::string &Name,
636 const char *Section,
637 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000638
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000639 /// GetOrEmitProtocol - Get the protocol object for the given
640 /// declaration, emitting it if necessary. The return value has type
641 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000642 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000643
644 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
645 /// object for the given declaration, emitting it if needed. These
646 /// forward references will be filled in with empty bodies if no
647 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000648 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000649
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000650 /// EmitProtocolExtension - Generate the protocol extension
651 /// structure used to store optional instance and class methods, and
652 /// protocol properties. The return value has type
653 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000654 llvm::Constant *
655 EmitProtocolExtension(const ObjCProtocolDecl *PD,
656 const ConstantVector &OptInstanceMethods,
657 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000658
659 /// EmitProtocolList - Generate the list of referenced
660 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +0000661 llvm::Constant *EmitProtocolList(const std::string &Name,
662 ObjCProtocolDecl::protocol_iterator begin,
663 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000664
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000665 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
666 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000667 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000668
Fariborz Jahanianda320092009-01-29 19:24:30 +0000669 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000670 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000671
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000672 virtual llvm::Function *ModuleInitFunction();
673
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000674 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000675 QualType ResultType,
676 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000677 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000678 bool IsClassMessage,
679 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000680
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000681 virtual CodeGen::RValue
682 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000683 QualType ResultType,
684 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000685 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000686 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000687 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000688 bool IsClassMessage,
689 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000690
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000691 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000692 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000693
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000694 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000695
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000696 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000697
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000698 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000699
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000700 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000701 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000702
Chris Lattner74391b42009-03-22 21:03:39 +0000703 virtual llvm::Constant *GetPropertyGetFunction();
704 virtual llvm::Constant *GetPropertySetFunction();
705 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000706
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000707 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
708 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000709 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
710 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000711 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000712 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000713 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
714 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000715 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
716 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000717 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
718 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000719 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
720 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000721
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000722 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
723 QualType ObjectTy,
724 llvm::Value *BaseValue,
725 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000726 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000727 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
728 ObjCInterfaceDecl *Interface,
729 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000730};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000731
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000732class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000733private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000734 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000735 llvm::GlobalVariable* ObjCEmptyCacheVar;
736 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000737
Daniel Dunbar11394522009-04-18 08:51:00 +0000738 /// SuperClassReferences - uniqued super class references.
739 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
740
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000741 /// MetaClassReferences - uniqued meta class references.
742 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +0000743
744 /// EHTypeReferences - uniqued class ehtype references.
745 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000746
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000747 /// FinishNonFragileABIModule - Write out global data structures at the end of
748 /// processing a translation unit.
749 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000750
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000751 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
752 unsigned InstanceStart,
753 unsigned InstanceSize,
754 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000755 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
756 llvm::Constant *IsAGV,
757 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000758 llvm::Constant *ClassRoGV,
759 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000760
761 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
762
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000763 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
764
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000765 /// EmitMethodList - Emit the method list for the given
766 /// implementation. The return value has type MethodListnfABITy.
767 llvm::Constant *EmitMethodList(const std::string &Name,
768 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000769 const ConstantVector &Methods);
770 /// EmitIvarList - Emit the ivar list for the given
771 /// implementation. If ForClass is true the list of class ivars
772 /// (i.e. metaclass ivars) is emitted, otherwise the list of
773 /// interface ivars will be emitted. The return value has type
774 /// IvarListnfABIPtrTy.
775 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000776
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000777 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000778 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000779 unsigned long int offset);
780
Fariborz Jahanianda320092009-01-29 19:24:30 +0000781 /// GetOrEmitProtocol - Get the protocol object for the given
782 /// declaration, emitting it if necessary. The return value has type
783 /// ProtocolPtrTy.
784 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
785
786 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
787 /// object for the given declaration, emitting it if needed. These
788 /// forward references will be filled in with empty bodies if no
789 /// definition is seen. The return value has type ProtocolPtrTy.
790 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
791
792 /// EmitProtocolList - Generate the list of referenced
793 /// protocols. The return value has type ProtocolListPtrTy.
794 llvm::Constant *EmitProtocolList(const std::string &Name,
795 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000796 ObjCProtocolDecl::protocol_iterator end);
797
798 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
799 QualType ResultType,
800 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000801 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000802 QualType Arg0Ty,
803 bool IsSuper,
804 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +0000805
806 /// GetClassGlobal - Return the global variable for the Objective-C
807 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +0000808 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
809
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000810 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +0000811 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000812 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +0000813 const ObjCInterfaceDecl *ID);
814
815 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
816 /// for the given super class reference.
817 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
818 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000819
820 /// EmitMetaClassRef - Return a Value * of the address of _class_t
821 /// meta-data
822 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
823 const ObjCInterfaceDecl *ID);
824
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000825 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
826 /// the given ivar.
827 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +0000828 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000829 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000830 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000831
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000832 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
833 /// for the given selector.
834 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +0000835
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000836 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +0000837 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000838 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
839 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000840
841 const char *getMetaclassSymbolPrefix() const {
842 return "OBJC_METACLASS_$_";
843 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +0000844
Daniel Dunbar6ab187a2009-04-07 05:48:37 +0000845 const char *getClassSymbolPrefix() const {
846 return "OBJC_CLASS_$_";
847 }
848
Daniel Dunbarb02532a2009-04-19 23:41:48 +0000849 void GetClassSizeInfo(const ObjCInterfaceDecl *OID,
850 uint32_t &InstanceStart,
851 uint32_t &InstanceSize);
852
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000853public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000854 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000855 // FIXME. All stubs for now!
856 virtual llvm::Function *ModuleInitFunction();
857
858 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
859 QualType ResultType,
860 Selector Sel,
861 llvm::Value *Receiver,
862 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000863 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000864
865 virtual CodeGen::RValue
866 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
867 QualType ResultType,
868 Selector Sel,
869 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000870 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000871 llvm::Value *Receiver,
872 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000873 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000874
875 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000876 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000877
878 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000879 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000880
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000881 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000882
883 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000884 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000885 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000886
Chris Lattner74391b42009-03-22 21:03:39 +0000887 virtual llvm::Constant *GetPropertyGetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000888 return ObjCTypes.GetPropertyFn;
889 }
Chris Lattner74391b42009-03-22 21:03:39 +0000890 virtual llvm::Constant *GetPropertySetFunction() {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000891 return ObjCTypes.SetPropertyFn;
892 }
Chris Lattner74391b42009-03-22 21:03:39 +0000893 virtual llvm::Constant *EnumerationMutationFunction() {
Daniel Dunbar28ed0842009-02-16 18:48:45 +0000894 return ObjCTypes.EnumerationMutationFn;
895 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000896
897 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000898 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000899 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000900 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000901 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000902 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000903 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000904 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000905 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000906 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000907 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000908 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000909 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +0000910 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000911 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
912 QualType ObjectTy,
913 llvm::Value *BaseValue,
914 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000915 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000916 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
917 ObjCInterfaceDecl *Interface,
918 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000919};
920
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000921} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000922
923/* *** Helper Functions *** */
924
925/// getConstantGEP() - Help routine to construct simple GEPs.
926static llvm::Constant *getConstantGEP(llvm::Constant *C,
927 unsigned idx0,
928 unsigned idx1) {
929 llvm::Value *Idxs[] = {
930 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
931 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
932 };
933 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
934}
935
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000936/// hasObjCExceptionAttribute - Return true if this class or any super
937/// class has the __objc_exception__ attribute.
938static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) {
Daniel Dunbarb11fa0d2009-04-13 21:08:27 +0000939 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +0000940 return true;
941 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
942 return hasObjCExceptionAttribute(Super);
943 return false;
944}
945
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000946/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000947
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000948CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
949 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000950{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000951 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000952 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000953}
954
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000955/// GetClass - Return a reference to the class for the given interface
956/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000957llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 const ObjCInterfaceDecl *ID) {
959 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000960}
961
962/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000963llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000964 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000965}
966
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000967/// Generate a constant CFString object.
968/*
969 struct __builtin_CFString {
970 const int *isa; // point to __CFConstantStringClassReference
971 int flags;
972 const char *str;
973 long length;
974 };
975*/
976
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000977llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +0000978 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +0000979 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000980}
981
982/// Generates a message send where the super is the receiver. This is
983/// a message send to self with special delivery semantics indicating
984/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000985CodeGen::RValue
986CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000987 QualType ResultType,
988 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000989 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000990 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000991 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000992 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000993 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000994 // Create and init a super structure; this is a (receiver, class)
995 // pair we will pass to objc_msgSendSuper.
996 llvm::Value *ObjCSuper =
997 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
998 llvm::Value *ReceiverAsObject =
999 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1000 CGF.Builder.CreateStore(ReceiverAsObject,
1001 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001002
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001003 // If this is a class message the metaclass is passed as the target.
1004 llvm::Value *Target;
1005 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001006 if (isCategoryImpl) {
1007 // Message sent to 'super' in a class method defined in a category
1008 // implementation requires an odd treatment.
1009 // If we are in a class method, we must retrieve the
1010 // _metaclass_ for the current class, pointed at by
1011 // the class's "isa" pointer. The following assumes that
1012 // isa" is the first ivar in a class (which it must be).
1013 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1014 Target = CGF.Builder.CreateStructGEP(Target, 0);
1015 Target = CGF.Builder.CreateLoad(Target);
1016 }
1017 else {
1018 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1019 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1020 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1021 Target = Super;
1022 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001023 } else {
1024 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1025 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001026 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
1027 // and ObjCTypes types.
1028 const llvm::Type *ClassTy =
1029 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001030 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001031 CGF.Builder.CreateStore(Target,
1032 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1033
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001034 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001035 ObjCSuper, ObjCTypes.SuperPtrCTy,
1036 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001037}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001038
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001039/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001040CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001041 QualType ResultType,
1042 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001043 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001044 bool IsClassMessage,
1045 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001046 llvm::Value *Arg0 =
1047 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001048 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001049 Arg0, CGF.getContext().getObjCIdType(),
1050 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001051}
1052
1053CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001054 QualType ResultType,
1055 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001056 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001057 QualType Arg0Ty,
1058 bool IsSuper,
1059 const CallArgList &CallArgs) {
1060 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001061 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
1062 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
1063 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001064 CGF.getContext().getObjCSelType()));
1065 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001066
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001067 CodeGenTypes &Types = CGM.getTypes();
1068 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
1069 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001070
1071 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001072 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +00001073 Fn = ObjCTypes.getSendStretFn(IsSuper);
1074 } else if (ResultType->isFloatingType()) {
1075 // FIXME: Sadly, this is wrong. This actually depends on the
1076 // architecture. This happens to be right for x86-32 though.
1077 Fn = ObjCTypes.getSendFpretFn(IsSuper);
1078 } else {
1079 Fn = ObjCTypes.getSendFn(IsSuper);
1080 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001081 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001082 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001083}
1084
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001085llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001086 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001087 // FIXME: I don't understand why gcc generates this, or where it is
1088 // resolved. Investigate. Its also wasteful to look this up over and
1089 // over.
1090 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1091
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1093 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001094}
1095
Fariborz Jahanianda320092009-01-29 19:24:30 +00001096void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001097 // FIXME: We shouldn't need this, the protocol decl should contain
1098 // enough information to tell us whether this was a declaration or a
1099 // definition.
1100 DefinedProtocols.insert(PD->getIdentifier());
1101
1102 // If we have generated a forward reference to this protocol, emit
1103 // it now. Otherwise do nothing, the protocol objects are lazily
1104 // emitted.
1105 if (Protocols.count(PD->getIdentifier()))
1106 GetOrEmitProtocol(PD);
1107}
1108
Fariborz Jahanianda320092009-01-29 19:24:30 +00001109llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001110 if (DefinedProtocols.count(PD->getIdentifier()))
1111 return GetOrEmitProtocol(PD);
1112 return GetOrEmitProtocolRef(PD);
1113}
1114
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001115/*
1116 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1117 struct _objc_protocol {
1118 struct _objc_protocol_extension *isa;
1119 char *protocol_name;
1120 struct _objc_protocol_list *protocol_list;
1121 struct _objc__method_prototype_list *instance_methods;
1122 struct _objc__method_prototype_list *class_methods
1123 };
1124
1125 See EmitProtocolExtension().
1126*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001127llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1128 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1129
1130 // Early exit if a defining object has already been generated.
1131 if (Entry && Entry->hasInitializer())
1132 return Entry;
1133
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001134 // FIXME: I don't understand why gcc generates this, or where it is
1135 // resolved. Investigate. Its also wasteful to look this up over and
1136 // over.
1137 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1138
Chris Lattner8ec03f52008-11-24 03:54:41 +00001139 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001140
1141 // Construct method lists.
1142 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1143 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001144 for (ObjCProtocolDecl::instmeth_iterator
1145 i = PD->instmeth_begin(CGM.getContext()),
1146 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001147 ObjCMethodDecl *MD = *i;
1148 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1149 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1150 OptInstanceMethods.push_back(C);
1151 } else {
1152 InstanceMethods.push_back(C);
1153 }
1154 }
1155
Douglas Gregor6ab35242009-04-09 21:40:53 +00001156 for (ObjCProtocolDecl::classmeth_iterator
1157 i = PD->classmeth_begin(CGM.getContext()),
1158 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001159 ObjCMethodDecl *MD = *i;
1160 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1161 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1162 OptClassMethods.push_back(C);
1163 } else {
1164 ClassMethods.push_back(C);
1165 }
1166 }
1167
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001168 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001169 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001170 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001171 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001172 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001173 PD->protocol_begin(),
1174 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001175 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001176 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1177 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001178 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1179 InstanceMethods);
1180 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001181 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1182 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001183 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1184 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001185 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1186 Values);
1187
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001188 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001189 // Already created, fix the linkage and update the initializer.
1190 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191 Entry->setInitializer(Init);
1192 } else {
1193 Entry =
1194 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1195 llvm::GlobalValue::InternalLinkage,
1196 Init,
1197 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1198 &CGM.getModule());
1199 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001200 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001201 UsedGlobals.push_back(Entry);
1202 // FIXME: Is this necessary? Why only for protocol?
1203 Entry->setAlignment(4);
1204 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001205
1206 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001207}
1208
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001209llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001210 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1211
1212 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001213 // We use the initializer as a marker of whether this is a forward
1214 // reference or not. At module finalization we add the empty
1215 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001216 Entry =
1217 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001218 llvm::GlobalValue::ExternalLinkage,
1219 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001220 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001221 &CGM.getModule());
1222 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001223 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001224 UsedGlobals.push_back(Entry);
1225 // FIXME: Is this necessary? Why only for protocol?
1226 Entry->setAlignment(4);
1227 }
1228
1229 return Entry;
1230}
1231
1232/*
1233 struct _objc_protocol_extension {
1234 uint32_t size;
1235 struct objc_method_description_list *optional_instance_methods;
1236 struct objc_method_description_list *optional_class_methods;
1237 struct objc_property_list *instance_properties;
1238 };
1239*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001240llvm::Constant *
1241CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1242 const ConstantVector &OptInstanceMethods,
1243 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001244 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001245 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001246 std::vector<llvm::Constant*> Values(4);
1247 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001248 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001249 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1250 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001251 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1252 OptInstanceMethods);
1253 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001254 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1255 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001256 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1257 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001258 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1259 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001260 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001261
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001262 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001263 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1264 Values[3]->isNullValue())
1265 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1266
1267 llvm::Constant *Init =
1268 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001269
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001270 // No special section, but goes in llvm.used
1271 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1272 Init,
1273 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001274}
1275
1276/*
1277 struct objc_protocol_list {
1278 struct objc_protocol_list *next;
1279 long count;
1280 Protocol *list[];
1281 };
1282*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001283llvm::Constant *
1284CGObjCMac::EmitProtocolList(const std::string &Name,
1285 ObjCProtocolDecl::protocol_iterator begin,
1286 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001287 std::vector<llvm::Constant*> ProtocolRefs;
1288
Daniel Dunbardbc93372008-08-21 21:57:41 +00001289 for (; begin != end; ++begin)
1290 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001291
1292 // Just return null for empty protocol lists
1293 if (ProtocolRefs.empty())
1294 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1295
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001296 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001297 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1298
1299 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001300 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001301 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1302 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1303 Values[2] =
1304 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1305 ProtocolRefs.size()),
1306 ProtocolRefs);
1307
1308 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1309 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001310 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001311 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001312 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1313}
1314
1315/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001316 struct _objc_property {
1317 const char * const name;
1318 const char * const attributes;
1319 };
1320
1321 struct _objc_property_list {
1322 uint32_t entsize; // sizeof (struct _objc_property)
1323 uint32_t prop_count;
1324 struct _objc_property[prop_count];
1325 };
1326*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001327llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1328 const Decl *Container,
1329 const ObjCContainerDecl *OCD,
1330 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001331 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001332 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1333 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001334 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001335 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001336 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001337 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1338 Prop));
1339 }
1340
1341 // Return null for empty list.
1342 if (Properties.empty())
1343 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1344
1345 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001346 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001347 std::vector<llvm::Constant*> Values(3);
1348 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1349 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1350 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1351 Properties.size());
1352 Values[2] = llvm::ConstantArray::get(AT, Properties);
1353 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1354
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001355 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001356 CreateMetadataVar(Name, Init,
1357 (ObjCABI == 2) ? "__DATA, __objc_const" :
1358 "__OBJC,__property,regular,no_dead_strip",
1359 (ObjCABI == 2) ? 8 : 4,
1360 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001361 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001362}
1363
1364/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001365 struct objc_method_description_list {
1366 int count;
1367 struct objc_method_description list[];
1368 };
1369*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001370llvm::Constant *
1371CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1372 std::vector<llvm::Constant*> Desc(2);
1373 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1374 ObjCTypes.SelectorPtrTy);
1375 Desc[1] = GetMethodVarType(MD);
1376 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1377 Desc);
1378}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001379
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001380llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1381 const char *Section,
1382 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001383 // Return null for empty list.
1384 if (Methods.empty())
1385 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1386
1387 std::vector<llvm::Constant*> Values(2);
1388 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1389 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1390 Methods.size());
1391 Values[1] = llvm::ConstantArray::get(AT, Methods);
1392 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1393
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001394 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001395 return llvm::ConstantExpr::getBitCast(GV,
1396 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001397}
1398
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001399/*
1400 struct _objc_category {
1401 char *category_name;
1402 char *class_name;
1403 struct _objc_method_list *instance_methods;
1404 struct _objc_method_list *class_methods;
1405 struct _objc_protocol_list *protocols;
1406 uint32_t size; // <rdar://4585769>
1407 struct _objc_property_list *instance_properties;
1408 };
1409 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001410void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001411 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001412
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001413 // FIXME: This is poor design, the OCD should have a pointer to the
1414 // category decl. Additionally, note that Category can be null for
1415 // the @implementation w/o an @interface case. Sema should just
1416 // create one for us as it does for @implementation so everyone else
1417 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001418 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001419 const ObjCCategoryDecl *Category =
1420 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001421 std::string ExtName(Interface->getNameAsString() + "_" +
1422 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001423
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001424 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1425 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1426 e = OCD->instmeth_end(); i != e; ++i) {
1427 // Instance methods should always be defined.
1428 InstanceMethods.push_back(GetMethodConstant(*i));
1429 }
1430 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1431 e = OCD->classmeth_end(); i != e; ++i) {
1432 // Class methods should always be defined.
1433 ClassMethods.push_back(GetMethodConstant(*i));
1434 }
1435
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001436 std::vector<llvm::Constant*> Values(7);
1437 Values[0] = GetClassName(OCD->getIdentifier());
1438 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001439 Values[2] =
1440 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1441 ExtName,
1442 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001443 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001444 Values[3] =
1445 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001446 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001447 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001448 if (Category) {
1449 Values[4] =
1450 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1451 Category->protocol_begin(),
1452 Category->protocol_end());
1453 } else {
1454 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1455 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001456 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001457
1458 // If there is no category @interface then there can be no properties.
1459 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001460 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001461 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001462 } else {
1463 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1464 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001465
1466 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1467 Values);
1468
1469 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001470 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1471 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001472 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001473 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001474}
1475
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001476// FIXME: Get from somewhere?
1477enum ClassFlags {
1478 eClassFlags_Factory = 0x00001,
1479 eClassFlags_Meta = 0x00002,
1480 // <rdr://5142207>
1481 eClassFlags_HasCXXStructors = 0x02000,
1482 eClassFlags_Hidden = 0x20000,
1483 eClassFlags_ABI2_Hidden = 0x00010,
1484 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1485};
1486
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001487/*
1488 struct _objc_class {
1489 Class isa;
1490 Class super_class;
1491 const char *name;
1492 long version;
1493 long info;
1494 long instance_size;
1495 struct _objc_ivar_list *ivars;
1496 struct _objc_method_list *methods;
1497 struct _objc_cache *cache;
1498 struct _objc_protocol_list *protocols;
1499 // Objective-C 1.0 extensions (<rdr://4585769>)
1500 const char *ivar_layout;
1501 struct _objc_class_ext *ext;
1502 };
1503
1504 See EmitClassExtension();
1505 */
1506void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001507 DefinedSymbols.insert(ID->getIdentifier());
1508
Chris Lattner8ec03f52008-11-24 03:54:41 +00001509 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001510 // FIXME: Gross
1511 ObjCInterfaceDecl *Interface =
1512 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001513 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001514 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001515 Interface->protocol_begin(),
1516 Interface->protocol_end());
Chris Lattnerb7b58b12009-04-19 06:02:28 +00001517 const llvm::Type *InterfaceTy;
1518 if (Interface->isForwardDecl())
1519 InterfaceTy = llvm::StructType::get(NULL, NULL);
1520 else
1521 InterfaceTy =
Chris Lattner03d9f342009-04-01 06:23:52 +00001522 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001523 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001524 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001525
1526 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001527 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001528 Flags |= eClassFlags_Hidden;
1529
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001530 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1531 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1532 e = ID->instmeth_end(); i != e; ++i) {
1533 // Instance methods should always be defined.
1534 InstanceMethods.push_back(GetMethodConstant(*i));
1535 }
1536 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1537 e = ID->classmeth_end(); i != e; ++i) {
1538 // Class methods should always be defined.
1539 ClassMethods.push_back(GetMethodConstant(*i));
1540 }
1541
1542 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1543 e = ID->propimpl_end(); i != e; ++i) {
1544 ObjCPropertyImplDecl *PID = *i;
1545
1546 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1547 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1548
1549 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1550 if (llvm::Constant *C = GetMethodConstant(MD))
1551 InstanceMethods.push_back(C);
1552 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1553 if (llvm::Constant *C = GetMethodConstant(MD))
1554 InstanceMethods.push_back(C);
1555 }
1556 }
1557
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001558 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001559 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001560 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001561 // Record a reference to the super class.
1562 LazySymbols.insert(Super->getIdentifier());
1563
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001564 Values[ 1] =
1565 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1566 ObjCTypes.ClassPtrTy);
1567 } else {
1568 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1569 }
1570 Values[ 2] = GetClassName(ID->getIdentifier());
1571 // Version is always 0.
1572 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1573 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1574 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001575 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001576 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001577 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001578 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001579 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001580 // cache is always NULL.
1581 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1582 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001583 // FIXME: Set ivar_layout
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001584 // Values[10] = BuildIvarLayout(ID, true);
1585 Values[10] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001586 Values[11] = EmitClassExtension(ID);
1587 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1588 Values);
1589
1590 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001591 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
1592 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001593 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001594 DefinedClasses.push_back(GV);
1595}
1596
1597llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1598 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001599 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001600 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001601 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001602 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001603
Daniel Dunbar04d40782009-04-14 06:00:08 +00001604 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001605 Flags |= eClassFlags_Hidden;
1606
1607 std::vector<llvm::Constant*> Values(12);
1608 // The isa for the metaclass is the root of the hierarchy.
1609 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1610 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1611 Root = Super;
1612 Values[ 0] =
1613 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1614 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001615 // The super class for the metaclass is emitted as the name of the
1616 // super class. The runtime fixes this up to point to the
1617 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001618 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1619 Values[ 1] =
1620 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1621 ObjCTypes.ClassPtrTy);
1622 } else {
1623 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1624 }
1625 Values[ 2] = GetClassName(ID->getIdentifier());
1626 // Version is always 0.
1627 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1628 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1629 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001630 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001631 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001632 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001633 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001634 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001635 // cache is always NULL.
1636 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1637 Values[ 9] = Protocols;
1638 // ivar_layout for metaclass is always NULL.
1639 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1640 // The class extension is always unused for metaclasses.
1641 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1642 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1643 Values);
1644
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001645 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001646 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001647
1648 // Check for a forward reference.
1649 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1650 if (GV) {
1651 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1652 "Forward metaclass reference has incorrect type.");
1653 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1654 GV->setInitializer(Init);
1655 } else {
1656 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1657 llvm::GlobalValue::InternalLinkage,
1658 Init, Name,
1659 &CGM.getModule());
1660 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001661 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001662 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001663 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001664
1665 return GV;
1666}
1667
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001668llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001669 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001670
1671 // FIXME: Should we look these up somewhere other than the
1672 // module. Its a bit silly since we only generate these while
1673 // processing an implementation, so exactly one pointer would work
1674 // if know when we entered/exitted an implementation block.
1675
1676 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001677 // Previously, metaclass with internal linkage may have been defined.
1678 // pass 'true' as 2nd argument so it is returned.
1679 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001680 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1681 "Forward metaclass reference has incorrect type.");
1682 return GV;
1683 } else {
1684 // Generate as an external reference to keep a consistent
1685 // module. This will be patched up when we emit the metaclass.
1686 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1687 llvm::GlobalValue::ExternalLinkage,
1688 0,
1689 Name,
1690 &CGM.getModule());
1691 }
1692}
1693
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001694/*
1695 struct objc_class_ext {
1696 uint32_t size;
1697 const char *weak_ivar_layout;
1698 struct _objc_property_list *properties;
1699 };
1700*/
1701llvm::Constant *
1702CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1703 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001704 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001705
1706 std::vector<llvm::Constant*> Values(3);
1707 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001708 // FIXME: Output weak_ivar_layout string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00001709 // Values[1] = BuildIvarLayout(ID, false);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00001710 Values[1] = GetIvarLayoutName(0, ObjCTypes);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001711 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001712 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001713
1714 // Return null if no extension bits are used.
1715 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1716 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1717
1718 llvm::Constant *Init =
1719 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001720 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001721 Init, "__OBJC,__class_ext,regular,no_dead_strip",
1722 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001723}
1724
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001725/// getInterfaceDeclForIvar - Get the interface declaration node where
1726/// this ivar is declared in.
1727/// FIXME. Ideally, this info should be in the ivar node. But currently
1728/// it is not and prevailing wisdom is that ASTs should not have more
1729/// info than is absolutely needed, even though this info reflects the
1730/// source language.
1731///
1732static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1733 const ObjCInterfaceDecl *OI,
Douglas Gregor6ab35242009-04-09 21:40:53 +00001734 const ObjCIvarDecl *IVD,
1735 ASTContext &Context) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001736 if (!OI)
1737 return 0;
1738 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1739 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1740 E = OI->ivar_end(); I != E; ++I)
1741 if ((*I)->getIdentifier() == IVD->getIdentifier())
1742 return OI;
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00001743 // look into properties.
Douglas Gregor6ab35242009-04-09 21:40:53 +00001744 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
1745 E = OI->prop_end(Context); I != E; ++I) {
Fariborz Jahanian5a4b4532009-03-31 17:00:52 +00001746 ObjCPropertyDecl *PDecl = (*I);
1747 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl())
1748 if (IV->getIdentifier() == IVD->getIdentifier())
1749 return OI;
1750 }
Douglas Gregor6ab35242009-04-09 21:40:53 +00001751 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context);
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001752}
1753
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001754/*
1755 struct objc_ivar {
1756 char *ivar_name;
1757 char *ivar_type;
1758 int ivar_offset;
1759 };
1760
1761 struct objc_ivar_list {
1762 int ivar_count;
1763 struct objc_ivar list[count];
1764 };
1765 */
1766llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001767 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001768 std::vector<llvm::Constant*> Ivars, Ivar(3);
1769
1770 // When emitting the root class GCC emits ivar entries for the
1771 // actual class structure. It is not clear if we need to follow this
1772 // behavior; for now lets try and get away with not doing it. If so,
1773 // the cleanest solution would be to make up an ObjCInterfaceDecl
1774 // for the class.
1775 if (ForClass)
1776 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001777
1778 ObjCInterfaceDecl *OID =
1779 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00001780 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001781
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001782 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
1783 GetNamedIvarList(OID, OIvars);
1784
1785 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
1786 ObjCIvarDecl *IVD = OIvars[i];
1787 const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001788 Ivar[0] = GetMethodVarName(Field->getIdentifier());
Devang Patel7794bb82009-03-04 18:21:39 +00001789 Ivar[1] = GetMethodVarType(Field);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00001790 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
1791 GetIvarBaseOffset(Layout, Field));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001792 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001793 }
1794
1795 // Return null for empty list.
1796 if (Ivars.empty())
1797 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1798
1799 std::vector<llvm::Constant*> Values(2);
1800 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1801 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1802 Ivars.size());
1803 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1804 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1805
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001806 llvm::GlobalVariable *GV;
1807 if (ForClass)
1808 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00001809 Init, "__OBJC,__class_vars,regular,no_dead_strip",
1810 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001811 else
1812 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
1813 + ID->getNameAsString(),
1814 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001815 4, true);
1816 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001817}
1818
1819/*
1820 struct objc_method {
1821 SEL method_name;
1822 char *method_types;
1823 void *method;
1824 };
1825
1826 struct objc_method_list {
1827 struct objc_method_list *obsolete;
1828 int count;
1829 struct objc_method methods_list[count];
1830 };
1831*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001832
1833/// GetMethodConstant - Return a struct objc_method constant for the
1834/// given method if it has been defined. The result is null if the
1835/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001836llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001837 // FIXME: Use DenseMap::lookup
1838 llvm::Function *Fn = MethodDefinitions[MD];
1839 if (!Fn)
1840 return 0;
1841
1842 std::vector<llvm::Constant*> Method(3);
1843 Method[0] =
1844 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1845 ObjCTypes.SelectorPtrTy);
1846 Method[1] = GetMethodVarType(MD);
1847 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1848 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1849}
1850
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001851llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1852 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001853 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001854 // Return null for empty list.
1855 if (Methods.empty())
1856 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1857
1858 std::vector<llvm::Constant*> Values(3);
1859 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1860 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1861 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1862 Methods.size());
1863 Values[2] = llvm::ConstantArray::get(AT, Methods);
1864 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1865
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001866 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001867 return llvm::ConstantExpr::getBitCast(GV,
1868 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001869}
1870
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001871llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001872 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001873 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001874 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001875
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001876 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001877 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001878 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001879 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001880 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001881 llvm::GlobalValue::InternalLinkage,
1882 Name,
1883 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001884 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001885
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001886 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001887}
1888
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001889uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00001890 const FieldDecl *Field) {
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00001891 if (!Field->isBitField())
Daniel Dunbar48fa0642009-04-19 02:03:42 +00001892 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
1893
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00001894 // FIXME. Must be a better way of getting a bitfield base offset.
Daniel Dunbar48fa0642009-04-19 02:03:42 +00001895 CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field);
1896 // FIXME: The "field no" for bitfields is something completely
1897 // different; it is the offset in multiples of the base type size!
1898 uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field);
1899 const llvm::Type *Ty =
1900 CGM.getTypes().ConvertTypeForMemRecursive(Field->getType());
1901 Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty);
1902 return (Offset + BFI.Begin) / 8;
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00001903}
1904
Daniel Dunbar48fa0642009-04-19 02:03:42 +00001905/// GetFieldBaseOffset - return the field's byte offset.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001906uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI,
1907 const llvm::StructLayout *Layout,
Chris Lattnercd0ee142009-03-31 08:33:16 +00001908 const FieldDecl *Field) {
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00001909 // Is this a c struct?
1910 if (!OI)
1911 return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001912 const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
Chris Lattnercd0ee142009-03-31 08:33:16 +00001913 const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
1914 return GetIvarBaseOffset(Layout, FD);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00001915}
1916
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001917llvm::GlobalVariable *
1918CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
1919 llvm::Constant *Init,
1920 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001921 unsigned Align,
1922 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001923 const llvm::Type *Ty = Init->getType();
1924 llvm::GlobalVariable *GV =
1925 new llvm::GlobalVariable(Ty, false,
1926 llvm::GlobalValue::InternalLinkage,
1927 Init,
1928 Name,
1929 &CGM.getModule());
1930 if (Section)
1931 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001932 if (Align)
1933 GV->setAlignment(Align);
1934 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001935 UsedGlobals.push_back(GV);
1936 return GV;
1937}
1938
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001939llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001940 // Abuse this interface function as a place to finalize.
1941 FinishModule();
1942
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001943 return NULL;
1944}
1945
Chris Lattner74391b42009-03-22 21:03:39 +00001946llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001947 return ObjCTypes.GetPropertyFn;
1948}
1949
Chris Lattner74391b42009-03-22 21:03:39 +00001950llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Daniel Dunbar49f66022008-09-24 03:38:44 +00001951 return ObjCTypes.SetPropertyFn;
1952}
1953
Chris Lattner74391b42009-03-22 21:03:39 +00001954llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001955 return ObjCTypes.EnumerationMutationFn;
1956}
1957
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001958/*
1959
1960Objective-C setjmp-longjmp (sjlj) Exception Handling
1961--
1962
1963The basic framework for a @try-catch-finally is as follows:
1964{
1965 objc_exception_data d;
1966 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001967 bool _call_try_exit = true;
1968
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001969 objc_exception_try_enter(&d);
1970 if (!setjmp(d.jmp_buf)) {
1971 ... try body ...
1972 } else {
1973 // exception path
1974 id _caught = objc_exception_extract(&d);
1975
1976 // enter new try scope for handlers
1977 if (!setjmp(d.jmp_buf)) {
1978 ... match exception and execute catch blocks ...
1979
1980 // fell off end, rethrow.
1981 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001982 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001983 } else {
1984 // exception in catch block
1985 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001986 _call_try_exit = false;
1987 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001988 }
1989 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001990 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001991
1992finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001993 if (_call_try_exit)
1994 objc_exception_try_exit(&d);
1995
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001996 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001997 ... dispatch to finally destination ...
1998
1999finally_rethrow:
2000 objc_exception_throw(_rethrow);
2001
2002finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002003}
2004
2005This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002006uses _rethrow to determine if objc_exception_try_exit should be called
2007and if the object should be rethrown. This breaks in the face of
2008throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002009
2010We specialize this framework for a few particular circumstances:
2011
2012 - If there are no catch blocks, then we avoid emitting the second
2013 exception handling context.
2014
2015 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2016 e)) we avoid emitting the code to rethrow an uncaught exception.
2017
2018 - FIXME: If there is no @finally block we can do a few more
2019 simplifications.
2020
2021Rethrows and Jumps-Through-Finally
2022--
2023
2024Support for implicit rethrows and jumping through the finally block is
2025handled by storing the current exception-handling context in
2026ObjCEHStack.
2027
Daniel Dunbar898d5082008-09-30 01:06:03 +00002028In order to implement proper @finally semantics, we support one basic
2029mechanism for jumping through the finally block to an arbitrary
2030destination. Constructs which generate exits from a @try or @catch
2031block use this mechanism to implement the proper semantics by chaining
2032jumps, as necessary.
2033
2034This mechanism works like the one used for indirect goto: we
2035arbitrarily assign an ID to each destination and store the ID for the
2036destination in a variable prior to entering the finally block. At the
2037end of the finally block we simply create a switch to the proper
2038destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002039
2040Code gen for @synchronized(expr) stmt;
2041Effectively generating code for:
2042objc_sync_enter(expr);
2043@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002044*/
2045
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002046void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2047 const Stmt &S) {
2048 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002049 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002050 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002051 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002052 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2053 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2054 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002055
2056 // For @synchronized, call objc_sync_enter(sync.expr). The
2057 // evaluation of the expression must occur before we enter the
2058 // @synchronized. We can safely avoid a temp here because jumps into
2059 // @synchronized are illegal & this will dominate uses.
2060 llvm::Value *SyncArg = 0;
2061 if (!isTry) {
2062 SyncArg =
2063 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2064 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002065 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002066 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002067
2068 // Push an EH context entry, used for handling rethrows and jumps
2069 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002070 CGF.PushCleanupBlock(FinallyBlock);
2071
Anders Carlsson273558f2009-02-07 21:37:21 +00002072 CGF.ObjCEHValueStack.push_back(0);
2073
Daniel Dunbar898d5082008-09-30 01:06:03 +00002074 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002075 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2076 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002077 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2078 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002079 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2080 "_call_try_exit");
2081 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2082
Anders Carlsson80f25672008-09-09 17:59:25 +00002083 // Enter a new try block and call setjmp.
2084 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
2085 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2086 "jmpbufarray");
2087 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
2088 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2089 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002090
Daniel Dunbar55e87422008-11-11 02:29:29 +00002091 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2092 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002093 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002094 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002095
2096 // Emit the @try block.
2097 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002098 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2099 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002100 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002101
2102 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002103 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002104
2105 // Retrieve the exception object. We may emit multiple blocks but
2106 // nothing can cross this so the value is already in SSA form.
2107 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2108 ExceptionData,
2109 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002110 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002111 if (!isTry)
2112 {
2113 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002114 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002115 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002116 }
2117 else if (const ObjCAtCatchStmt* CatchStmt =
2118 cast<ObjCAtTryStmt>(S).getCatchStmts())
2119 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002120 // Enter a new exception try block (in case a @catch block throws
2121 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00002122 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002123
Anders Carlsson80f25672008-09-09 17:59:25 +00002124 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
2125 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002126 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002127
Daniel Dunbar55e87422008-11-11 02:29:29 +00002128 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2129 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002130 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002131
2132 CGF.EmitBlock(CatchBlock);
2133
Daniel Dunbar55e40722008-09-27 07:03:52 +00002134 // Handle catch list. As a special case we check if everything is
2135 // matched and avoid generating code for falling off the end if
2136 // so.
2137 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002138 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002139 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002140
Steve Naroff7ba138a2009-03-03 19:52:17 +00002141 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002142 const PointerType *PT = 0;
2143
Anders Carlsson80f25672008-09-09 17:59:25 +00002144 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002145 if (!CatchParam) {
2146 AllMatched = true;
2147 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002148 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002149
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002150 // catch(id e) always matches.
2151 // FIXME: For the time being we also match id<X>; this should
2152 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002153 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002154 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002155 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002156 }
2157
Daniel Dunbar55e40722008-09-27 07:03:52 +00002158 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002159 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002160 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002161 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002162 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002163 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002164
Anders Carlssondde0a942008-09-11 09:15:33 +00002165 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002166 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002167 break;
2168 }
2169
Daniel Dunbar129271a2008-09-27 07:36:24 +00002170 assert(PT && "Unexpected non-pointer type in @catch");
2171 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002172 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002173 assert(ObjCType && "Catch parameter must have Objective-C type!");
2174
2175 // Check if the @catch block matches the exception object.
2176 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2177
Anders Carlsson80f25672008-09-09 17:59:25 +00002178 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2179 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002180
Daniel Dunbar55e87422008-11-11 02:29:29 +00002181 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002182
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002183 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002184 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002185
2186 // Emit the @catch block.
2187 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002188 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002189 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002190
2191 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002192 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002193 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002194 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002195
2196 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002197 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002198
2199 CGF.EmitBlock(NextCatchBlock);
2200 }
2201
Daniel Dunbar55e40722008-09-27 07:03:52 +00002202 if (!AllMatched) {
2203 // None of the handlers caught the exception, so store it to be
2204 // rethrown at the end of the @finally block.
2205 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002206 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002207 }
2208
2209 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002210 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002211 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2212 ExceptionData),
2213 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002214 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002215 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002216 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002217 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002218 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002219 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002220 }
2221
Daniel Dunbar898d5082008-09-30 01:06:03 +00002222 // Pop the exception-handling stack entry. It is important to do
2223 // this now, because the code in the @finally block is not in this
2224 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002225 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2226
Anders Carlsson273558f2009-02-07 21:37:21 +00002227 CGF.ObjCEHValueStack.pop_back();
2228
Anders Carlsson80f25672008-09-09 17:59:25 +00002229 // Emit the @finally block.
2230 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002231 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2232
2233 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2234
2235 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002236 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002237
2238 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002239 if (isTry) {
2240 if (const ObjCAtFinallyStmt* FinallyStmt =
2241 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2242 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002243 } else {
2244 // Emit objc_sync_exit(expr); as finally's sole statement for
2245 // @synchronized.
2246 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002247 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002248
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002249 // Emit the switch block
2250 if (Info.SwitchBlock)
2251 CGF.EmitBlock(Info.SwitchBlock);
2252 if (Info.EndBlock)
2253 CGF.EmitBlock(Info.EndBlock);
2254
Daniel Dunbar898d5082008-09-30 01:06:03 +00002255 CGF.EmitBlock(FinallyRethrow);
2256 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2257 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002258 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002259
2260 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002261}
2262
2263void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002264 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002265 llvm::Value *ExceptionAsObject;
2266
2267 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2268 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2269 ExceptionAsObject =
2270 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2271 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002272 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002273 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002274 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002275 }
2276
2277 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002278 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002279
2280 // Clear the insertion point to indicate we are in unreachable code.
2281 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002282}
2283
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002284/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002285/// object: objc_read_weak (id *src)
2286///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002287llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002288 llvm::Value *AddrWeakObj)
2289{
Eli Friedman8339b352009-03-07 03:57:15 +00002290 const llvm::Type* DestTy =
2291 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002292 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002293 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002294 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002295 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002296 return read_weak;
2297}
2298
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002299/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2300/// objc_assign_weak (id src, id *dst)
2301///
2302void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2303 llvm::Value *src, llvm::Value *dst)
2304{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002305 const llvm::Type * SrcTy = src->getType();
2306 if (!isa<llvm::PointerType>(SrcTy)) {
2307 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2308 assert(Size <= 8 && "does not support size > 8");
2309 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2310 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002311 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2312 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002313 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2314 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002315 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002316 src, dst, "weakassign");
2317 return;
2318}
2319
Fariborz Jahanian58626502008-11-19 00:59:10 +00002320/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2321/// objc_assign_global (id src, id *dst)
2322///
2323void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2324 llvm::Value *src, llvm::Value *dst)
2325{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002326 const llvm::Type * SrcTy = src->getType();
2327 if (!isa<llvm::PointerType>(SrcTy)) {
2328 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2329 assert(Size <= 8 && "does not support size > 8");
2330 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2331 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002332 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2333 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002334 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2335 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002336 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2337 src, dst, "globalassign");
2338 return;
2339}
2340
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002341/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2342/// objc_assign_ivar (id src, id *dst)
2343///
2344void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2345 llvm::Value *src, llvm::Value *dst)
2346{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002347 const llvm::Type * SrcTy = src->getType();
2348 if (!isa<llvm::PointerType>(SrcTy)) {
2349 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2350 assert(Size <= 8 && "does not support size > 8");
2351 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2352 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002353 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2354 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002355 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2356 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2357 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2358 src, dst, "assignivar");
2359 return;
2360}
2361
Fariborz Jahanian58626502008-11-19 00:59:10 +00002362/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2363/// objc_assign_strongCast (id src, id *dst)
2364///
2365void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2366 llvm::Value *src, llvm::Value *dst)
2367{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002368 const llvm::Type * SrcTy = src->getType();
2369 if (!isa<llvm::PointerType>(SrcTy)) {
2370 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
2371 assert(Size <= 8 && "does not support size > 8");
2372 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2373 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002374 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2375 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002376 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2377 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002378 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2379 src, dst, "weakassign");
2380 return;
2381}
2382
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002383/// EmitObjCValueForIvar - Code Gen for ivar reference.
2384///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002385LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2386 QualType ObjectTy,
2387 llvm::Value *BaseValue,
2388 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002389 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002390 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
2391 const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002392 if (Ivar->isBitField())
2393 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2394 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002395 // TODO: Add a special case for isa (index 0)
2396 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2397 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002398 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002399 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
2400 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002401 LValue::SetObjCIvar(LV, true);
2402 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002403}
2404
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002405llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2406 ObjCInterfaceDecl *Interface,
2407 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002408 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface);
Daniel Dunbar60952f92009-04-20 00:37:55 +00002409 const FieldDecl *Field =
2410 Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +00002411 uint64_t Offset = GetIvarBaseOffset(Layout, Field);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002412 return llvm::ConstantInt::get(
2413 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2414 Offset);
2415}
2416
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002417/* *** Private Interface *** */
2418
2419/// EmitImageInfo - Emit the image info marker used to encode some module
2420/// level information.
2421///
2422/// See: <rdr://4810609&4810587&4810587>
2423/// struct IMAGE_INFO {
2424/// unsigned version;
2425/// unsigned flags;
2426/// };
2427enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002428 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2429 // this implies.
2430 eImageInfo_GarbageCollected = (1 << 1),
2431 eImageInfo_GCOnly = (1 << 2),
2432 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2433
2434 // A flag indicating that the module has no instances of an
2435 // @synthesize of a superclass variable. <rdar://problem/6803242>
2436 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002437};
2438
2439void CGObjCMac::EmitImageInfo() {
2440 unsigned version = 0; // Version is unused?
2441 unsigned flags = 0;
2442
2443 // FIXME: Fix and continue?
2444 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2445 flags |= eImageInfo_GarbageCollected;
2446 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2447 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002448
2449 // We never allow @synthesize of a superclass property.
2450 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002451
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002452 // Emitted as int[2];
2453 llvm::Constant *values[2] = {
2454 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2455 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2456 };
2457 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002458
2459 const char *Section;
2460 if (ObjCABI == 1)
2461 Section = "__OBJC, __image_info,regular";
2462 else
2463 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002464 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002465 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2466 llvm::ConstantArray::get(AT, values, 2),
2467 Section,
2468 0,
2469 true);
2470 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002471}
2472
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002473
2474// struct objc_module {
2475// unsigned long version;
2476// unsigned long size;
2477// const char *name;
2478// Symtab symtab;
2479// };
2480
2481// FIXME: Get from somewhere
2482static const int ModuleVersion = 7;
2483
2484void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002485 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002486
2487 std::vector<llvm::Constant*> Values(4);
2488 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2489 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002490 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002491 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002492 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002493 CreateMetadataVar("\01L_OBJC_MODULES",
2494 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2495 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002496 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002497}
2498
2499llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 unsigned NumClasses = DefinedClasses.size();
2501 unsigned NumCategories = DefinedCategories.size();
2502
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002503 // Return null if no symbols were defined.
2504 if (!NumClasses && !NumCategories)
2505 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2506
2507 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002508 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2509 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2510 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2511 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2512
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002513 // The runtime expects exactly the list of defined classes followed
2514 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002515 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002516 for (unsigned i=0; i<NumClasses; i++)
2517 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2518 ObjCTypes.Int8PtrTy);
2519 for (unsigned i=0; i<NumCategories; i++)
2520 Symbols[NumClasses + i] =
2521 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2522 ObjCTypes.Int8PtrTy);
2523
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002524 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002525 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002526 NumClasses + NumCategories),
2527 Symbols);
2528
2529 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2530
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002531 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002532 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2533 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002534 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002535 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2536}
2537
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002538llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002540 LazySymbols.insert(ID->getIdentifier());
2541
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002542 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2543
2544 if (!Entry) {
2545 llvm::Constant *Casted =
2546 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2547 ObjCTypes.ClassPtrTy);
2548 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002549 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2550 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002551 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002552 }
2553
2554 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002555}
2556
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002557llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002558 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2559
2560 if (!Entry) {
2561 llvm::Constant *Casted =
2562 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2563 ObjCTypes.SelectorPtrTy);
2564 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002565 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2566 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002567 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002568 }
2569
2570 return Builder.CreateLoad(Entry, false, "tmp");
2571}
2572
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002573llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002574 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002575
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002576 if (!Entry)
2577 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2578 llvm::ConstantArray::get(Ident->getName()),
2579 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002580 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002581
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002582 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002583}
2584
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002585/// GetInterfaceDeclStructLayout - Get layout for ivars of given
2586/// interface declaration.
2587const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout(
2588 const ObjCInterfaceDecl *OID) const {
Daniel Dunbar24c89912009-04-21 21:41:56 +00002589 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002590 QualType T =
2591 CGM.getContext().getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID));
2592 const llvm::StructType *InterfaceTy =
2593 cast<llvm::StructType>(CGM.getTypes().ConvertType(T));
2594 return CGM.getTargetData().getStructLayout(InterfaceTy);
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002595}
2596
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002597/// GetIvarLayoutName - Returns a unique constant for the given
2598/// ivar layout bitmap.
2599llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2600 const ObjCCommonTypesHelper &ObjCTypes) {
2601 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2602}
2603
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002604void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI,
2605 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002606 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002607 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002608 unsigned int BytePos, bool ForStrongLayout,
2609 int &Index, int &SkIndex, bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002610 bool IsUnion = (RD && RD->isUnion());
2611 uint64_t MaxUnionIvarSize = 0;
2612 uint64_t MaxSkippedUnionIvarSize = 0;
2613 FieldDecl *MaxField = 0;
2614 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002615 FieldDecl *LastFieldBitfield = 0;
2616
Chris Lattnerf1690852009-03-31 08:48:01 +00002617 unsigned base = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002618 if (RecFields.empty())
2619 return;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002620 if (IsUnion)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002621 base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]);
Chris Lattnerf1690852009-03-31 08:48:01 +00002622 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
2623 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
2624
2625 llvm::SmallVector<FieldDecl*, 16> TmpRecFields;
2626
2627 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002628 FieldDecl *Field = RecFields[i];
2629 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002630 if (!Field->getIdentifier() || Field->isBitField()) {
2631 LastFieldBitfield = Field;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002632 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002633 }
2634 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002635 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002636 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002637 if (FQT->isUnionType())
2638 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002639 else
2640 assert(FQT->isRecordType() &&
2641 "only union/record is supported for ivar layout bitmap");
2642
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002643 const RecordType *RT = FQT->getAsRecordType();
2644 const RecordDecl *RD = RT->getDecl();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00002645 // FIXME - Find a more efficient way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002646 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2647 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002648 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2649 const llvm::StructLayout *RecLayout =
2650 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2651
2652 BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002653 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002654 ForStrongLayout, Index, SkIndex,
2655 HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002656 TmpRecFields.clear();
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002657 continue;
2658 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002659
2660 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002661 const ConstantArrayType *CArray =
2662 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002663 uint64_t ElCount = CArray->getSize().getZExtValue();
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002664 assert(CArray && "only array with know element size is supported");
2665 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002666 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2667 const ConstantArrayType *CArray =
2668 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002669 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00002670 FQT = CArray->getElementType();
2671 }
2672
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002673 assert(!FQT->isUnionType() &&
2674 "layout for array of unions not supported");
2675 if (FQT->isRecordType()) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002676 int OldIndex = Index;
2677 int OldSkIndex = SkIndex;
2678
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002679 // FIXME - Use a common routine with the above!
2680 const RecordType *RT = FQT->getAsRecordType();
2681 const RecordDecl *RD = RT->getDecl();
2682 // FIXME - Find a more efficiant way of passing records down.
Douglas Gregor6ab35242009-04-09 21:40:53 +00002683 TmpRecFields.append(RD->field_begin(CGM.getContext()),
2684 RD->field_end(CGM.getContext()));
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002685 const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT);
2686 const llvm::StructLayout *RecLayout =
2687 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Chris Lattnerf1690852009-03-31 08:48:01 +00002688
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00002689 BuildAggrIvarLayout(0, RecLayout, RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002690 TmpRecFields,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002691 BytePos + GetFieldBaseOffset(OI, Layout, Field),
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002692 ForStrongLayout, Index, SkIndex,
2693 HasUnion);
Chris Lattnerf1690852009-03-31 08:48:01 +00002694 TmpRecFields.clear();
2695
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002696 // Replicate layout information for each array element. Note that
2697 // one element is already done.
2698 uint64_t ElIx = 1;
2699 for (int FirstIndex = Index, FirstSkIndex = SkIndex;
2700 ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002701 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002702 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2703 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002704 GC_IVAR gcivar;
2705 gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx;
2706 gcivar.ivar_size = IvarsInfo[i].ivar_size;
2707 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002708 }
2709
Chris Lattnerf1690852009-03-31 08:48:01 +00002710 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002711 GC_IVAR skivar;
2712 skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx;
2713 skivar.ivar_size = SkipIvars[i].ivar_size;
2714 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002715 }
2716 }
2717 continue;
2718 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002719 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002720 // At this point, we are done with Record/Union and array there of.
2721 // For other arrays we are down to its element type.
2722 QualType::GCAttrTypes GCAttr = QualType::GCNone;
2723 do {
2724 if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) {
2725 GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak;
2726 break;
2727 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002728 else if (CGM.getContext().isObjCObjectPointerType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002729 GCAttr = QualType::Strong;
2730 break;
2731 }
2732 else if (const PointerType *PT = FQT->getAsPointerType()) {
2733 FQT = PT->getPointeeType();
2734 }
2735 else {
2736 break;
2737 }
2738 } while (true);
Chris Lattnerf1690852009-03-31 08:48:01 +00002739
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002740 if ((ForStrongLayout && GCAttr == QualType::Strong)
2741 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
2742 if (IsUnion)
2743 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002744 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType())
2745 / WordSizeInBits;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002746 if (UnionIvarSize > MaxUnionIvarSize)
2747 {
2748 MaxUnionIvarSize = UnionIvarSize;
2749 MaxField = Field;
2750 }
2751 }
2752 else
2753 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002754 GC_IVAR gcivar;
2755 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2756 gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
2757 WordSizeInBits;
2758 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002759 }
2760 }
2761 else if ((ForStrongLayout &&
2762 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
2763 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
2764 if (IsUnion)
2765 {
2766 uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType());
2767 if (UnionIvarSize > MaxSkippedUnionIvarSize)
2768 {
2769 MaxSkippedUnionIvarSize = UnionIvarSize;
2770 MaxSkippedField = Field;
2771 }
2772 }
2773 else
2774 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002775 GC_IVAR skivar;
2776 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field);
2777 skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) /
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002778 ByteSizeInBits;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002779 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002780 }
2781 }
2782 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00002783 if (LastFieldBitfield) {
2784 // Last field was a bitfield. Must update skip info.
2785 GC_IVAR skivar;
2786 skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout,
2787 LastFieldBitfield);
2788 Expr *BitWidth = LastFieldBitfield->getBitWidth();
2789 uint64_t BitFieldSize =
2790 BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue();
2791 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
2792 + ((BitFieldSize % ByteSizeInBits) != 0);
2793 SkipIvars.push_back(skivar); ++SkIndex;
2794 }
2795
Chris Lattnerf1690852009-03-31 08:48:01 +00002796 if (MaxField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002797 GC_IVAR gcivar;
2798 gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField);
2799 gcivar.ivar_size = MaxUnionIvarSize;
2800 IvarsInfo.push_back(gcivar); ++Index;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00002801 }
Chris Lattnerf1690852009-03-31 08:48:01 +00002802
2803 if (MaxSkippedField) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002804 GC_IVAR skivar;
2805 skivar.ivar_bytepos = BytePos +
2806 GetFieldBaseOffset(OI, Layout, MaxSkippedField);
2807 skivar.ivar_size = MaxSkippedUnionIvarSize;
2808 SkipIvars.push_back(skivar); ++SkIndex;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002809 }
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002810}
2811
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002812static int
Chris Lattnerf1690852009-03-31 08:48:01 +00002813IvarBytePosCompare(const void *a, const void *b)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002814{
2815 unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos;
2816 unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos;
2817
2818 if (sa < sb)
2819 return -1;
2820 if (sa > sb)
2821 return 1;
2822 return 0;
2823}
2824
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002825/// BuildIvarLayout - Builds ivar layout bitmap for the class
2826/// implementation for the __strong or __weak case.
2827/// The layout map displays which words in ivar list must be skipped
2828/// and which must be scanned by GC (see below). String is built of bytes.
2829/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
2830/// of words to skip and right nibble is count of words to scan. So, each
2831/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
2832/// represented by a 0x00 byte which also ends the string.
2833/// 1. when ForStrongLayout is true, following ivars are scanned:
2834/// - id, Class
2835/// - object *
2836/// - __strong anything
2837///
2838/// 2. When ForStrongLayout is false, following ivars are scanned:
2839/// - __weak anything
2840///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002841llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002842 const ObjCImplementationDecl *OMD,
2843 bool ForStrongLayout) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002844 int Index = -1;
2845 int SkIndex = -1;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002846 bool hasUnion = false;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002847 int SkipScan;
2848 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002849 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2850 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
2851 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002852
Chris Lattnerf1690852009-03-31 08:48:01 +00002853 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002854 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00002855 CGM.getContext().CollectObjCIvars(OI, RecFields);
2856 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002857 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00002858
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002859 SkipIvars.clear();
2860 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00002861
2862 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI);
Chris Lattnerf1690852009-03-31 08:48:01 +00002863 BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout,
2864 Index, SkIndex, hasUnion);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002865 if (Index == -1)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002866 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002867
2868 // Sort on byte position in case we encounterred a union nested in
2869 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002870 if (hasUnion && !IvarsInfo.empty())
2871 qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2872 if (hasUnion && !SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002873 qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare);
2874
2875 // Build the string of skip/scan nibbles
2876 SkipScan = -1;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002877 SkipScanIvars.clear();
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002878 unsigned int WordSize =
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002879 CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002880 if (IvarsInfo[0].ivar_bytepos == 0) {
2881 WordsToSkip = 0;
2882 WordsToScan = IvarsInfo[0].ivar_size;
2883 }
2884 else {
2885 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
2886 WordsToScan = IvarsInfo[0].ivar_size;
2887 }
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002888 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++)
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002889 {
2890 unsigned int TailPrevGCObjC =
2891 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
2892 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC)
2893 {
2894 // consecutive 'scanned' object pointers.
2895 WordsToScan += IvarsInfo[i].ivar_size;
2896 }
2897 else
2898 {
2899 // Skip over 'gc'able object pointer which lay over each other.
2900 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
2901 continue;
2902 // Must skip over 1 or more words. We save current skip/scan values
2903 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002904 SKIP_SCAN SkScan;
2905 SkScan.skip = WordsToSkip;
2906 SkScan.scan = WordsToScan;
2907 SkipScanIvars.push_back(SkScan); ++SkipScan;
2908
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002909 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002910 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
2911 SkScan.scan = 0;
2912 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002913 WordsToSkip = 0;
2914 WordsToScan = IvarsInfo[i].ivar_size;
2915 }
2916 }
2917 if (WordsToScan > 0)
2918 {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002919 SKIP_SCAN SkScan;
2920 SkScan.skip = WordsToSkip;
2921 SkScan.scan = WordsToScan;
2922 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002923 }
2924
2925 bool BytesSkipped = false;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002926 if (!SkipIvars.empty())
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002927 {
2928 int LastByteSkipped =
2929 SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size;
2930 int LastByteScanned =
2931 IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize;
2932 BytesSkipped = (LastByteSkipped > LastByteScanned);
2933 // Compute number of bytes to skip at the tail end of the last ivar scanned.
2934 if (BytesSkipped)
2935 {
2936 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002937 SKIP_SCAN SkScan;
2938 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
2939 SkScan.scan = 0;
2940 SkipScanIvars.push_back(SkScan); ++SkipScan;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00002941 }
2942 }
2943 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
2944 // as 0xMN.
2945 for (int i = 0; i <= SkipScan; i++)
2946 {
2947 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
2948 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
2949 // 0xM0 followed by 0x0N detected.
2950 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
2951 for (int j = i+1; j < SkipScan; j++)
2952 SkipScanIvars[j] = SkipScanIvars[j+1];
2953 --SkipScan;
2954 }
2955 }
2956
2957 // Generate the string.
2958 std::string BitMap;
2959 for (int i = 0; i <= SkipScan; i++)
2960 {
2961 unsigned char byte;
2962 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
2963 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
2964 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
2965 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
2966
2967 if (skip_small > 0 || skip_big > 0)
2968 BytesSkipped = true;
2969 // first skip big.
2970 for (unsigned int ix = 0; ix < skip_big; ix++)
2971 BitMap += (unsigned char)(0xf0);
2972
2973 // next (skip small, scan)
2974 if (skip_small)
2975 {
2976 byte = skip_small << 4;
2977 if (scan_big > 0)
2978 {
2979 byte |= 0xf;
2980 --scan_big;
2981 }
2982 else if (scan_small)
2983 {
2984 byte |= scan_small;
2985 scan_small = 0;
2986 }
2987 BitMap += byte;
2988 }
2989 // next scan big
2990 for (unsigned int ix = 0; ix < scan_big; ix++)
2991 BitMap += (unsigned char)(0x0f);
2992 // last scan small
2993 if (scan_small)
2994 {
2995 byte = scan_small;
2996 BitMap += byte;
2997 }
2998 }
2999 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003000 unsigned char zero = 0;
3001 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003002
3003 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3004 printf("\n%s ivar layout for class '%s': ",
3005 ForStrongLayout ? "strong" : "weak",
3006 OMD->getClassInterface()->getNameAsCString());
3007 const unsigned char *s = (unsigned char*)BitMap.c_str();
3008 for (unsigned i = 0; i < BitMap.size(); i++)
3009 if (!(s[i] & 0xf0))
3010 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3011 else
3012 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3013 printf("\n");
3014 }
3015
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003016 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3017 // final layout.
3018 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003019 return llvm::Constant::getNullValue(PtrTy);
3020 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3021 llvm::ConstantArray::get(BitMap.c_str()),
3022 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003023 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003024 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003025}
3026
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003027llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003028 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3029
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003030 // FIXME: Avoid std::string copying.
3031 if (!Entry)
3032 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3033 llvm::ConstantArray::get(Sel.getAsString()),
3034 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003035 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003036
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003037 return getConstantGEP(Entry, 0, 0);
3038}
3039
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003040// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003041llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003042 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3043}
3044
3045// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003046llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003047 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3048}
3049
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003050llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003051 std::string TypeStr;
3052 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3053
3054 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003055
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003056 if (!Entry)
3057 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3058 llvm::ConstantArray::get(TypeStr),
3059 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003060 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003061
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003062 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003063}
3064
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003065llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003066 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003067 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3068 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003069
3070 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3071
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003072 if (!Entry)
3073 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3074 llvm::ConstantArray::get(TypeStr),
3075 "__TEXT,__cstring,cstring_literals",
3076 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003077
3078 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003079}
3080
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003081// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003082llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003083 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3084
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003085 if (!Entry)
3086 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3087 llvm::ConstantArray::get(Ident->getName()),
3088 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003089 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003090
3091 return getConstantGEP(Entry, 0, 0);
3092}
3093
3094// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003095// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003096llvm::Constant *
3097 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3098 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003099 std::string TypeStr;
3100 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003101 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3102}
3103
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003104void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3105 const ObjCContainerDecl *CD,
3106 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003107 NameOut = '\01';
3108 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003109 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003110 assert (CD && "Missing container decl in GetNameForMethod");
3111 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003112 if (const ObjCCategoryImplDecl *CID =
3113 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3114 NameOut += '(';
3115 NameOut += CID->getNameAsString();
3116 NameOut+= ')';
3117 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003118 NameOut += ' ';
3119 NameOut += D->getSelector().getAsString();
3120 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003121}
3122
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003123void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003124 EmitModuleInfo();
3125
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003126 // Emit the dummy bodies for any protocols which were referenced but
3127 // never defined.
3128 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3129 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3130 if (i->second->hasInitializer())
3131 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003132
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003133 std::vector<llvm::Constant*> Values(5);
3134 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3135 Values[1] = GetClassName(i->first);
3136 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3137 Values[3] = Values[4] =
3138 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3139 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3140 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3141 Values));
3142 }
3143
3144 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003145 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003146 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003147 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003148 }
3149
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003150 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003151 llvm::GlobalValue *GV =
3152 new llvm::GlobalVariable(AT, false,
3153 llvm::GlobalValue::AppendingLinkage,
3154 llvm::ConstantArray::get(AT, Used),
3155 "llvm.used",
3156 &CGM.getModule());
3157
3158 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003159
3160 // Add assembler directives to add lazy undefined symbol references
3161 // for classes which are referenced but not defined. This is
3162 // important for correct linker interaction.
3163
3164 // FIXME: Uh, this isn't particularly portable.
3165 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003166
3167 if (!CGM.getModule().getModuleInlineAsm().empty())
3168 s << "\n";
3169
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003170 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3171 e = LazySymbols.end(); i != e; ++i) {
3172 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3173 }
3174 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3175 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003176 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003177 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3178 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003179
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003180 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003181}
3182
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003183CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003184 : CGObjCCommonMac(cgm),
3185 ObjCTypes(cgm)
3186{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003187 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003188 ObjCABI = 2;
3189}
3190
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003191/* *** */
3192
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003193ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3194: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003195{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3197 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003198
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003199 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003200 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003201 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003202 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003203 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3204
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003205 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003206 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003207 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003208
3209 // FIXME: It would be nice to unify this with the opaque type, so
3210 // that the IR comes out a bit cleaner.
3211 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3212 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003213
3214 // I'm not sure I like this. The implicit coordination is a bit
3215 // gross. We should solve this in a reasonable fashion because this
3216 // is a pretty common task (match some runtime data structure with
3217 // an LLVM data structure).
3218
3219 // FIXME: This is leaked.
3220 // FIXME: Merge with rewriter code?
3221
3222 // struct _objc_super {
3223 // id self;
3224 // Class cls;
3225 // }
3226 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3227 SourceLocation(),
3228 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003229 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3230 Ctx.getObjCIdType(), 0, false));
3231 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3232 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003233 RD->completeDefinition(Ctx);
3234
3235 SuperCTy = Ctx.getTagDeclType(RD);
3236 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3237
3238 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003239 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3240
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003241 // struct _prop_t {
3242 // char *name;
3243 // char *attributes;
3244 // }
3245 PropertyTy = llvm::StructType::get(Int8PtrTy,
3246 Int8PtrTy,
3247 NULL);
3248 CGM.getModule().addTypeName("struct._prop_t",
3249 PropertyTy);
3250
3251 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003252 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003253 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003254 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003255 // }
3256 PropertyListTy = llvm::StructType::get(IntTy,
3257 IntTy,
3258 llvm::ArrayType::get(PropertyTy, 0),
3259 NULL);
3260 CGM.getModule().addTypeName("struct._prop_list_t",
3261 PropertyListTy);
3262 // struct _prop_list_t *
3263 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3264
3265 // struct _objc_method {
3266 // SEL _cmd;
3267 // char *method_type;
3268 // char *_imp;
3269 // }
3270 MethodTy = llvm::StructType::get(SelectorPtrTy,
3271 Int8PtrTy,
3272 Int8PtrTy,
3273 NULL);
3274 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003275
3276 // struct _objc_cache *
3277 CacheTy = llvm::OpaqueType::get();
3278 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3279 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003280
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003281 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003282
3283 QualType IdType = Ctx.getObjCIdType();
3284 QualType SelType = Ctx.getObjCSelType();
3285 llvm::SmallVector<QualType,16> Params;
3286 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003287
3288 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003289 Params.push_back(IdType);
3290 Params.push_back(SelType);
3291 Params.push_back(Ctx.LongTy);
3292 Params.push_back(Ctx.BoolTy);
3293 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
3294 false);
3295 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003296
3297 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
3298 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003299 Params.push_back(IdType);
3300 Params.push_back(SelType);
3301 Params.push_back(Ctx.LongTy);
3302 Params.push_back(IdType);
3303 Params.push_back(Ctx.BoolTy);
3304 Params.push_back(Ctx.BoolTy);
3305 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3306 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
3307
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003308 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003309
3310 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003311 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003312 Params.push_back(IdType);
3313 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
3314 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
3315 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003316
3317 // gc's API
3318 // id objc_read_weak (id *)
3319 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003320 Params.push_back(Ctx.getPointerType(IdType));
3321 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
3322 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
3323
Chris Lattner96508e12009-04-17 22:12:36 +00003324 // id objc_assign_global (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003325 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003326 Params.push_back(IdType);
3327 Params.push_back(Ctx.getPointerType(IdType));
3328
3329 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
Daniel Dunbar34c94a22009-02-04 00:44:42 +00003330 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
3331 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
3332 GcAssignStrongCastFn =
3333 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003334
3335 // void objc_exception_throw(id)
3336 Params.clear();
3337 Params.push_back(IdType);
3338
3339 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00003340 ExceptionThrowFn =
3341 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
Daniel Dunbar1c566672009-02-24 01:43:46 +00003342
3343 // synchronized APIs
Daniel Dunbar1c566672009-02-24 01:43:46 +00003344 // void objc_sync_exit (id)
3345 Params.clear();
3346 Params.push_back(IdType);
3347
3348 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003349 SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003350}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003351
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003352ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3353 : ObjCCommonTypesHelper(cgm)
3354{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003355 // struct _objc_method_description {
3356 // SEL name;
3357 // char *types;
3358 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003359 MethodDescriptionTy =
3360 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003361 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003362 NULL);
3363 CGM.getModule().addTypeName("struct._objc_method_description",
3364 MethodDescriptionTy);
3365
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003366 // struct _objc_method_description_list {
3367 // int count;
3368 // struct _objc_method_description[1];
3369 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003370 MethodDescriptionListTy =
3371 llvm::StructType::get(IntTy,
3372 llvm::ArrayType::get(MethodDescriptionTy, 0),
3373 NULL);
3374 CGM.getModule().addTypeName("struct._objc_method_description_list",
3375 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003376
3377 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003378 MethodDescriptionListPtrTy =
3379 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3380
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003381 // Protocol description structures
3382
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003383 // struct _objc_protocol_extension {
3384 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3385 // struct _objc_method_description_list *optional_instance_methods;
3386 // struct _objc_method_description_list *optional_class_methods;
3387 // struct _objc_property_list *instance_properties;
3388 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003389 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003390 llvm::StructType::get(IntTy,
3391 MethodDescriptionListPtrTy,
3392 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003393 PropertyListPtrTy,
3394 NULL);
3395 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3396 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003397
3398 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003399 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3400
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003401 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003402
3403 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3404 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3405
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003406 const llvm::Type *T =
3407 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3408 LongTy,
3409 llvm::ArrayType::get(ProtocolTyHolder, 0),
3410 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003411 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3412
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003413 // struct _objc_protocol {
3414 // struct _objc_protocol_extension *isa;
3415 // char *protocol_name;
3416 // struct _objc_protocol **_objc_protocol_list;
3417 // struct _objc_method_description_list *instance_methods;
3418 // struct _objc_method_description_list *class_methods;
3419 // }
3420 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003421 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003422 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3423 MethodDescriptionListPtrTy,
3424 MethodDescriptionListPtrTy,
3425 NULL);
3426 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3427
3428 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3429 CGM.getModule().addTypeName("struct._objc_protocol_list",
3430 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003431 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003432 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3433
3434 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003435 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003436 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003437
3438 // Class description structures
3439
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003440 // struct _objc_ivar {
3441 // char *ivar_name;
3442 // char *ivar_type;
3443 // int ivar_offset;
3444 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003445 IvarTy = llvm::StructType::get(Int8PtrTy,
3446 Int8PtrTy,
3447 IntTy,
3448 NULL);
3449 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3450
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003451 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003452 IvarListTy = llvm::OpaqueType::get();
3453 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3454 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3455
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003456 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003457 MethodListTy = llvm::OpaqueType::get();
3458 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3459 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3460
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003461 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003462 ClassExtensionTy =
3463 llvm::StructType::get(IntTy,
3464 Int8PtrTy,
3465 PropertyListPtrTy,
3466 NULL);
3467 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3468 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3469
3470 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3471
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003472 // struct _objc_class {
3473 // Class isa;
3474 // Class super_class;
3475 // char *name;
3476 // long version;
3477 // long info;
3478 // long instance_size;
3479 // struct _objc_ivar_list *ivars;
3480 // struct _objc_method_list *methods;
3481 // struct _objc_cache *cache;
3482 // struct _objc_protocol_list *protocols;
3483 // char *ivar_layout;
3484 // struct _objc_class_ext *ext;
3485 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003486 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3487 llvm::PointerType::getUnqual(ClassTyHolder),
3488 Int8PtrTy,
3489 LongTy,
3490 LongTy,
3491 LongTy,
3492 IvarListPtrTy,
3493 MethodListPtrTy,
3494 CachePtrTy,
3495 ProtocolListPtrTy,
3496 Int8PtrTy,
3497 ClassExtensionPtrTy,
3498 NULL);
3499 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3500
3501 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3502 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3503 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3504
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003505 // struct _objc_category {
3506 // char *category_name;
3507 // char *class_name;
3508 // struct _objc_method_list *instance_method;
3509 // struct _objc_method_list *class_method;
3510 // uint32_t size; // sizeof(struct _objc_category)
3511 // struct _objc_property_list *instance_properties;// category's @property
3512 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003513 CategoryTy = llvm::StructType::get(Int8PtrTy,
3514 Int8PtrTy,
3515 MethodListPtrTy,
3516 MethodListPtrTy,
3517 ProtocolListPtrTy,
3518 IntTy,
3519 PropertyListPtrTy,
3520 NULL);
3521 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3522
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003523 // Global metadata structures
3524
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003525 // struct _objc_symtab {
3526 // long sel_ref_cnt;
3527 // SEL *refs;
3528 // short cls_def_cnt;
3529 // short cat_def_cnt;
3530 // char *defs[cls_def_cnt + cat_def_cnt];
3531 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003532 SymtabTy = llvm::StructType::get(LongTy,
3533 SelectorPtrTy,
3534 ShortTy,
3535 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003536 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003537 NULL);
3538 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3539 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3540
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003541 // struct _objc_module {
3542 // long version;
3543 // long size; // sizeof(struct _objc_module)
3544 // char *name;
3545 // struct _objc_symtab* symtab;
3546 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003547 ModuleTy =
3548 llvm::StructType::get(LongTy,
3549 LongTy,
3550 Int8PtrTy,
3551 SymtabPtrTy,
3552 NULL);
3553 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003554
Daniel Dunbar49f66022008-09-24 03:38:44 +00003555 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003556
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003557 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003558 std::vector<const llvm::Type*> Params;
3559 Params.push_back(ObjectPtrTy);
3560 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003561 MessageSendFn =
3562 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3563 Params,
3564 true),
3565 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003566
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003567 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003568 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003569 Params.push_back(ObjectPtrTy);
3570 Params.push_back(SelectorPtrTy);
3571 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003572 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3573 Params,
3574 true),
3575 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003576
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003577 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00003578 Params.clear();
3579 Params.push_back(ObjectPtrTy);
3580 Params.push_back(SelectorPtrTy);
3581 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003582 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00003583 MessageSendFpretFn =
3584 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
3585 Params,
3586 true),
3587 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003588
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003589 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003590 Params.clear();
3591 Params.push_back(SuperPtrTy);
3592 Params.push_back(SelectorPtrTy);
3593 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003594 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3595 Params,
3596 true),
3597 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003598
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003599 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
3600 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003601 Params.clear();
3602 Params.push_back(Int8PtrTy);
3603 Params.push_back(SuperPtrTy);
3604 Params.push_back(SelectorPtrTy);
3605 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003606 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3607 Params,
3608 true),
3609 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003610
3611 // There is no objc_msgSendSuper_fpret? How can that work?
3612 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003613
Anders Carlsson124526b2008-09-09 10:10:21 +00003614 // FIXME: This is the size of the setjmp buffer and should be
3615 // target specific. 18 is what's used on 32-bit X86.
3616 uint64_t SetJmpBufferSize = 18;
3617
3618 // Exceptions
3619 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003620 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003621
3622 ExceptionDataTy =
3623 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3624 SetJmpBufferSize),
3625 StackPtrTy, NULL);
3626 CGM.getModule().addTypeName("struct._objc_exception_data",
3627 ExceptionDataTy);
3628
3629 Params.clear();
Anders Carlsson124526b2008-09-09 10:10:21 +00003630 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3631 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003632 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3633 Params,
3634 false),
3635 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003636 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003637 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3638 Params,
3639 false),
3640 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003641 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003642 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3643 Params,
3644 false),
3645 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003646
3647 Params.clear();
3648 Params.push_back(ClassPtrTy);
3649 Params.push_back(ObjectPtrTy);
3650 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003651 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3652 Params,
3653 false),
3654 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003655
Anders Carlsson124526b2008-09-09 10:10:21 +00003656 Params.clear();
3657 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3658 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003659 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3660 Params,
3661 false),
3662 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003663
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003664}
3665
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003666ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003667: ObjCCommonTypesHelper(cgm)
3668{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003669 // struct _method_list_t {
3670 // uint32_t entsize; // sizeof(struct _objc_method)
3671 // uint32_t method_count;
3672 // struct _objc_method method_list[method_count];
3673 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003674 MethodListnfABITy = llvm::StructType::get(IntTy,
3675 IntTy,
3676 llvm::ArrayType::get(MethodTy, 0),
3677 NULL);
3678 CGM.getModule().addTypeName("struct.__method_list_t",
3679 MethodListnfABITy);
3680 // struct method_list_t *
3681 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003682
3683 // struct _protocol_t {
3684 // id isa; // NULL
3685 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003686 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003687 // const struct method_list_t * const instance_methods;
3688 // const struct method_list_t * const class_methods;
3689 // const struct method_list_t *optionalInstanceMethods;
3690 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003691 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003692 // const uint32_t size; // sizeof(struct _protocol_t)
3693 // const uint32_t flags; // = 0
3694 // }
3695
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003696 // Holder for struct _protocol_list_t *
3697 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3698
3699 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3700 Int8PtrTy,
3701 llvm::PointerType::getUnqual(
3702 ProtocolListTyHolder),
3703 MethodListnfABIPtrTy,
3704 MethodListnfABIPtrTy,
3705 MethodListnfABIPtrTy,
3706 MethodListnfABIPtrTy,
3707 PropertyListPtrTy,
3708 IntTy,
3709 IntTy,
3710 NULL);
3711 CGM.getModule().addTypeName("struct._protocol_t",
3712 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003713
3714 // struct _protocol_t*
3715 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003716
Fariborz Jahanianda320092009-01-29 19:24:30 +00003717 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003718 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003719 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003720 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003721 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3722 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003723 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003724 NULL);
3725 CGM.getModule().addTypeName("struct._objc_protocol_list",
3726 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003727 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3728 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003729
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003730 // struct _objc_protocol_list*
3731 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003732
3733 // struct _ivar_t {
3734 // unsigned long int *offset; // pointer to ivar offset location
3735 // char *name;
3736 // char *type;
3737 // uint32_t alignment;
3738 // uint32_t size;
3739 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003740 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3741 Int8PtrTy,
3742 Int8PtrTy,
3743 IntTy,
3744 IntTy,
3745 NULL);
3746 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3747
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003748 // struct _ivar_list_t {
3749 // uint32 entsize; // sizeof(struct _ivar_t)
3750 // uint32 count;
3751 // struct _iver_t list[count];
3752 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003753 IvarListnfABITy = llvm::StructType::get(IntTy,
3754 IntTy,
3755 llvm::ArrayType::get(
3756 IvarnfABITy, 0),
3757 NULL);
3758 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3759
3760 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003761
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003762 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003763 // uint32_t const flags;
3764 // uint32_t const instanceStart;
3765 // uint32_t const instanceSize;
3766 // uint32_t const reserved; // only when building for 64bit targets
3767 // const uint8_t * const ivarLayout;
3768 // const char *const name;
3769 // const struct _method_list_t * const baseMethods;
3770 // const struct _objc_protocol_list *const baseProtocols;
3771 // const struct _ivar_list_t *const ivars;
3772 // const uint8_t * const weakIvarLayout;
3773 // const struct _prop_list_t * const properties;
3774 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003775
3776 // FIXME. Add 'reserved' field in 64bit abi mode!
3777 ClassRonfABITy = llvm::StructType::get(IntTy,
3778 IntTy,
3779 IntTy,
3780 Int8PtrTy,
3781 Int8PtrTy,
3782 MethodListnfABIPtrTy,
3783 ProtocolListnfABIPtrTy,
3784 IvarListnfABIPtrTy,
3785 Int8PtrTy,
3786 PropertyListPtrTy,
3787 NULL);
3788 CGM.getModule().addTypeName("struct._class_ro_t",
3789 ClassRonfABITy);
3790
3791 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3792 std::vector<const llvm::Type*> Params;
3793 Params.push_back(ObjectPtrTy);
3794 Params.push_back(SelectorPtrTy);
3795 ImpnfABITy = llvm::PointerType::getUnqual(
3796 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3797
3798 // struct _class_t {
3799 // struct _class_t *isa;
3800 // struct _class_t * const superclass;
3801 // void *cache;
3802 // IMP *vtable;
3803 // struct class_ro_t *ro;
3804 // }
3805
3806 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3807 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3808 llvm::PointerType::getUnqual(ClassTyHolder),
3809 CachePtrTy,
3810 llvm::PointerType::getUnqual(ImpnfABITy),
3811 llvm::PointerType::getUnqual(
3812 ClassRonfABITy),
3813 NULL);
3814 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3815
3816 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3817 ClassnfABITy);
3818
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003819 // LLVM for struct _class_t *
3820 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3821
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003822 // struct _category_t {
3823 // const char * const name;
3824 // struct _class_t *const cls;
3825 // const struct _method_list_t * const instance_methods;
3826 // const struct _method_list_t * const class_methods;
3827 // const struct _protocol_list_t * const protocols;
3828 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003829 // }
3830 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003831 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003832 MethodListnfABIPtrTy,
3833 MethodListnfABIPtrTy,
3834 ProtocolListnfABIPtrTy,
3835 PropertyListPtrTy,
3836 NULL);
3837 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003838
3839 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003840 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3841 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003842
3843 // MessageRefTy - LLVM for:
3844 // struct _message_ref_t {
3845 // IMP messenger;
3846 // SEL name;
3847 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003848
3849 // First the clang type for struct _message_ref_t
3850 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3851 SourceLocation(),
3852 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003853 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3854 Ctx.VoidPtrTy, 0, false));
3855 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3856 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003857 RD->completeDefinition(Ctx);
3858
3859 MessageRefCTy = Ctx.getTagDeclType(RD);
3860 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3861 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003862
3863 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3864 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3865
3866 // SuperMessageRefTy - LLVM for:
3867 // struct _super_message_ref_t {
3868 // SUPER_IMP messenger;
3869 // SEL name;
3870 // };
3871 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3872 SelectorPtrTy,
3873 NULL);
3874 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3875
3876 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3877 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3878
3879 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3880 Params.clear();
3881 Params.push_back(ObjectPtrTy);
3882 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003883 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3884 Params,
3885 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003886 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003887 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003888 "objc_msgSend_fixup");
3889
3890 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3891 MessageSendFpretFixupFn =
3892 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3893 Params,
3894 true),
3895 "objc_msgSend_fpret_fixup");
3896
3897 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3898 MessageSendStretFixupFn =
3899 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3900 Params,
3901 true),
3902 "objc_msgSend_stret_fixup");
3903
3904 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3905 MessageSendIdFixupFn =
3906 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3907 Params,
3908 true),
3909 "objc_msgSendId_fixup");
3910
3911
3912 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3913 MessageSendIdStretFixupFn =
3914 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3915 Params,
3916 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003917 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003918
3919 // id objc_msgSendSuper2_fixup (struct objc_super *,
3920 // struct _super_message_ref_t*, ...)
3921 Params.clear();
3922 Params.push_back(SuperPtrTy);
3923 Params.push_back(SuperMessageRefPtrTy);
3924 MessageSendSuper2FixupFn =
3925 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3926 Params,
3927 true),
3928 "objc_msgSendSuper2_fixup");
3929
3930
3931 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3932 // struct _super_message_ref_t*, ...)
3933 MessageSendSuper2StretFixupFn =
3934 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3935 Params,
3936 true),
3937 "objc_msgSendSuper2_stret_fixup");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00003938
3939 Params.clear();
Daniel Dunbare588b992009-03-01 04:46:24 +00003940
3941 // struct objc_typeinfo {
3942 // const void** vtable; // objc_ehtype_vtable + 2
3943 // const char* name; // c++ typeinfo string
3944 // Class cls;
3945 // };
3946 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
3947 Int8PtrTy,
3948 ClassnfABIPtrTy,
3949 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00003950 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00003951 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003952}
3953
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003954llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3955 FinishNonFragileABIModule();
3956
3957 return NULL;
3958}
3959
3960void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3961 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003962
3963 // Build list of all implemented classe addresses in array
3964 // L_OBJC_LABEL_CLASS_$.
3965 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3966 // list of 'nonlazy' implementations (defined as those with a +load{}
3967 // method!!).
3968 unsigned NumClasses = DefinedClasses.size();
3969 if (NumClasses) {
3970 std::vector<llvm::Constant*> Symbols(NumClasses);
3971 for (unsigned i=0; i<NumClasses; i++)
3972 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3973 ObjCTypes.Int8PtrTy);
3974 llvm::Constant* Init =
3975 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3976 NumClasses),
3977 Symbols);
3978
3979 llvm::GlobalVariable *GV =
3980 new llvm::GlobalVariable(Init->getType(), false,
3981 llvm::GlobalValue::InternalLinkage,
3982 Init,
3983 "\01L_OBJC_LABEL_CLASS_$",
3984 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00003985 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003986 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3987 UsedGlobals.push_back(GV);
3988 }
3989
3990 // Build list of all implemented category addresses in array
3991 // L_OBJC_LABEL_CATEGORY_$.
3992 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3993 // list of 'nonlazy' category implementations (defined as those with a +load{}
3994 // method!!).
3995 unsigned NumCategory = DefinedCategories.size();
3996 if (NumCategory) {
3997 std::vector<llvm::Constant*> Symbols(NumCategory);
3998 for (unsigned i=0; i<NumCategory; i++)
3999 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
4000 ObjCTypes.Int8PtrTy);
4001 llvm::Constant* Init =
4002 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4003 NumCategory),
4004 Symbols);
4005
4006 llvm::GlobalVariable *GV =
4007 new llvm::GlobalVariable(Init->getType(), false,
4008 llvm::GlobalValue::InternalLinkage,
4009 Init,
4010 "\01L_OBJC_LABEL_CATEGORY_$",
4011 &CGM.getModule());
Daniel Dunbar58a29122009-03-09 22:18:41 +00004012 GV->setAlignment(8);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004013 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
4014 UsedGlobals.push_back(GV);
4015 }
4016
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004017 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4018 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4019 std::vector<llvm::Constant*> Values(2);
4020 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004021 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004022 // FIXME: Fix and continue?
4023 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4024 flags |= eImageInfo_GarbageCollected;
4025 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4026 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004027 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004028 llvm::Constant* Init = llvm::ConstantArray::get(
4029 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4030 Values);
4031 llvm::GlobalVariable *IMGV =
4032 new llvm::GlobalVariable(Init->getType(), false,
4033 llvm::GlobalValue::InternalLinkage,
4034 Init,
4035 "\01L_OBJC_IMAGE_INFO",
4036 &CGM.getModule());
4037 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
4038 UsedGlobals.push_back(IMGV);
4039
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004040 std::vector<llvm::Constant*> Used;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004041
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004042 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
4043 e = UsedGlobals.end(); i != e; ++i) {
4044 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
4045 }
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004046
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004047 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
4048 llvm::GlobalValue *GV =
4049 new llvm::GlobalVariable(AT, false,
4050 llvm::GlobalValue::AppendingLinkage,
4051 llvm::ConstantArray::get(AT, Used),
4052 "llvm.used",
4053 &CGM.getModule());
4054
4055 GV->setSection("llvm.metadata");
4056
4057}
4058
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004059// Metadata flags
4060enum MetaDataDlags {
4061 CLS = 0x0,
4062 CLS_META = 0x1,
4063 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004064 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004065 CLS_EXCEPTION = 0x20
4066};
4067/// BuildClassRoTInitializer - generate meta-data for:
4068/// struct _class_ro_t {
4069/// uint32_t const flags;
4070/// uint32_t const instanceStart;
4071/// uint32_t const instanceSize;
4072/// uint32_t const reserved; // only when building for 64bit targets
4073/// const uint8_t * const ivarLayout;
4074/// const char *const name;
4075/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004076/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004077/// const struct _ivar_list_t *const ivars;
4078/// const uint8_t * const weakIvarLayout;
4079/// const struct _prop_list_t * const properties;
4080/// }
4081///
4082llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4083 unsigned flags,
4084 unsigned InstanceStart,
4085 unsigned InstanceSize,
4086 const ObjCImplementationDecl *ID) {
4087 std::string ClassName = ID->getNameAsString();
4088 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4089 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4090 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4091 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4092 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00004093 // FIXME. ivarLayout is currently null!
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004094 // Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4095 // : BuildIvarLayout(ID, true);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004096 Values[ 3] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004097 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004098 // const struct _method_list_t * const baseMethods;
4099 std::vector<llvm::Constant*> Methods;
4100 std::string MethodListName("\01l_OBJC_$_");
4101 if (flags & CLS_META) {
4102 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4103 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
4104 e = ID->classmeth_end(); i != e; ++i) {
4105 // Class methods should always be defined.
4106 Methods.push_back(GetMethodConstant(*i));
4107 }
4108 } else {
4109 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4110 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
4111 e = ID->instmeth_end(); i != e; ++i) {
4112 // Instance methods should always be defined.
4113 Methods.push_back(GetMethodConstant(*i));
4114 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004115 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
4116 e = ID->propimpl_end(); i != e; ++i) {
4117 ObjCPropertyImplDecl *PID = *i;
4118
4119 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4120 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4121
4122 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4123 if (llvm::Constant *C = GetMethodConstant(MD))
4124 Methods.push_back(C);
4125 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4126 if (llvm::Constant *C = GetMethodConstant(MD))
4127 Methods.push_back(C);
4128 }
4129 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004130 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004131 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004132 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004133
4134 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4135 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4136 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4137 + OID->getNameAsString(),
4138 OID->protocol_begin(),
4139 OID->protocol_end());
4140
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004141 if (flags & CLS_META)
4142 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4143 else
4144 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004145 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004146 // Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4147 // : BuildIvarLayout(ID, false);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004148 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004149 if (flags & CLS_META)
4150 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4151 else
4152 Values[ 9] =
4153 EmitPropertyList(
4154 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4155 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004156 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4157 Values);
4158 llvm::GlobalVariable *CLASS_RO_GV =
4159 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4160 llvm::GlobalValue::InternalLinkage,
4161 Init,
4162 (flags & CLS_META) ?
4163 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4164 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4165 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004166 CLASS_RO_GV->setAlignment(
4167 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004168 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004169 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004170
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004171}
4172
4173/// BuildClassMetaData - This routine defines that to-level meta-data
4174/// for the given ClassName for:
4175/// struct _class_t {
4176/// struct _class_t *isa;
4177/// struct _class_t * const superclass;
4178/// void *cache;
4179/// IMP *vtable;
4180/// struct class_ro_t *ro;
4181/// }
4182///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004183llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4184 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004185 llvm::Constant *IsAGV,
4186 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004187 llvm::Constant *ClassRoGV,
4188 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004189 std::vector<llvm::Constant*> Values(5);
4190 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004191 Values[1] = SuperClassGV
4192 ? SuperClassGV
4193 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004194 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4195 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4196 Values[4] = ClassRoGV; // &CLASS_RO_GV
4197 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4198 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004199 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4200 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004201 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004202 GV->setAlignment(
4203 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004204 if (HiddenVisibility)
4205 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004206 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004207}
4208
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004209/// countInheritedIvars - count number of ivars in class and its super class(s)
4210///
4211static int countInheritedIvars(const ObjCInterfaceDecl *OI,
4212 ASTContext &Context) {
4213 int count = 0;
4214 if (!OI)
4215 return 0;
4216 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
4217 if (SuperClass)
4218 count += countInheritedIvars(SuperClass, Context);
4219 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
4220 E = OI->ivar_end(); I != E; ++I)
4221 ++count;
4222 // look into properties.
4223 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context),
4224 E = OI->prop_end(Context); I != E; ++I) {
4225 if ((*I)->getPropertyIvarDecl())
4226 ++count;
4227 }
4228 return count;
4229}
4230
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004231void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID,
4232 uint32_t &InstanceStart,
4233 uint32_t &InstanceSize) {
Daniel Dunbar24c89912009-04-21 21:41:56 +00004234 assert(!OID->isForwardDecl() && "Invalid interface decl!");
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004235 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
4236
Daniel Dunbar6ec07162009-04-20 07:18:49 +00004237 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(),
4238 CGM.getContext());
4239 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
4240 RecordDecl::field_iterator firstField = RD->field_begin(CGM.getContext());
4241 RecordDecl::field_iterator lastField = RD->field_end(CGM.getContext());
4242 while (countSuperClassIvars-- > 0) {
4243 lastField = firstField;
4244 ++firstField;
4245 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004246
4247 for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()),
4248 ifield = firstField; ifield != e; ++ifield)
4249 lastField = ifield;
4250
4251 InstanceStart = InstanceSize = 0;
4252 if (lastField != RD->field_end(CGM.getContext())) {
4253 FieldDecl *Field = *lastField;
4254 const llvm::Type *FieldTy =
4255 CGM.getTypes().ConvertTypeForMem(Field->getType());
4256 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4257 InstanceSize = GetIvarBaseOffset(Layout, Field) + Size;
4258 if (firstField == RD->field_end(CGM.getContext()))
4259 InstanceStart = InstanceSize;
4260 else {
4261 Field = *firstField;
4262 InstanceStart = GetIvarBaseOffset(Layout, Field);
4263 }
4264 }
4265}
4266
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004267void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4268 std::string ClassName = ID->getNameAsString();
4269 if (!ObjCEmptyCacheVar) {
4270 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004271 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004272 false,
4273 llvm::GlobalValue::ExternalLinkage,
4274 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004275 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004276 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004277
4278 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004279 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004280 false,
4281 llvm::GlobalValue::ExternalLinkage,
4282 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004283 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004284 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004285 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004286 assert(ID->getClassInterface() &&
4287 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004288 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004289 uint32_t InstanceStart =
4290 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
4291 uint32_t InstanceSize = InstanceStart;
4292 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004293 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4294 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004295
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004296 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004297
Daniel Dunbar04d40782009-04-14 06:00:08 +00004298 bool classIsHidden =
4299 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004300 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004301 flags |= OBJC2_CLS_HIDDEN;
4302 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004303 // class is root
4304 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004305 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004306 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004307 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004308 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004309 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4310 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4311 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004312 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004313 // work on super class metadata symbol.
4314 std::string SuperClassName =
4315 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004316 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004317 }
4318 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4319 InstanceStart,
4320 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004321 std::string TClassName = ObjCMetaClassName + ClassName;
4322 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004323 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4324 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004325
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004326 // Metadata for the class
4327 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004328 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004329 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004330
4331 if (hasObjCExceptionAttribute(ID->getClassInterface()))
4332 flags |= CLS_EXCEPTION;
4333
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004334 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004335 flags |= CLS_ROOT;
4336 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004337 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004338 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004339 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004340 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004341 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004342 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004343 GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004344 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004345 InstanceStart,
4346 InstanceSize,
4347 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004348
4349 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004350 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004351 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4352 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004353 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004354
4355 // Force the definition of the EHType if necessary.
4356 if (flags & CLS_EXCEPTION)
4357 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004358}
4359
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004360/// GenerateProtocolRef - This routine is called to generate code for
4361/// a protocol reference expression; as in:
4362/// @code
4363/// @protocol(Proto1);
4364/// @endcode
4365/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4366/// which will hold address of the protocol meta-data.
4367///
4368llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4369 const ObjCProtocolDecl *PD) {
4370
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004371 // This routine is called for @protocol only. So, we must build definition
4372 // of protocol's meta-data (not a reference to it!)
4373 //
4374 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004375 ObjCTypes.ExternalProtocolPtrTy);
4376
4377 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4378 ProtocolName += PD->getNameAsCString();
4379
4380 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4381 if (PTGV)
4382 return Builder.CreateLoad(PTGV, false, "tmp");
4383 PTGV = new llvm::GlobalVariable(
4384 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004385 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004386 Init,
4387 ProtocolName,
4388 &CGM.getModule());
4389 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4390 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4391 UsedGlobals.push_back(PTGV);
4392 return Builder.CreateLoad(PTGV, false, "tmp");
4393}
4394
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004395/// GenerateCategory - Build metadata for a category implementation.
4396/// struct _category_t {
4397/// const char * const name;
4398/// struct _class_t *const cls;
4399/// const struct _method_list_t * const instance_methods;
4400/// const struct _method_list_t * const class_methods;
4401/// const struct _protocol_list_t * const protocols;
4402/// const struct _prop_list_t * const properties;
4403/// }
4404///
4405void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
4406{
4407 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004408 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4409 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004410 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004411 std::string ExtClassName(getClassSymbolPrefix() +
4412 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004413
4414 std::vector<llvm::Constant*> Values(6);
4415 Values[0] = GetClassName(OCD->getIdentifier());
4416 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004417 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004418 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004419 std::vector<llvm::Constant*> Methods;
4420 std::string MethodListName(Prefix);
4421 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4422 "_$_" + OCD->getNameAsString();
4423
4424 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
4425 e = OCD->instmeth_end(); i != e; ++i) {
4426 // Instance methods should always be defined.
4427 Methods.push_back(GetMethodConstant(*i));
4428 }
4429
4430 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004431 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004432 Methods);
4433
4434 MethodListName = Prefix;
4435 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4436 OCD->getNameAsString();
4437 Methods.clear();
4438 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
4439 e = OCD->classmeth_end(); i != e; ++i) {
4440 // Class methods should always be defined.
4441 Methods.push_back(GetMethodConstant(*i));
4442 }
4443
4444 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004445 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004446 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004447 const ObjCCategoryDecl *Category =
4448 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004449 if (Category) {
4450 std::string ExtName(Interface->getNameAsString() + "_$_" +
4451 OCD->getNameAsString());
4452 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4453 + Interface->getNameAsString() + "_$_"
4454 + Category->getNameAsString(),
4455 Category->protocol_begin(),
4456 Category->protocol_end());
4457 Values[5] =
4458 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4459 OCD, Category, ObjCTypes);
4460 }
4461 else {
4462 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4463 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4464 }
4465
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004466 llvm::Constant *Init =
4467 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4468 Values);
4469 llvm::GlobalVariable *GCATV
4470 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4471 false,
4472 llvm::GlobalValue::InternalLinkage,
4473 Init,
4474 ExtCatName,
4475 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004476 GCATV->setAlignment(
4477 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004478 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004479 UsedGlobals.push_back(GCATV);
4480 DefinedCategories.push_back(GCATV);
4481}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004482
4483/// GetMethodConstant - Return a struct objc_method constant for the
4484/// given method if it has been defined. The result is null if the
4485/// method has not been defined. The return value has type MethodPtrTy.
4486llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4487 const ObjCMethodDecl *MD) {
4488 // FIXME: Use DenseMap::lookup
4489 llvm::Function *Fn = MethodDefinitions[MD];
4490 if (!Fn)
4491 return 0;
4492
4493 std::vector<llvm::Constant*> Method(3);
4494 Method[0] =
4495 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4496 ObjCTypes.SelectorPtrTy);
4497 Method[1] = GetMethodVarType(MD);
4498 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4499 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4500}
4501
4502/// EmitMethodList - Build meta-data for method declarations
4503/// struct _method_list_t {
4504/// uint32_t entsize; // sizeof(struct _objc_method)
4505/// uint32_t method_count;
4506/// struct _objc_method method_list[method_count];
4507/// }
4508///
4509llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4510 const std::string &Name,
4511 const char *Section,
4512 const ConstantVector &Methods) {
4513 // Return null for empty list.
4514 if (Methods.empty())
4515 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4516
4517 std::vector<llvm::Constant*> Values(3);
4518 // sizeof(struct _objc_method)
4519 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
4520 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4521 // method_count
4522 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4523 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4524 Methods.size());
4525 Values[2] = llvm::ConstantArray::get(AT, Methods);
4526 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4527
4528 llvm::GlobalVariable *GV =
4529 new llvm::GlobalVariable(Init->getType(), false,
4530 llvm::GlobalValue::InternalLinkage,
4531 Init,
4532 Name,
4533 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004534 GV->setAlignment(
4535 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004536 GV->setSection(Section);
4537 UsedGlobals.push_back(GV);
4538 return llvm::ConstantExpr::getBitCast(GV,
4539 ObjCTypes.MethodListnfABIPtrTy);
4540}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004541
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004542/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4543/// the given ivar.
4544///
4545llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004546 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004547 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004548 std::string Name = "OBJC_IVAR_$_" +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004549 getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() +
4550 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004551 llvm::GlobalVariable *IvarOffsetGV =
4552 CGM.getModule().getGlobalVariable(Name);
4553 if (!IvarOffsetGV)
4554 IvarOffsetGV =
4555 new llvm::GlobalVariable(ObjCTypes.LongTy,
4556 false,
4557 llvm::GlobalValue::ExternalLinkage,
4558 0,
4559 Name,
4560 &CGM.getModule());
4561 return IvarOffsetGV;
4562}
4563
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004564llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004565 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004566 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004567 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004568 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4569 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4570 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004571 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004572 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004573
4574 // FIXME: This matches gcc, but shouldn't the visibility be set on
4575 // the use as well (i.e., in ObjCIvarOffsetVariable).
4576 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4577 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4578 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004579 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004580 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004581 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004582 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004583 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004584}
4585
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004586/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004587/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004588/// IvarListnfABIPtrTy.
4589/// struct _ivar_t {
4590/// unsigned long int *offset; // pointer to ivar offset location
4591/// char *name;
4592/// char *type;
4593/// uint32_t alignment;
4594/// uint32_t size;
4595/// }
4596/// struct _ivar_list_t {
4597/// uint32 entsize; // sizeof(struct _ivar_t)
4598/// uint32 count;
4599/// struct _iver_t list[count];
4600/// }
4601///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004602
4603void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID,
4604 llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const {
4605 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
4606 E = OID->ivar_end(); I != E; ++I) {
4607 // Ignore unnamed bit-fields.
4608 if (!(*I)->getDeclName())
4609 continue;
4610
4611 Res.push_back(*I);
4612 }
4613
4614 for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()),
4615 E = OID->prop_end(CGM.getContext()); I != E; ++I)
4616 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
4617 Res.push_back(IV);
4618}
4619
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004620llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4621 const ObjCImplementationDecl *ID) {
4622
4623 std::vector<llvm::Constant*> Ivars, Ivar(5);
4624
4625 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4626 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4627
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004628 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00004629 const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004630
Daniel Dunbar91636d62009-04-20 00:33:43 +00004631 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004632 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004633 GetNamedIvarList(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004634
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004635 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4636 ObjCIvarDecl *IVD = OIvars[i];
4637 const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD);
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004638 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar48fa0642009-04-19 02:03:42 +00004639 GetIvarBaseOffset(Layout, Field));
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004640 Ivar[1] = GetMethodVarName(Field->getIdentifier());
Devang Patel7794bb82009-03-04 18:21:39 +00004641 Ivar[2] = GetMethodVarType(Field);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004642 const llvm::Type *FieldTy =
4643 CGM.getTypes().ConvertTypeForMem(Field->getType());
4644 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4645 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4646 Field->getType().getTypePtr()) >> 3;
4647 Align = llvm::Log2_32(Align);
4648 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004649 // NOTE. Size of a bitfield does not match gcc's, because of the
4650 // way bitfields are treated special in each. But I am told that
4651 // 'size' for bitfield ivars is ignored by the runtime so it does
4652 // not matter. If it matters, there is enough info to get the
4653 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004654 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4655 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4656 }
4657 // Return null for empty list.
4658 if (Ivars.empty())
4659 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4660 std::vector<llvm::Constant*> Values(3);
4661 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4662 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4663 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4664 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4665 Ivars.size());
4666 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4667 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4668 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4669 llvm::GlobalVariable *GV =
4670 new llvm::GlobalVariable(Init->getType(), false,
4671 llvm::GlobalValue::InternalLinkage,
4672 Init,
4673 Prefix + OID->getNameAsString(),
4674 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004675 GV->setAlignment(
4676 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004677 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004678
4679 UsedGlobals.push_back(GV);
4680 return llvm::ConstantExpr::getBitCast(GV,
4681 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004682}
4683
4684llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4685 const ObjCProtocolDecl *PD) {
4686 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4687
4688 if (!Entry) {
4689 // We use the initializer as a marker of whether this is a forward
4690 // reference or not. At module finalization we add the empty
4691 // contents for protocols which were referenced but never defined.
4692 Entry =
4693 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4694 llvm::GlobalValue::ExternalLinkage,
4695 0,
4696 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4697 &CGM.getModule());
4698 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4699 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004700 }
4701
4702 return Entry;
4703}
4704
4705/// GetOrEmitProtocol - Generate the protocol meta-data:
4706/// @code
4707/// struct _protocol_t {
4708/// id isa; // NULL
4709/// const char * const protocol_name;
4710/// const struct _protocol_list_t * protocol_list; // super protocols
4711/// const struct method_list_t * const instance_methods;
4712/// const struct method_list_t * const class_methods;
4713/// const struct method_list_t *optionalInstanceMethods;
4714/// const struct method_list_t *optionalClassMethods;
4715/// const struct _prop_list_t * properties;
4716/// const uint32_t size; // sizeof(struct _protocol_t)
4717/// const uint32_t flags; // = 0
4718/// }
4719/// @endcode
4720///
4721
4722llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4723 const ObjCProtocolDecl *PD) {
4724 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4725
4726 // Early exit if a defining object has already been generated.
4727 if (Entry && Entry->hasInitializer())
4728 return Entry;
4729
4730 const char *ProtocolName = PD->getNameAsCString();
4731
4732 // Construct method lists.
4733 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4734 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004735 for (ObjCProtocolDecl::instmeth_iterator
4736 i = PD->instmeth_begin(CGM.getContext()),
4737 e = PD->instmeth_end(CGM.getContext());
4738 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004739 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004740 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004741 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4742 OptInstanceMethods.push_back(C);
4743 } else {
4744 InstanceMethods.push_back(C);
4745 }
4746 }
4747
Douglas Gregor6ab35242009-04-09 21:40:53 +00004748 for (ObjCProtocolDecl::classmeth_iterator
4749 i = PD->classmeth_begin(CGM.getContext()),
4750 e = PD->classmeth_end(CGM.getContext());
4751 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004752 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004753 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004754 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4755 OptClassMethods.push_back(C);
4756 } else {
4757 ClassMethods.push_back(C);
4758 }
4759 }
4760
4761 std::vector<llvm::Constant*> Values(10);
4762 // isa is NULL
4763 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4764 Values[1] = GetClassName(PD->getIdentifier());
4765 Values[2] = EmitProtocolList(
4766 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4767 PD->protocol_begin(),
4768 PD->protocol_end());
4769
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004770 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004771 + PD->getNameAsString(),
4772 "__DATA, __objc_const",
4773 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004774 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004775 + PD->getNameAsString(),
4776 "__DATA, __objc_const",
4777 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004778 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004779 + PD->getNameAsString(),
4780 "__DATA, __objc_const",
4781 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004782 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004783 + PD->getNameAsString(),
4784 "__DATA, __objc_const",
4785 OptClassMethods);
4786 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4787 0, PD, ObjCTypes);
4788 uint32_t Size =
4789 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4790 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4791 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4792 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4793 Values);
4794
4795 if (Entry) {
4796 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004797 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004798 Entry->setInitializer(Init);
4799 } else {
4800 Entry =
4801 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004802 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 Init,
4804 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4805 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004806 Entry->setAlignment(
4807 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004808 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004809 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004810 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4811
4812 // Use this protocol meta-data to build protocol list table in section
4813 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004814 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004815 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004816 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004817 Entry,
4818 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4819 +ProtocolName,
4820 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004821 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004822 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004823 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004824 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4825 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004826 return Entry;
4827}
4828
4829/// EmitProtocolList - Generate protocol list meta-data:
4830/// @code
4831/// struct _protocol_list_t {
4832/// long protocol_count; // Note, this is 32/64 bit
4833/// struct _protocol_t[protocol_count];
4834/// }
4835/// @endcode
4836///
4837llvm::Constant *
4838CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4839 ObjCProtocolDecl::protocol_iterator begin,
4840 ObjCProtocolDecl::protocol_iterator end) {
4841 std::vector<llvm::Constant*> ProtocolRefs;
4842
Fariborz Jahanianda320092009-01-29 19:24:30 +00004843 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004844 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004845 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4846
Daniel Dunbar948e2582009-02-15 07:36:20 +00004847 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004848 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4849 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004850 return llvm::ConstantExpr::getBitCast(GV,
4851 ObjCTypes.ProtocolListnfABIPtrTy);
4852
4853 for (; begin != end; ++begin)
4854 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4855
Fariborz Jahanianda320092009-01-29 19:24:30 +00004856 // This list is null terminated.
4857 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004858 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004859
4860 std::vector<llvm::Constant*> Values(2);
4861 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4862 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004863 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004864 ProtocolRefs.size()),
4865 ProtocolRefs);
4866
4867 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4868 GV = new llvm::GlobalVariable(Init->getType(), false,
4869 llvm::GlobalValue::InternalLinkage,
4870 Init,
4871 Name,
4872 &CGM.getModule());
4873 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004874 GV->setAlignment(
4875 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004876 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004877 return llvm::ConstantExpr::getBitCast(GV,
4878 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004879}
4880
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004881/// GetMethodDescriptionConstant - This routine build following meta-data:
4882/// struct _objc_method {
4883/// SEL _cmd;
4884/// char *method_type;
4885/// char *_imp;
4886/// }
4887
4888llvm::Constant *
4889CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4890 std::vector<llvm::Constant*> Desc(3);
4891 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4892 ObjCTypes.SelectorPtrTy);
4893 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004894 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004895 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4896 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4897}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004898
4899/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4900/// This code gen. amounts to generating code for:
4901/// @code
4902/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4903/// @encode
4904///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004905LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004906 CodeGen::CodeGenFunction &CGF,
4907 QualType ObjectTy,
4908 llvm::Value *BaseValue,
4909 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004910 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004911 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4912 const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004913 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004914
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004915 // (char *) BaseValue
Chris Lattner51123fe2009-04-17 17:46:19 +00004916 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, ObjCTypes.Int8PtrTy);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004917 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4918 // (char*)BaseValue + Offset_symbol
4919 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4920 // (type *)((char*)BaseValue + Offset_symbol)
4921 const llvm::Type *IvarTy =
Chris Lattner51123fe2009-04-17 17:46:19 +00004922 CGM.getTypes().ConvertTypeForMem(Ivar->getType());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004923 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4924 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004925
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004926 if (Ivar->isBitField()) {
Chris Lattner51123fe2009-04-17 17:46:19 +00004927 QualType FieldTy = Field->getType();
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004928 CodeGenTypes::BitFieldInfo bitFieldInfo =
4929 CGM.getTypes().getBitFieldInfo(Field);
4930 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
Chris Lattner51123fe2009-04-17 17:46:19 +00004931 FieldTy->isSignedIntegerType(),
4932 FieldTy.getCVRQualifiers()|CVRQualifiers);
Fariborz Jahaniana6681ae2009-03-09 20:44:22 +00004933 }
4934
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004935 LValue LV = LValue::MakeAddr(V,
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004936 Ivar->getType().getCVRQualifiers()|CVRQualifiers,
4937 CGM.getContext().getObjCGCAttrKind(Ivar->getType()));
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004938 LValue::SetObjCIvar(LV, true);
4939 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004940}
4941
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004942llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4943 CodeGen::CodeGenFunction &CGF,
4944 ObjCInterfaceDecl *Interface,
4945 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004946 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4947 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004948}
4949
Fariborz Jahanian46551122009-02-04 00:22:57 +00004950CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4951 CodeGen::CodeGenFunction &CGF,
4952 QualType ResultType,
4953 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004954 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004955 QualType Arg0Ty,
4956 bool IsSuper,
4957 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004958 // FIXME. Even though IsSuper is passes. This function doese not
4959 // handle calls to 'super' receivers.
4960 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004961 llvm::Value *Arg0 = Receiver;
4962 if (!IsSuper)
4963 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004964
4965 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004966 // FIXME. This is too much work to get the ABI-specific result type
4967 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004968 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4969 llvm::SmallVector<QualType, 16>());
4970 llvm::Constant *Fn;
4971 std::string Name("\01l_");
4972 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004973#if 0
4974 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004975 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4976 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4977 // FIXME. Is there a better way of getting these names.
4978 // They are available in RuntimeFunctions vector pair.
4979 Name += "objc_msgSendId_stret_fixup";
4980 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004981 else
4982#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004983 if (IsSuper) {
4984 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4985 Name += "objc_msgSendSuper2_stret_fixup";
4986 }
4987 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004988 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004989 Fn = ObjCTypes.MessageSendStretFixupFn;
4990 Name += "objc_msgSend_stret_fixup";
4991 }
4992 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004993 else if (ResultType->isFloatingType() &&
4994 // Selection of frret API only happens in 32bit nonfragile ABI.
4995 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004996 Fn = ObjCTypes.MessageSendFpretFixupFn;
4997 Name += "objc_msgSend_fpret_fixup";
4998 }
4999 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005000#if 0
5001// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005002 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
5003 Fn = ObjCTypes.MessageSendIdFixupFn;
5004 Name += "objc_msgSendId_fixup";
5005 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005006 else
5007#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005008 if (IsSuper) {
5009 Fn = ObjCTypes.MessageSendSuper2FixupFn;
5010 Name += "objc_msgSendSuper2_fixup";
5011 }
5012 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005013 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005014 Fn = ObjCTypes.MessageSendFixupFn;
5015 Name += "objc_msgSend_fixup";
5016 }
5017 }
5018 Name += '_';
5019 std::string SelName(Sel.getAsString());
5020 // Replace all ':' in selector name with '_' ouch!
5021 for(unsigned i = 0; i < SelName.size(); i++)
5022 if (SelName[i] == ':')
5023 SelName[i] = '_';
5024 Name += SelName;
5025 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5026 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005027 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005028 std::vector<llvm::Constant*> Values(2);
5029 Values[0] = Fn;
5030 Values[1] = GetMethodVarName(Sel);
5031 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5032 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005033 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005034 Init,
5035 Name,
5036 &CGM.getModule());
5037 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005038 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005039 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005040 }
5041 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005042
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005043 CallArgList ActualArgs;
5044 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5045 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5046 ObjCTypes.MessageRefCPtrTy));
5047 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005048 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5049 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5050 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005051 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005052 Callee = CGF.Builder.CreateBitCast(Callee,
5053 llvm::PointerType::getUnqual(FTy));
5054 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005055}
5056
5057/// Generate code for a message send expression in the nonfragile abi.
5058CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5059 CodeGen::CodeGenFunction &CGF,
5060 QualType ResultType,
5061 Selector Sel,
5062 llvm::Value *Receiver,
5063 bool IsClassMessage,
5064 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00005065 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005066 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00005067 false, CallArgs);
5068}
5069
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005070llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005071CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005072 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5073
Daniel Dunbardfff2302009-03-02 05:18:14 +00005074 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005075 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5076 llvm::GlobalValue::ExternalLinkage,
5077 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005078 }
5079
5080 return GV;
5081}
5082
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005083llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005084 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005085 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5086
5087 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005088 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005089 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005090 Entry =
5091 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5092 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005093 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005094 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005095 &CGM.getModule());
5096 Entry->setAlignment(
5097 CGM.getTargetData().getPrefTypeAlignment(
5098 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005099 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5100 UsedGlobals.push_back(Entry);
5101 }
5102
5103 return Builder.CreateLoad(Entry, false, "tmp");
5104}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005105
Daniel Dunbar11394522009-04-18 08:51:00 +00005106llvm::Value *
5107CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5108 const ObjCInterfaceDecl *ID) {
5109 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5110
5111 if (!Entry) {
5112 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5113 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5114 Entry =
5115 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5116 llvm::GlobalValue::InternalLinkage,
5117 ClassGV,
5118 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5119 &CGM.getModule());
5120 Entry->setAlignment(
5121 CGM.getTargetData().getPrefTypeAlignment(
5122 ObjCTypes.ClassnfABIPtrTy));
5123 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005124 UsedGlobals.push_back(Entry);
5125 }
5126
5127 return Builder.CreateLoad(Entry, false, "tmp");
5128}
5129
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005130/// EmitMetaClassRef - Return a Value * of the address of _class_t
5131/// meta-data
5132///
5133llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5134 const ObjCInterfaceDecl *ID) {
5135 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5136 if (Entry)
5137 return Builder.CreateLoad(Entry, false, "tmp");
5138
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005139 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005140 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005141 Entry =
5142 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5143 llvm::GlobalValue::InternalLinkage,
5144 MetaClassGV,
5145 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5146 &CGM.getModule());
5147 Entry->setAlignment(
5148 CGM.getTargetData().getPrefTypeAlignment(
5149 ObjCTypes.ClassnfABIPtrTy));
5150
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005151 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005152 UsedGlobals.push_back(Entry);
5153
5154 return Builder.CreateLoad(Entry, false, "tmp");
5155}
5156
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005157/// GetClass - Return a reference to the class for the given interface
5158/// decl.
5159llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5160 const ObjCInterfaceDecl *ID) {
5161 return EmitClassRef(Builder, ID);
5162}
5163
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005164/// Generates a message send where the super is the receiver. This is
5165/// a message send to self with special delivery semantics indicating
5166/// which class's method should be called.
5167CodeGen::RValue
5168CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5169 QualType ResultType,
5170 Selector Sel,
5171 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005172 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005173 llvm::Value *Receiver,
5174 bool IsClassMessage,
5175 const CodeGen::CallArgList &CallArgs) {
5176 // ...
5177 // Create and init a super structure; this is a (receiver, class)
5178 // pair we will pass to objc_msgSendSuper.
5179 llvm::Value *ObjCSuper =
5180 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5181
5182 llvm::Value *ReceiverAsObject =
5183 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5184 CGF.Builder.CreateStore(ReceiverAsObject,
5185 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5186
5187 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005188 llvm::Value *Target;
5189 if (IsClassMessage) {
5190 if (isCategoryImpl) {
5191 // Message sent to "super' in a class method defined in
5192 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005193 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005194 Target = CGF.Builder.CreateStructGEP(Target, 0);
5195 Target = CGF.Builder.CreateLoad(Target);
5196 }
5197 else
5198 Target = EmitMetaClassRef(CGF.Builder, Class);
5199 }
5200 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005201 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005202
5203 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
5204 // and ObjCTypes types.
5205 const llvm::Type *ClassTy =
5206 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5207 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5208 CGF.Builder.CreateStore(Target,
5209 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5210
5211 return EmitMessageSend(CGF, ResultType, Sel,
5212 ObjCSuper, ObjCTypes.SuperPtrCTy,
5213 true, CallArgs);
5214}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005215
5216llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5217 Selector Sel) {
5218 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5219
5220 if (!Entry) {
5221 llvm::Constant *Casted =
5222 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5223 ObjCTypes.SelectorPtrTy);
5224 Entry =
5225 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5226 llvm::GlobalValue::InternalLinkage,
5227 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5228 &CGM.getModule());
5229 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
5230 UsedGlobals.push_back(Entry);
5231 }
5232
5233 return Builder.CreateLoad(Entry, false, "tmp");
5234}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005235/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5236/// objc_assign_ivar (id src, id *dst)
5237///
5238void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5239 llvm::Value *src, llvm::Value *dst)
5240{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005241 const llvm::Type * SrcTy = src->getType();
5242 if (!isa<llvm::PointerType>(SrcTy)) {
5243 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5244 assert(Size <= 8 && "does not support size > 8");
5245 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5246 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005247 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5248 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005249 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5250 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5251 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
5252 src, dst, "assignivar");
5253 return;
5254}
5255
5256/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5257/// objc_assign_strongCast (id src, id *dst)
5258///
5259void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5260 CodeGen::CodeGenFunction &CGF,
5261 llvm::Value *src, llvm::Value *dst)
5262{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005263 const llvm::Type * SrcTy = src->getType();
5264 if (!isa<llvm::PointerType>(SrcTy)) {
5265 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5266 assert(Size <= 8 && "does not support size > 8");
5267 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5268 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005269 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5270 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005271 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5272 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5273 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
5274 src, dst, "weakassign");
5275 return;
5276}
5277
5278/// EmitObjCWeakRead - Code gen for loading value of a __weak
5279/// object: objc_read_weak (id *src)
5280///
5281llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5282 CodeGen::CodeGenFunction &CGF,
5283 llvm::Value *AddrWeakObj)
5284{
Eli Friedman8339b352009-03-07 03:57:15 +00005285 const llvm::Type* DestTy =
5286 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005287 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
5288 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
5289 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005290 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005291 return read_weak;
5292}
5293
5294/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5295/// objc_assign_weak (id src, id *dst)
5296///
5297void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5298 llvm::Value *src, llvm::Value *dst)
5299{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005300 const llvm::Type * SrcTy = src->getType();
5301 if (!isa<llvm::PointerType>(SrcTy)) {
5302 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5303 assert(Size <= 8 && "does not support size > 8");
5304 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5305 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005306 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5307 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005308 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5309 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005310 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005311 src, dst, "weakassign");
5312 return;
5313}
5314
5315/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5316/// objc_assign_global (id src, id *dst)
5317///
5318void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5319 llvm::Value *src, llvm::Value *dst)
5320{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005321 const llvm::Type * SrcTy = src->getType();
5322 if (!isa<llvm::PointerType>(SrcTy)) {
5323 unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy);
5324 assert(Size <= 8 && "does not support size > 8");
5325 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5326 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005327 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5328 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005329 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5330 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5331 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
5332 src, dst, "globalassign");
5333 return;
5334}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005335
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005336void
5337CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5338 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005339 bool isTry = isa<ObjCAtTryStmt>(S);
5340 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5341 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005342 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005343 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005344 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005345 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5346
5347 // For @synchronized, call objc_sync_enter(sync.expr). The
5348 // evaluation of the expression must occur before we enter the
5349 // @synchronized. We can safely avoid a temp here because jumps into
5350 // @synchronized are illegal & this will dominate uses.
5351 llvm::Value *SyncArg = 0;
5352 if (!isTry) {
5353 SyncArg =
5354 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5355 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005356 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005357 }
5358
5359 // Push an EH context entry, used for handling rethrows and jumps
5360 // through finally.
5361 CGF.PushCleanupBlock(FinallyBlock);
5362
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005363 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005364
5365 CGF.EmitBlock(TryBlock);
5366 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5367 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5368 CGF.EmitBranchThroughCleanup(FinallyEnd);
5369
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005370 // Emit the exception handler.
5371
5372 CGF.EmitBlock(TryHandler);
5373
5374 llvm::Value *llvm_eh_exception =
5375 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5376 llvm::Value *llvm_eh_selector_i64 =
5377 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5378 llvm::Value *llvm_eh_typeid_for_i64 =
5379 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5380 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5381 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5382
5383 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5384 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005385 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005386
5387 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005388 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005389 bool HasCatchAll = false;
5390 if (isTry) {
5391 if (const ObjCAtCatchStmt* CatchStmt =
5392 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5393 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005394 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005395 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005396
5397 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005398 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005399 // Use i8* null here to signal this is a catch all, not a cleanup.
5400 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5401 SelectorArgs.push_back(Null);
5402 HasCatchAll = true;
5403 break;
5404 }
5405
Daniel Dunbarede8de92009-03-06 00:01:21 +00005406 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5407 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005408 llvm::Value *IDEHType =
5409 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5410 if (!IDEHType)
5411 IDEHType =
5412 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5413 llvm::GlobalValue::ExternalLinkage,
5414 0, "OBJC_EHTYPE_id", &CGM.getModule());
5415 SelectorArgs.push_back(IDEHType);
5416 HasCatchAll = true;
5417 break;
5418 }
5419
5420 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005421 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005422 assert(PT && "Invalid @catch type.");
5423 const ObjCInterfaceType *IT =
5424 PT->getPointeeType()->getAsObjCInterfaceType();
5425 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005426 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005427 SelectorArgs.push_back(EHType);
5428 }
5429 }
5430 }
5431
5432 // We use a cleanup unless there was already a catch all.
5433 if (!HasCatchAll) {
5434 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005435 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005436 }
5437
5438 llvm::Value *Selector =
5439 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5440 SelectorArgs.begin(), SelectorArgs.end(),
5441 "selector");
5442 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005443 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005444 const Stmt *CatchBody = Handlers[i].second;
5445
5446 llvm::BasicBlock *Next = 0;
5447
5448 // The last handler always matches.
5449 if (i + 1 != e) {
5450 assert(CatchParam && "Only last handler can be a catch all.");
5451
5452 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5453 Next = CGF.createBasicBlock("catch.next");
5454 llvm::Value *Id =
5455 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5456 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5457 ObjCTypes.Int8PtrTy));
5458 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5459 Match, Next);
5460
5461 CGF.EmitBlock(Match);
5462 }
5463
5464 if (CatchBody) {
5465 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5466 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5467
5468 // Cleanups must call objc_end_catch.
5469 //
5470 // FIXME: It seems incorrect for objc_begin_catch to be inside
5471 // this context, but this matches gcc.
5472 CGF.PushCleanupBlock(MatchEnd);
5473 CGF.setInvokeDest(MatchHandler);
5474
5475 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005476 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005477
5478 // Bind the catch parameter if it exists.
5479 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005480 ExcObject =
5481 CGF.Builder.CreateBitCast(ExcObject,
5482 CGF.ConvertType(CatchParam->getType()));
5483 // CatchParam is a ParmVarDecl because of the grammar
5484 // construction used to handle this, but for codegen purposes
5485 // we treat this as a local decl.
5486 CGF.EmitLocalBlockVarDecl(*CatchParam);
5487 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005488 }
5489
5490 CGF.ObjCEHValueStack.push_back(ExcObject);
5491 CGF.EmitStmt(CatchBody);
5492 CGF.ObjCEHValueStack.pop_back();
5493
5494 CGF.EmitBranchThroughCleanup(FinallyEnd);
5495
5496 CGF.EmitBlock(MatchHandler);
5497
5498 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5499 // We are required to emit this call to satisfy LLVM, even
5500 // though we don't use the result.
5501 llvm::SmallVector<llvm::Value*, 8> Args;
5502 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005503 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005504 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5505 0));
5506 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5507 CGF.Builder.CreateStore(Exc, RethrowPtr);
5508 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5509
5510 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5511
5512 CGF.EmitBlock(MatchEnd);
5513
5514 // Unfortunately, we also have to generate another EH frame here
5515 // in case this throws.
5516 llvm::BasicBlock *MatchEndHandler =
5517 CGF.createBasicBlock("match.end.handler");
5518 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005519 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005520 Cont, MatchEndHandler,
5521 Args.begin(), Args.begin());
5522
5523 CGF.EmitBlock(Cont);
5524 if (Info.SwitchBlock)
5525 CGF.EmitBlock(Info.SwitchBlock);
5526 if (Info.EndBlock)
5527 CGF.EmitBlock(Info.EndBlock);
5528
5529 CGF.EmitBlock(MatchEndHandler);
5530 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5531 // We are required to emit this call to satisfy LLVM, even
5532 // though we don't use the result.
5533 Args.clear();
5534 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005535 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005536 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5537 0));
5538 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5539 CGF.Builder.CreateStore(Exc, RethrowPtr);
5540 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5541
5542 if (Next)
5543 CGF.EmitBlock(Next);
5544 } else {
5545 assert(!Next && "catchup should be last handler.");
5546
5547 CGF.Builder.CreateStore(Exc, RethrowPtr);
5548 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5549 }
5550 }
5551
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005552 // Pop the cleanup entry, the @finally is outside this cleanup
5553 // scope.
5554 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5555 CGF.setInvokeDest(PrevLandingPad);
5556
5557 CGF.EmitBlock(FinallyBlock);
5558
5559 if (isTry) {
5560 if (const ObjCAtFinallyStmt* FinallyStmt =
5561 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5562 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5563 } else {
5564 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5565 // @synchronized.
5566 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005567 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005568
5569 if (Info.SwitchBlock)
5570 CGF.EmitBlock(Info.SwitchBlock);
5571 if (Info.EndBlock)
5572 CGF.EmitBlock(Info.EndBlock);
5573
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005574 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005575 CGF.EmitBranch(FinallyEnd);
5576
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005577 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005578 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005579 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005580 CGF.Builder.CreateUnreachable();
5581
5582 CGF.EmitBlock(FinallyEnd);
5583}
5584
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005585/// EmitThrowStmt - Generate code for a throw statement.
5586void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5587 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005588 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005589 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005590 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005591 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005592 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5593 "Unexpected rethrow outside @catch block.");
5594 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005595 }
5596
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005597 llvm::Value *ExceptionAsObject =
5598 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5599 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5600 if (InvokeDest) {
5601 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
5602 CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn,
5603 Cont, InvokeDest,
5604 &ExceptionAsObject, &ExceptionAsObject + 1);
5605 CGF.EmitBlock(Cont);
5606 } else
5607 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
5608 CGF.Builder.CreateUnreachable();
5609
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005610 // Clear the insertion point to indicate we are in unreachable code.
5611 CGF.Builder.ClearInsertionPoint();
5612}
Daniel Dunbare588b992009-03-01 04:46:24 +00005613
5614llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005615CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5616 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005617 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005618
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005619 // If we don't need a definition, return the entry if found or check
5620 // if we use an external reference.
5621 if (!ForDefinition) {
5622 if (Entry)
5623 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005624
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005625 // If this type (or a super class) has the __objc_exception__
5626 // attribute, emit an external reference.
5627 if (hasObjCExceptionAttribute(ID))
5628 return Entry =
5629 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5630 llvm::GlobalValue::ExternalLinkage,
5631 0,
5632 (std::string("OBJC_EHTYPE_$_") +
5633 ID->getIdentifier()->getName()),
5634 &CGM.getModule());
5635 }
5636
5637 // Otherwise we need to either make a new entry or fill in the
5638 // initializer.
5639 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005640 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005641 std::string VTableName = "objc_ehtype_vtable";
5642 llvm::GlobalVariable *VTableGV =
5643 CGM.getModule().getGlobalVariable(VTableName);
5644 if (!VTableGV)
5645 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5646 llvm::GlobalValue::ExternalLinkage,
5647 0, VTableName, &CGM.getModule());
5648
5649 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5650
5651 std::vector<llvm::Constant*> Values(3);
5652 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5653 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005654 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005655 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5656
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005657 if (Entry) {
5658 Entry->setInitializer(Init);
5659 } else {
5660 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5661 llvm::GlobalValue::WeakAnyLinkage,
5662 Init,
5663 (std::string("OBJC_EHTYPE_$_") +
5664 ID->getIdentifier()->getName()),
5665 &CGM.getModule());
5666 }
5667
Daniel Dunbar04d40782009-04-14 06:00:08 +00005668 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005669 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005670 Entry->setAlignment(8);
5671
5672 if (ForDefinition) {
5673 Entry->setSection("__DATA,__objc_const");
5674 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5675 } else {
5676 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5677 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005678
5679 return Entry;
5680}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005681
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005682/* *** */
5683
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005684CodeGen::CGObjCRuntime *
5685CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005686 return new CGObjCMac(CGM);
5687}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005688
5689CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005690CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005691 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005692}