blob: aff804cd4b468b0d6af77cf1fb9f752242bd192a [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//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
John McCalld16c2cf2011-02-08 08:22:06 +000015#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000016#include "CGCleanup.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "CGRecordLayout.h"
18#include "CodeGenFunction.h"
19#include "CodeGenModule.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000027#include "llvm/ADT/DenseSet.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/ADT/SmallPtrSet.h"
30#include "llvm/ADT/SmallString.h"
31#include "llvm/DataLayout.h"
John McCall87bb5822010-07-31 23:20:56 +000032#include "llvm/InlineAsm.h"
33#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000034#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000035#include "llvm/Module.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000042
43namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000044
Daniel Dunbar6bff2512009-08-03 17:06:42 +000045// FIXME: We should find a nicer way to make the labels for metadata, string
46// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000047
Fariborz Jahanianee0af742009-01-21 22:04:16 +000048class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000049protected:
50 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000051
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000052private:
John McCall0774cb82011-05-15 01:53:33 +000053 // The types of these functions don't really matter because we
54 // should always bitcast before calling them.
55
56 /// id objc_msgSend (id, SEL, ...)
57 ///
58 /// The default messenger, used for sends whose ABI is unchanged from
59 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000060 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000061 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
62 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000063 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000064 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
65 params, true),
John McCallf85e1932011-06-15 23:02:42 +000066 "objc_msgSend",
Bill Wendlingad017fa2012-12-20 19:22:21 +000067 llvm::Attributes::get(CGM.getLLVMContext(),
68 llvm::Attributes::NonLazyBind));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000069 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000070
John McCall0774cb82011-05-15 01:53:33 +000071 /// void objc_msgSend_stret (id, SEL, ...)
72 ///
73 /// The messenger used when the return value is an aggregate returned
74 /// by indirect reference in the first argument, and therefore the
75 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000076 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000077 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000078 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
79 params, true),
80 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000081
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000082 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000083
John McCall0774cb82011-05-15 01:53:33 +000084 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
85 ///
86 /// The messenger used when the return value is returned on the x87
87 /// floating-point stack; without a special entrypoint, the nil case
88 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000089 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000090 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000091 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
92 params, true),
John McCall0774cb82011-05-15 01:53:33 +000093 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000094
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000095 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000096
Anders Carlssoneea64802011-10-31 16:27:11 +000097 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
98 ///
99 /// The messenger used when the return value is returned in two values on the
100 /// x87 floating point stack; without a special entrypoint, the nil case
101 /// would be unbalanced. Only used on 64-bit X86.
102 llvm::Constant *getMessageSendFp2retFn() const {
103 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
104 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
105 llvm::Type *resultType =
106 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
107
108 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
109 params, true),
110 "objc_msgSend_fp2ret");
111 }
112
John McCall0774cb82011-05-15 01:53:33 +0000113 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
114 ///
115 /// The messenger used for super calls, which have different dispatch
116 /// semantics. The class passed is the superclass of the current
117 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000118 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000119 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000121 params, true),
122 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000123 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000124
John McCall0774cb82011-05-15 01:53:33 +0000125 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
126 ///
127 /// A slightly different messenger used for super calls. The class
128 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000129 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000130 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000131 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000132 params, true),
133 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000134 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000135
John McCall0774cb82011-05-15 01:53:33 +0000136 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
137 /// SEL op, ...)
138 ///
139 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000140 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000141 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000142 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000143 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000144 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000145 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146
John McCall0774cb82011-05-15 01:53:33 +0000147 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
148 /// SEL op, ...)
149 ///
150 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000151 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000152 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000153 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000154 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000156 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000158 llvm::Constant *getMessageSendSuperFpretFn() const {
159 // There is no objc_msgSendSuper_fpret? How can that work?
160 return getMessageSendSuperFn();
161 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000163 llvm::Constant *getMessageSendSuperFpretFn2() const {
164 // There is no objc_msgSendSuper_fpret? How can that work?
165 return getMessageSendSuperFn2();
166 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000167
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000168protected:
169 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000170
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000171public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000172 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000173 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000175 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000176 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000177
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000178 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000179 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000180
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000181 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000182 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000183
184private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000185 /// ProtocolPtrTy - LLVM type for external protocol handles
186 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000187 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000188
189public:
190 llvm::Type *getExternalProtocolPtrTy() {
191 if (!ExternalProtocolPtrTy) {
192 // FIXME: It would be nice to unify this with the opaque type, so that the
193 // IR comes out a bit cleaner.
194 CodeGen::CodeGenTypes &Types = CGM.getTypes();
195 ASTContext &Ctx = CGM.getContext();
196 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
197 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
198 }
199
200 return ExternalProtocolPtrTy;
201 }
202
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000203 // SuperCTy - clang type for struct objc_super.
204 QualType SuperCTy;
205 // SuperPtrCTy - clang type for struct objc_super *.
206 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000207
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000208 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000209 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000210 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000211 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000212
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000213 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
214 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000215 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000216
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217 /// PropertyListTy - LLVM type for struct objc_property_list
218 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000219 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000220 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000221 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000222
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000223 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000224 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000225
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000226 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000227 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000228 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000230
Chris Lattner72db6c32009-04-22 02:44:54 +0000231 llvm::Constant *getGetPropertyFn() {
232 CodeGen::CodeGenTypes &Types = CGM.getTypes();
233 ASTContext &Ctx = CGM.getContext();
234 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000236 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
237 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000238 Params.push_back(IdType);
239 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000240 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000241 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000242 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000243 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
244 FunctionType::ExtInfo(),
245 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000246 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
247 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000248
Chris Lattner72db6c32009-04-22 02:44:54 +0000249 llvm::Constant *getSetPropertyFn() {
250 CodeGen::CodeGenTypes &Types = CGM.getTypes();
251 ASTContext &Ctx = CGM.getContext();
252 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000254 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
255 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000256 Params.push_back(IdType);
257 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000258 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000259 Params.push_back(IdType);
260 Params.push_back(Ctx.BoolTy);
261 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000262 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000263 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
264 FunctionType::ExtInfo(),
265 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000266 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
267 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000268
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000269 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
270 CodeGen::CodeGenTypes &Types = CGM.getTypes();
271 ASTContext &Ctx = CGM.getContext();
272 // void objc_setProperty_atomic(id self, SEL _cmd,
273 // id newValue, ptrdiff_t offset);
274 // void objc_setProperty_nonatomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280
281 SmallVector<CanQualType,4> Params;
282 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
283 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
284 Params.push_back(IdType);
285 Params.push_back(SelType);
286 Params.push_back(IdType);
287 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
288 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000289 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
290 FunctionType::ExtInfo(),
291 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000292 const char *name;
293 if (atomic && copy)
294 name = "objc_setProperty_atomic_copy";
295 else if (atomic && !copy)
296 name = "objc_setProperty_atomic";
297 else if (!atomic && copy)
298 name = "objc_setProperty_nonatomic_copy";
299 else
300 name = "objc_setProperty_nonatomic";
301
302 return CGM.CreateRuntimeFunction(FTy, name);
303 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000304
305 llvm::Constant *getCopyStructFn() {
306 CodeGen::CodeGenTypes &Types = CGM.getTypes();
307 ASTContext &Ctx = CGM.getContext();
308 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000309 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000310 Params.push_back(Ctx.VoidPtrTy);
311 Params.push_back(Ctx.VoidPtrTy);
312 Params.push_back(Ctx.LongTy);
313 Params.push_back(Ctx.BoolTy);
314 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000315 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000316 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
317 FunctionType::ExtInfo(),
318 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000319 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
320 }
321
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000322 /// This routine declares and returns address of:
323 /// void objc_copyCppObjectAtomic(
324 /// void *dest, const void *src,
325 /// void (*copyHelper) (void *dest, const void *source));
326 llvm::Constant *getCppAtomicObjectFunction() {
327 CodeGen::CodeGenTypes &Types = CGM.getTypes();
328 ASTContext &Ctx = CGM.getContext();
329 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
330 SmallVector<CanQualType,3> Params;
331 Params.push_back(Ctx.VoidPtrTy);
332 Params.push_back(Ctx.VoidPtrTy);
333 Params.push_back(Ctx.VoidPtrTy);
334 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000335 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
336 FunctionType::ExtInfo(),
337 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000338 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
339 }
340
Chris Lattner72db6c32009-04-22 02:44:54 +0000341 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000342 CodeGen::CodeGenTypes &Types = CGM.getTypes();
343 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000344 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000345 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000346 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000347 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000348 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000349 FunctionType::ExtInfo(),
350 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000351 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
352 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000353
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000354 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000355 llvm::Constant *getGcReadWeakFn() {
356 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000357 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000358 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000359 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000360 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000361 }
362
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000363 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000364 llvm::Constant *getGcAssignWeakFn() {
365 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000366 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000367 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000368 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000369 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
370 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000371
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000372 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000373 llvm::Constant *getGcAssignGlobalFn() {
374 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000375 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000376 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000377 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000378 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
379 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000380
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000381 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
382 llvm::Constant *getGcAssignThreadLocalFn() {
383 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000384 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000385 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000386 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000387 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
388 }
389
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000390 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000391 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000392 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000393 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
394 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000395 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000396 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000397 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
398 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000399
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000400 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
401 llvm::Constant *GcMemmoveCollectableFn() {
402 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000403 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000404 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000405 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
406 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000407
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000408 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000409 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000410 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000411 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000412 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000413 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000414 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
415 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000416
417 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 llvm::Constant *getExceptionThrowFn() {
419 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000420 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000421 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000422 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
424 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000425
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000426 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
427 llvm::Constant *getExceptionRethrowFn() {
428 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000429 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000430 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
431 }
432
Daniel Dunbar1c566672009-02-24 01:43:46 +0000433 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000434 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000435 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000436 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000437 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000438 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000439 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
440 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000441
Daniel Dunbar1c566672009-02-24 01:43:46 +0000442 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000443 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000444 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000445 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000446 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000447 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
449 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000450
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000451 llvm::Constant *getSendFn(bool IsSuper) const {
452 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
453 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000454
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000455 llvm::Constant *getSendFn2(bool IsSuper) const {
456 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
457 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000458
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000459 llvm::Constant *getSendStretFn(bool IsSuper) const {
460 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
461 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000462
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000463 llvm::Constant *getSendStretFn2(bool IsSuper) const {
464 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
465 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000466
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000467 llvm::Constant *getSendFpretFn(bool IsSuper) const {
468 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
469 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000470
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000471 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
472 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
473 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000474
Anders Carlssoneea64802011-10-31 16:27:11 +0000475 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
476 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
477 }
478
479 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
480 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
481 }
482
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000483 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
484 ~ObjCCommonTypesHelper(){}
485};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000486
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000487/// ObjCTypesHelper - Helper class that encapsulates lazy
488/// construction of varies types used during ObjC generation.
489class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000490public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000491 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000495 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000497
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000498 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000499 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000500 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolExtensionTy - LLVM type for struct
503 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000504 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000505 /// ProtocolExtensionTy - LLVM type for struct
506 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000507 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000508 /// MethodDescriptionTy - LLVM type for struct
509 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000510 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000511 /// MethodDescriptionListTy - LLVM type for struct
512 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000513 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000514 /// MethodDescriptionListPtrTy - LLVM type for struct
515 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000516 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000517 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000521 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000541
Anders Carlsson124526b2008-09-09 10:10:21 +0000542 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000543 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000544
Anders Carlsson124526b2008-09-09 10:10:21 +0000545 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000546 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000547 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000548 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000549 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000550 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000551 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000552
553 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000554 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000555 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000556 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000557 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000558 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000559 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000560
561 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000562 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000563 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000564 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000565 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000566 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000567 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Anders Carlsson124526b2008-09-09 10:10:21 +0000569 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000571 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000573 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 "objc_exception_match");
575
Chris Lattner34b02a12009-04-22 02:26:14 +0000576 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000577
Anders Carlsson124526b2008-09-09 10:10:21 +0000578 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000579 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000580 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000581 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
583 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000584 "_setjmp",
Bill Wendlingad017fa2012-12-20 19:22:21 +0000585 llvm::Attributes::get(CGM.getLLVMContext(),
586 llvm::Attributes::NonLazyBind));
Chris Lattner34b02a12009-04-22 02:26:14 +0000587 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000588
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000589public:
590 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000591 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000592};
593
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000594/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000595/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000596class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000597public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000598
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000599 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000600 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000601
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000602 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000603 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000605 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000607
Daniel Dunbar948e2582009-02-15 07:36:20 +0000608 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000609 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000610
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000611 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000612 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000614 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000615 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000616
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000617 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000618 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000620 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000621 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000622
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000623 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000624 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000626 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000627 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000629 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000630 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000633 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000636 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000639 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000641 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000643 // MessageRefTy - LLVM for:
644 // struct _message_ref_t {
645 // IMP messenger;
646 // SEL name;
647 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000648 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000649 // MessageRefCTy - clang type for struct _message_ref_t
650 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000652 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000653 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000654 // MessageRefCPtrTy - clang type for struct _message_ref_t*
655 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000656
Fariborz Jahanianef163782009-02-05 01:13:09 +0000657 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000658 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000659
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000660 // SuperMessageRefTy - LLVM for:
661 // struct _super_message_ref_t {
662 // SUPER_IMP messenger;
663 // SEL name;
664 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000665 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000666
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000667 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000668 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000669
Chris Lattner1c02f862009-04-22 02:53:24 +0000670 llvm::Constant *getMessageSendFixupFn() {
671 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000674 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000675 "objc_msgSend_fixup");
676 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000677
Chris Lattner1c02f862009-04-22 02:53:24 +0000678 llvm::Constant *getMessageSendFpretFixupFn() {
679 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000680 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000681 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000682 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000683 "objc_msgSend_fpret_fixup");
684 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000685
Chris Lattner1c02f862009-04-22 02:53:24 +0000686 llvm::Constant *getMessageSendStretFixupFn() {
687 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000688 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000689 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000690 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000691 "objc_msgSend_stret_fixup");
692 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000693
Chris Lattner1c02f862009-04-22 02:53:24 +0000694 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000697 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000698 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000699 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 "objc_msgSendSuper2_fixup");
701 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000702
Chris Lattner1c02f862009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000705 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000706 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000708 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 "objc_msgSendSuper2_stret_fixup");
710 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000711
Chris Lattner8a569112009-04-22 02:15:23 +0000712 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000714 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Chris Lattner8a569112009-04-22 02:15:23 +0000716 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000719 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000720 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000721 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000722 "objc_begin_catch");
723 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000724
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000725 llvm::StructType *EHTypeTy;
726 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000727
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000728 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
729 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000730};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000733public:
734 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000735 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000736 public:
Eli Friedmane5b46662012-11-06 22:15:52 +0000737 unsigned ivar_bytepos;
738 unsigned ivar_size;
739 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000741
742 // Allow sorting based on byte pos.
743 bool operator<(const GC_IVAR &b) const {
744 return ivar_bytepos < b.ivar_bytepos;
745 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000746 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000747
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000748 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000749 public:
750 unsigned skip;
751 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000753 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000754 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000755
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000756 /// opcode for captured block variables layout 'instructions'.
757 /// In the following descriptions, 'I' is the value of the immediate field.
758 /// (field following the opcode).
759 ///
760 enum BLOCK_LAYOUT_OPCODE {
761 /// An operator which affects how the following layout should be
762 /// interpreted.
763 /// I == 0: Halt interpretation and treat everything else as
764 /// a non-pointer. Note that this instruction is equal
765 /// to '\0'.
766 /// I != 0: Currently unused.
767 BLOCK_LAYOUT_OPERATOR = 0,
768
769 /// The next I+1 bytes do not contain a value of object pointer type.
770 /// Note that this can leave the stream unaligned, meaning that
771 /// subsequent word-size instructions do not begin at a multiple of
772 /// the pointer size.
773 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
774
775 /// The next I+1 words do not contain a value of object pointer type.
776 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
777 /// when the required skip quantity is a multiple of the pointer size.
778 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
779
780 /// The next I+1 words are __strong pointers to Objective-C
781 /// objects or blocks.
782 BLOCK_LAYOUT_STRONG = 3,
783
784 /// The next I+1 words are pointers to __block variables.
785 BLOCK_LAYOUT_BYREF = 4,
786
787 /// The next I+1 words are __weak pointers to Objective-C
788 /// objects or blocks.
789 BLOCK_LAYOUT_WEAK = 5,
790
791 /// The next I+1 words are __unsafe_unretained pointers to
792 /// Objective-C objects or blocks.
793 BLOCK_LAYOUT_UNRETAINED = 6
794
795 /// The next I+1 words are block or object pointers with some
796 /// as-yet-unspecified ownership semantics. If we add more
797 /// flavors of ownership semantics, values will be taken from
798 /// this range.
799 ///
800 /// This is included so that older tools can at least continue
801 /// processing the layout past such things.
802 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
803
804 /// All other opcodes are reserved. Halt interpretation and
805 /// treat everything else as opaque.
806 };
807
808 class RUN_SKIP {
809 public:
810 enum BLOCK_LAYOUT_OPCODE opcode;
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000811 CharUnits block_var_bytepos;
812 CharUnits block_var_size;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000813 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000814 CharUnits BytePos = CharUnits::Zero(),
815 CharUnits Size = CharUnits::Zero())
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000816 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000817
818 // Allow sorting based on byte pos.
819 bool operator<(const RUN_SKIP &b) const {
820 return block_var_bytepos < b.block_var_bytepos;
821 }
822 };
823
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000824protected:
Owen Anderson69243822009-07-13 04:10:07 +0000825 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000826 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000827 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000829 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000830 SmallVector<GC_IVAR, 16> SkipIvars;
831 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000832
833 // arc/mrr layout of captured block literal variables.
834 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000835
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000836 /// LazySymbols - Symbols to generate a lazy reference for. See
837 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000838 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000839
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000840 /// DefinedSymbols - External symbols which are defined by this
841 /// module. The symbols in this list and LazySymbols are used to add
842 /// special linker symbols which ensure that Objective-C modules are
843 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000844 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000845
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000846 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000847 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000849 /// MethodVarNames - uniqued method variable names.
850 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000852 /// DefinedCategoryNames - list of category names in form Class_Category.
853 llvm::SetVector<std::string> DefinedCategoryNames;
854
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000855 /// MethodVarTypes - uniqued method type signatures. We have to use
856 /// a StringMap here because have no other unique reference.
857 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000858
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000859 /// MethodDefinitions - map of methods which have been defined in
860 /// this translation unit.
861 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000862
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000863 /// PropertyNames - uniqued method variable names.
864 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000865
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000866 /// ClassReferences - uniqued class references.
867 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000869 /// SelectorReferences - uniqued selector references.
870 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000871
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000872 /// Protocols - Protocols for which an objc_protocol structure has
873 /// been emitted. Forward declarations are handled by creating an
874 /// empty structure whose initializer is filled in when/if defined.
875 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000876
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000877 /// DefinedProtocols - Protocols which have actually been
878 /// defined. We should not need this, see FIXME in GenerateProtocol.
879 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000881 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000882 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000883
884 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000885 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000887 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000888 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000889
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000890 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000891 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000893 /// GetNameForMethod - Return a name for the given method.
894 /// \param[out] NameOut - The return value.
895 void GetNameForMethod(const ObjCMethodDecl *OMD,
896 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000897 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000899 /// GetMethodVarName - Return a unique constant for the given
900 /// selector's name. The return value has type char *.
901 llvm::Constant *GetMethodVarName(Selector Sel);
902 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000903
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000904 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000905 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000906
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000907 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000908 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
909 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000910 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000911
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000912 /// GetPropertyName - Return a unique constant for the given
913 /// name. The return value has type char *.
914 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000915
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000916 // FIXME: This can be dropped once string functions are unified.
917 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
918 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000920 /// GetClassName - Return a unique constant for the given selector's
921 /// name. The return value has type char *.
922 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000924 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
925
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000926 /// BuildIvarLayout - Builds ivar layout bitmap for the class
927 /// implementation for the __strong or __weak case.
928 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000929 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
930 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000931
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000932 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000933
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000934 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedmane5b46662012-11-06 22:15:52 +0000935 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000937 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000938 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000939 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000940 ArrayRef<const FieldDecl*> RecFields,
Eli Friedmane5b46662012-11-06 22:15:52 +0000941 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000942 bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000943
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000944 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout);
Fariborz Jahanian44fcff92012-11-02 22:51:18 +0000945
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000946 void UpdateRunSkipBlockVars(bool IsByref,
947 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +0000948 CharUnits FieldOffset,
949 CharUnits FieldSize);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000950
951 void BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000952 CharUnits BytePos, bool &HasUnion,
953 bool ByrefLayout=false);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000954
955 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
956 const RecordDecl *RD,
957 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000958 CharUnits BytePos, bool &HasUnion,
959 bool ByrefLayout);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000960
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000961 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
962
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +0000963 llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout);
964
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000965
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000966 /// GetIvarLayoutName - Returns a unique constant for the given
967 /// ivar layout bitmap.
968 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
969 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000970
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000971 /// EmitPropertyList - Emit the given property list. The return
972 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000973 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000974 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000975 const ObjCContainerDecl *OCD,
976 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000977
Bob Wilsondc8dab62011-11-30 01:57:58 +0000978 /// EmitProtocolMethodTypes - Generate the array of extended method type
979 /// strings. The return value has type Int8PtrPtrTy.
980 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000981 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000982 const ObjCCommonTypesHelper &ObjCTypes);
983
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000984 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000985 void PushProtocolProperties(
986 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
987 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
988 const Decl *Container,
989 const ObjCProtocolDecl *PROTO,
990 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000991
Fariborz Jahanianda320092009-01-29 19:24:30 +0000992 /// GetProtocolRef - Return a reference to the internal protocol
993 /// description, creating an empty one if it has not been
994 /// defined. The return value has type ProtocolPtrTy.
995 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000996
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000997 /// CreateMetadataVar - Create a global variable with internal
998 /// linkage for use by the Objective-C runtime.
999 ///
1000 /// This is a convenience wrapper which not only creates the
1001 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +00001002 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001003 ///
1004 /// \param Name - The variable name.
1005 /// \param Init - The variable initializer; this is also used to
1006 /// define the type of the variable.
1007 /// \param Section - The section the variable should go into, or 0.
1008 /// \param Align - The alignment for the variable, or 0.
1009 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +00001010 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +00001011 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001012 llvm::Constant *Init,
1013 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001014 unsigned Align,
1015 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001016
John McCall944c8432011-05-14 03:10:52 +00001017 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1018 ReturnValueSlot Return,
1019 QualType ResultType,
1020 llvm::Value *Sel,
1021 llvm::Value *Arg0,
1022 QualType Arg0Ty,
1023 bool IsSuper,
1024 const CallArgList &CallArgs,
1025 const ObjCMethodDecl *OMD,
1026 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001027
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001028 /// EmitImageInfo - Emit the image info marker used to encode some module
1029 /// level information.
1030 void EmitImageInfo();
1031
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001032public:
Owen Anderson69243822009-07-13 04:10:07 +00001033 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +00001034 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001035
David Chisnall0d13f6f2010-01-23 02:40:42 +00001036 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001037
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001038 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1039 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001040
Fariborz Jahanianda320092009-01-29 19:24:30 +00001041 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001042
Fariborz Jahanianda320092009-01-29 19:24:30 +00001043 /// GetOrEmitProtocol - Get the protocol object for the given
1044 /// declaration, emitting it if necessary. The return value has type
1045 /// ProtocolPtrTy.
1046 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001047
Fariborz Jahanianda320092009-01-29 19:24:30 +00001048 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1049 /// object for the given declaration, emitting it if needed. These
1050 /// forward references will be filled in with empty bodies if no
1051 /// definition is seen. The return value has type ProtocolPtrTy.
1052 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001053 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1054 const CGBlockInfo &blockInfo);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001055 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1056 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001057
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00001058 virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM,
1059 QualType T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001060};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001061
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001062class CGObjCMac : public CGObjCCommonMac {
1063private:
1064 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001065
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001066 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001067 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001068 void EmitModuleInfo();
1069
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001070 /// EmitModuleSymols - Emit module symbols, the list of defined
1071 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001072 llvm::Constant *EmitModuleSymbols();
1073
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001074 /// FinishModule - Write out global data structures at the end of
1075 /// processing a translation unit.
1076 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001077
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001078 /// EmitClassExtension - Generate the class extension structure used
1079 /// to store the weak ivar layout and properties. The return value
1080 /// has type ClassExtensionPtrTy.
1081 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1082
1083 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1084 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001085 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001086 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001087
John McCallf85e1932011-06-15 23:02:42 +00001088 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1089 IdentifierInfo *II);
1090
1091 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1092
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001093 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1094 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001095
1096 /// EmitIvarList - Emit the ivar list for the given
1097 /// implementation. If ForClass is true the list of class ivars
1098 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1099 /// interface ivars will be emitted. The return value has type
1100 /// IvarListPtrTy.
1101 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001102 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001104 /// EmitMetaClass - Emit a forward reference to the class structure
1105 /// for the metaclass of the given interface. The return value has
1106 /// type ClassPtrTy.
1107 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1108
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001109 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001110 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001111 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1112 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001113 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001114
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001115 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001117 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001118
1119 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001120 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001121 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001122 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001123 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001124
1125 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001126 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001127 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001128 /// - IsProtocol: True iff these methods are for a protocol.
1129 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 /// - Required: When true, only "required" methods are
1131 /// listed. Similarly, when false only "optional" methods are
1132 /// listed. For classes this should always be true.
1133 /// - begin, end: The method list to output.
1134 ///
1135 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001136 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001137 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001138 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001139
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001140 /// GetOrEmitProtocol - Get the protocol object for the given
1141 /// declaration, emitting it if necessary. The return value has type
1142 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001143 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001144
1145 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1146 /// object for the given declaration, emitting it if needed. These
1147 /// forward references will be filled in with empty bodies if no
1148 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001149 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001150
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001151 /// EmitProtocolExtension - Generate the protocol extension
1152 /// structure used to store optional instance and class methods, and
1153 /// protocol properties. The return value has type
1154 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001155 llvm::Constant *
1156 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001157 ArrayRef<llvm::Constant*> OptInstanceMethods,
1158 ArrayRef<llvm::Constant*> OptClassMethods,
1159 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001160
1161 /// EmitProtocolList - Generate the list of referenced
1162 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001163 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001164 ObjCProtocolDecl::protocol_iterator begin,
1165 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001166
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001167 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1168 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001169 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1170 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001171
1172public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001173 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001174
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001175 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001177 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001178 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001179 QualType ResultType,
1180 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001181 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001182 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001183 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001184 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001185
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001186 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001187 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001188 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001189 QualType ResultType,
1190 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001191 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001192 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001193 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001194 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001195 const CallArgList &CallArgs,
1196 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001197
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001198 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001199 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001200
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001201 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1202 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001203
1204 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1205 /// untyped one.
1206 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1207 const ObjCMethodDecl *Method);
1208
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001209 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001210
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001211 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001212
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001213 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001214
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001215 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001216
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001217 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001218 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001219
Chris Lattner74391b42009-03-22 21:03:39 +00001220 virtual llvm::Constant *GetPropertyGetFunction();
1221 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001222 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1223 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001224 virtual llvm::Constant *GetGetStructFunction();
1225 virtual llvm::Constant *GetSetStructFunction();
David Chisnalld397cfe2012-12-17 18:54:24 +00001226 virtual llvm::Constant *GetCppAtomicObjectGetFunction();
1227 virtual llvm::Constant *GetCppAtomicObjectSetFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001228 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001229
John McCallf1549f62010-07-06 01:34:17 +00001230 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1231 const ObjCAtTryStmt &S);
1232 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1233 const ObjCAtSynchronizedStmt &S);
1234 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001235 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1236 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001237 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001239 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001240 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001241 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001242 llvm::Value *src, llvm::Value *dest,
1243 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001244 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001245 llvm::Value *src, llvm::Value *dest,
1246 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001247 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1248 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001249 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1250 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001251 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001252
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001253 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1254 QualType ObjectTy,
1255 llvm::Value *BaseValue,
1256 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001257 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001258 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001259 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001260 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001261
1262 /// GetClassGlobal - Return the global variable for the Objective-C
1263 /// class of the given name.
1264 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001265 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001266 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001267};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001269class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001270private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001271 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001272 llvm::GlobalVariable* ObjCEmptyCacheVar;
1273 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274
Daniel Dunbar11394522009-04-18 08:51:00 +00001275 /// SuperClassReferences - uniqued super class references.
1276 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001278 /// MetaClassReferences - uniqued meta class references.
1279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001280
1281 /// EHTypeReferences - uniqued class ehtype references.
1282 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283
John McCall944c8432011-05-14 03:10:52 +00001284 /// VTableDispatchMethods - List of methods for which we generate
1285 /// vtable-based message dispatch.
1286 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001288 /// DefinedMetaClasses - List of defined meta-classes.
1289 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1290
John McCall944c8432011-05-14 03:10:52 +00001291 /// isVTableDispatchedSelector - Returns true if SEL is a
1292 /// vtable-based selector.
1293 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001295 /// FinishNonFragileABIModule - Write out global data structures at the end of
1296 /// processing a translation unit.
1297 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001298
Daniel Dunbar463b8762009-05-15 21:48:48 +00001299 /// AddModuleClassList - Add the given list of class pointers to the
1300 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001301 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001302 const char *SymbolName,
1303 const char *SectionName);
1304
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001305 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1306 unsigned InstanceStart,
1307 unsigned InstanceSize,
1308 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001309 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001311 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001312 llvm::Constant *ClassRoGV,
1313 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001315 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001316
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001317 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001318
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001319 /// EmitMethodList - Emit the method list for the given
1320 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001321 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001322 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001323 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001324 /// EmitIvarList - Emit the ivar list for the given
1325 /// implementation. If ForClass is true the list of class ivars
1326 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1327 /// interface ivars will be emitted. The return value has type
1328 /// IvarListnfABIPtrTy.
1329 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001330
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001331 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001332 const ObjCIvarDecl *Ivar,
Eli Friedmane5b46662012-11-06 22:15:52 +00001333 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001334
Fariborz Jahanianda320092009-01-29 19:24:30 +00001335 /// GetOrEmitProtocol - Get the protocol object for the given
1336 /// declaration, emitting it if necessary. The return value has type
1337 /// ProtocolPtrTy.
1338 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001339
Fariborz Jahanianda320092009-01-29 19:24:30 +00001340 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1341 /// object for the given declaration, emitting it if needed. These
1342 /// forward references will be filled in with empty bodies if no
1343 /// definition is seen. The return value has type ProtocolPtrTy.
1344 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001345
Fariborz Jahanianda320092009-01-29 19:24:30 +00001346 /// EmitProtocolList - Generate the list of referenced
1347 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001348 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001349 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001350 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001351
John McCall944c8432011-05-14 03:10:52 +00001352 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1353 ReturnValueSlot Return,
1354 QualType ResultType,
1355 Selector Sel,
1356 llvm::Value *Receiver,
1357 QualType Arg0Ty,
1358 bool IsSuper,
1359 const CallArgList &CallArgs,
1360 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001361
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001362 /// GetClassGlobal - Return the global variable for the Objective-C
1363 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001364 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001365
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001366 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001367 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001368 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001369 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001370
1371 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1372 IdentifierInfo *II);
1373
1374 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001375
Daniel Dunbar11394522009-04-18 08:51:00 +00001376 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1377 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1379 const ObjCInterfaceDecl *ID);
1380
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001381 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1382 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001383 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001384 const ObjCInterfaceDecl *ID);
1385
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001386 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1387 /// the given ivar.
1388 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001389 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001390 const ObjCInterfaceDecl *ID,
1391 const ObjCIvarDecl *Ivar);
1392
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001393 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1394 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001395 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1396 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001397
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001398 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001399 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001400 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001401 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001402
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001403 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001404 return "OBJC_METACLASS_$_";
1405 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001406
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001407 const char *getClassSymbolPrefix() const {
1408 return "OBJC_CLASS_$_";
1409 }
1410
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001411 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001412 uint32_t &InstanceStart,
1413 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001414
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001415 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001416 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001417 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1418 return CGM.getContext().Selectors.getSelector(0, &II);
1419 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001420
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001421 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001422 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1423 return CGM.getContext().Selectors.getSelector(1, &II);
1424 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001425
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001426 /// ImplementationIsNonLazy - Check whether the given category or
1427 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001428 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001429
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001430public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001431 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001432 // FIXME. All stubs for now!
1433 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001434
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001435 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001436 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001437 QualType ResultType,
1438 Selector Sel,
1439 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001440 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001441 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001442 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001443
1444 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001445 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001446 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001447 QualType ResultType,
1448 Selector Sel,
1449 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001450 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001451 llvm::Value *Receiver,
1452 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001453 const CallArgList &CallArgs,
1454 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001455
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001456 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001457 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001458
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001459 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1460 bool lvalue = false)
1461 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001462
1463 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1464 /// untyped one.
1465 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1466 const ObjCMethodDecl *Method)
1467 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001468
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001469 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001470
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001471 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001472
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001473 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001474
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001475 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001476 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001478 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001479
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001480 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001481 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001482 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001483 virtual llvm::Constant *GetPropertySetFunction() {
1484 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001485 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001486
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001487 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1488 bool copy) {
1489 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1490 }
1491
David Chisnall8fac25d2010-12-26 22:13:16 +00001492 virtual llvm::Constant *GetSetStructFunction() {
1493 return ObjCTypes.getCopyStructFn();
1494 }
1495 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001496 return ObjCTypes.getCopyStructFn();
1497 }
David Chisnalld397cfe2012-12-17 18:54:24 +00001498 virtual llvm::Constant *GetCppAtomicObjectSetFunction() {
1499 return ObjCTypes.getCppAtomicObjectFunction();
1500 }
1501 virtual llvm::Constant *GetCppAtomicObjectGetFunction() {
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001502 return ObjCTypes.getCppAtomicObjectFunction();
1503 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001504
Chris Lattner74391b42009-03-22 21:03:39 +00001505 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001506 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001507 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001508
John McCallf1549f62010-07-06 01:34:17 +00001509 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1510 const ObjCAtTryStmt &S);
1511 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1512 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001513 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001514 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001515 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001516 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001517 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001518 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001519 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001520 llvm::Value *src, llvm::Value *dest,
1521 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001522 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001523 llvm::Value *src, llvm::Value *dest,
1524 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001525 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001526 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001527 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1528 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001529 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001530 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1531 QualType ObjectTy,
1532 llvm::Value *BaseValue,
1533 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001534 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001535 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001536 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001537 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001538};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001539
1540/// A helper class for performing the null-initialization of a return
1541/// value.
1542struct NullReturnState {
1543 llvm::BasicBlock *NullBB;
1544 llvm::BasicBlock *callBB;
1545 NullReturnState() : NullBB(0), callBB(0) {}
1546
1547 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1548 // Make blocks for the null-init and call edges.
1549 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1550 callBB = CGF.createBasicBlock("msgSend.call");
1551
1552 // Check for a null receiver and, if there is one, jump to the
1553 // null-init test.
1554 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1555 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1556
1557 // Otherwise, start performing the call.
1558 CGF.EmitBlock(callBB);
1559 }
1560
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001561 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1562 const CallArgList &CallArgs,
1563 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001564 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001565
1566 llvm::Value *NullInitPtr = 0;
1567 if (result.isScalar() && !resultType->isVoidType()) {
1568 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1569 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1570 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001571
1572 // Finish the call path.
1573 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1574 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1575
1576 // Emit the null-init block and perform the null-initialization there.
1577 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001578
1579 // Release consumed arguments along the null-receiver path.
1580 if (Method) {
1581 CallArgList::const_iterator I = CallArgs.begin();
1582 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1583 e = Method->param_end(); i != e; ++i, ++I) {
1584 const ParmVarDecl *ParamDecl = (*i);
1585 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1586 RValue RV = I->RV;
1587 assert(RV.isScalar() &&
1588 "NullReturnState::complete - arg not on object");
1589 CGF.EmitARCRelease(RV.getScalarVal(), true);
1590 }
1591 }
1592 }
1593
1594 if (result.isScalar()) {
1595 if (NullInitPtr)
1596 CGF.EmitNullInitialization(NullInitPtr, resultType);
1597 // Jump to the continuation block.
1598 CGF.EmitBlock(contBB);
1599 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1600 : result;
1601 }
1602
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001603 if (!resultType->isAnyComplexType()) {
1604 assert(result.isAggregate() && "null init of non-aggregate result?");
1605 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1606 // Jump to the continuation block.
1607 CGF.EmitBlock(contBB);
1608 return result;
1609 }
1610
1611 // _Complex type
1612 // FIXME. Now easy to handle any other scalar type whose result is returned
1613 // in memory due to ABI limitations.
1614 CGF.EmitBlock(contBB);
1615 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1616 llvm::Type *MemberType = CallCV.first->getType();
1617 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1618 // Create phi instruction for scalar complex value.
1619 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1620 PHIReal->addIncoming(ZeroCV, NullBB);
1621 PHIReal->addIncoming(CallCV.first, callBB);
1622 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1623 PHIImag->addIncoming(ZeroCV, NullBB);
1624 PHIImag->addIncoming(CallCV.second, callBB);
1625 return RValue::getComplex(PHIReal, PHIImag);
1626 }
1627};
1628
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001629} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001630
1631/* *** Helper Functions *** */
1632
1633/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001634static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001635 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001636 unsigned idx0,
1637 unsigned idx1) {
1638 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001639 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1640 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001641 };
Jay Foada5c04342011-07-21 14:31:17 +00001642 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001643}
1644
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001645/// hasObjCExceptionAttribute - Return true if this class or any super
1646/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001647static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001648 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001649 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001650 return true;
1651 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001652 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001653 return false;
1654}
1655
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001656/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001657
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001658CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001659 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001660 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001661 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001662}
1663
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001664/// GetClass - Return a reference to the class for the given interface
1665/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001666llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001667 const ObjCInterfaceDecl *ID) {
1668 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001669}
1670
1671/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001672llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1673 bool lval) {
1674 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001675}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001676llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001677 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001678 return EmitSelector(Builder, Method->getSelector());
1679}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001680
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001681llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001682 if (T->isObjCIdType() ||
1683 T->isObjCQualifiedIdType()) {
1684 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001685 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001686 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001687 if (T->isObjCClassType() ||
1688 T->isObjCQualifiedClassType()) {
1689 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001690 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001691 }
1692 if (T->isObjCObjectPointerType())
1693 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1694
John McCall5a180392010-07-24 00:37:23 +00001695 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001696}
1697
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001698/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001699/*
1700 struct __builtin_CFString {
1701 const int *isa; // point to __CFConstantStringClassReference
1702 int flags;
1703 const char *str;
1704 long length;
1705 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001706*/
1707
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001708/// or Generate a constant NSString object.
1709/*
1710 struct __builtin_NSString {
1711 const int *isa; // point to __NSConstantStringClassReference
1712 const char *str;
1713 unsigned int length;
1714 };
1715*/
1716
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001717llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001718 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001719 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001720 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001721 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001722}
1723
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001724enum {
1725 kCFTaggedObjectID_Integer = (1 << 1) + 1
1726};
1727
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001728/// Generates a message send where the super is the receiver. This is
1729/// a message send to self with special delivery semantics indicating
1730/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001731CodeGen::RValue
1732CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001733 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001734 QualType ResultType,
1735 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001736 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001737 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001738 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001739 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001740 const CodeGen::CallArgList &CallArgs,
1741 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001742 // Create and init a super structure; this is a (receiver, class)
1743 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001744 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001745 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001746 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001747 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001748 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001749 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001750
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001751 // If this is a class message the metaclass is passed as the target.
1752 llvm::Value *Target;
1753 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001754 if (isCategoryImpl) {
1755 // Message sent to 'super' in a class method defined in a category
1756 // implementation requires an odd treatment.
1757 // If we are in a class method, we must retrieve the
1758 // _metaclass_ for the current class, pointed at by
1759 // the class's "isa" pointer. The following assumes that
1760 // isa" is the first ivar in a class (which it must be).
1761 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1762 Target = CGF.Builder.CreateStructGEP(Target, 0);
1763 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001764 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001765 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1766 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1767 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1768 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001769 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001770 }
1771 else if (isCategoryImpl)
1772 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1773 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001774 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1775 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1776 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001777 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001778 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1779 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001780 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001781 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001782 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001783 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001784 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001785 return EmitMessageSend(CGF, Return, ResultType,
1786 EmitSelector(CGF.Builder, Sel),
1787 ObjCSuper, ObjCTypes.SuperPtrCTy,
1788 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001789}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001790
1791/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001792CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001793 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001794 QualType ResultType,
1795 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001796 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001797 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001798 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001799 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001800 return EmitMessageSend(CGF, Return, ResultType,
1801 EmitSelector(CGF.Builder, Sel),
1802 Receiver, CGF.getContext().getObjCIdType(),
1803 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001804}
1805
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001806CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001807CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1808 ReturnValueSlot Return,
1809 QualType ResultType,
1810 llvm::Value *Sel,
1811 llvm::Value *Arg0,
1812 QualType Arg0Ty,
1813 bool IsSuper,
1814 const CallArgList &CallArgs,
1815 const ObjCMethodDecl *Method,
1816 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001817 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001818 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001819 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001820 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1821 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001822 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001823
John McCallde5d3c72012-02-17 03:33:10 +00001824 // If we're calling a method, use the formal signature.
1825 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001826
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001827 if (Method)
1828 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1829 CGM.getContext().getCanonicalType(ResultType) &&
1830 "Result type mismatch!");
1831
John McCallcba681a2011-05-14 21:12:11 +00001832 NullReturnState nullReturn;
1833
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001834 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001835 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001836 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001837 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001839 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1840 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1841 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001842 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1843 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1844 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001845 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001846 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001847 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001848 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001849
1850 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001851 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001852 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1853 e = Method->param_end(); i != e; ++i) {
1854 const ParmVarDecl *ParamDecl = (*i);
1855 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1856 if (!nullReturn.NullBB)
1857 nullReturn.init(CGF, Arg0);
1858 requiresnullCheck = true;
1859 break;
1860 }
1861 }
1862
John McCallde5d3c72012-02-17 03:33:10 +00001863 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1864 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001865 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1866 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001867}
1868
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001869static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1870 if (FQT.isObjCGCStrong())
1871 return Qualifiers::Strong;
1872
John McCallf85e1932011-06-15 23:02:42 +00001873 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001874 return Qualifiers::Weak;
1875
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001876 // check for __unsafe_unretained
1877 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1878 return Qualifiers::GCNone;
1879
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001880 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1881 return Qualifiers::Strong;
1882
1883 if (const PointerType *PT = FQT->getAs<PointerType>())
1884 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1885
1886 return Qualifiers::GCNone;
1887}
1888
John McCall6b5a61b2011-02-07 10:33:21 +00001889llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1890 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001891
Chris Lattner8b418682012-02-07 00:39:47 +00001892 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikie4e4d0842012-03-11 07:00:24 +00001893 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1894 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001895 return nullPtr;
1896
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001897 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001898 SkipIvars.clear();
1899 IvarsInfo.clear();
Eli Friedmane5b46662012-11-06 22:15:52 +00001900 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1901 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001902
Fariborz Jahanian81979822010-09-09 00:21:45 +00001903 // __isa is the first field in block descriptor and must assume by runtime's
1904 // convention that it is GC'able.
Eli Friedmane5b46662012-11-06 22:15:52 +00001905 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001906
1907 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1908
1909 // Calculate the basic layout of the block structure.
1910 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001911 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001912
1913 // Ignore the optional 'this' capture: C++ objects are not assumed
1914 // to be GC'ed.
1915
1916 // Walk the captured variables.
1917 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1918 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1919 const VarDecl *variable = ci->getVariable();
1920 QualType type = variable->getType();
1921
1922 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1923
1924 // Ignore constant captures.
1925 if (capture.isConstant()) continue;
1926
Eli Friedmane5b46662012-11-06 22:15:52 +00001927 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
John McCall6b5a61b2011-02-07 10:33:21 +00001928
1929 // __block variables are passed by their descriptor address.
1930 if (ci->isByRef()) {
Eli Friedmane5b46662012-11-06 22:15:52 +00001931 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001932 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001933 }
1934
1935 assert(!type->isArrayType() && "array variable should not be caught");
1936 if (const RecordType *record = type->getAs<RecordType>()) {
1937 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001938 continue;
1939 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001940
John McCall6b5a61b2011-02-07 10:33:21 +00001941 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
Eli Friedmane5b46662012-11-06 22:15:52 +00001942 unsigned fieldSize = CGM.getContext().getTypeSize(type);
John McCall6b5a61b2011-02-07 10:33:21 +00001943
1944 if (GCAttr == Qualifiers::Strong)
Eli Friedmane5b46662012-11-06 22:15:52 +00001945 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1946 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001947 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
Eli Friedmane5b46662012-11-06 22:15:52 +00001948 SkipIvars.push_back(GC_IVAR(fieldOffset,
1949 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001950 }
1951
1952 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001953 return nullPtr;
1954
1955 // Sort on byte position; captures might not be allocated in order,
1956 // and unions can do funny things.
1957 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1958 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001959
1960 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001961 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001962 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001963 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001964 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001965 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001966 if (!(s[i] & 0xf0))
1967 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1968 else
1969 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1970 printf("\n");
1971 }
1972
1973 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001974}
1975
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001976/// getBlockCaptureLifetime - This routine returns life time of the captured
1977/// block variable for the purpose of block layout meta-data generation. FQT is
1978/// the type of the variable captured in the block.
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00001979Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT,
1980 bool ByrefLayout) {
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001981 if (CGM.getLangOpts().ObjCAutoRefCount)
1982 return FQT.getObjCLifetime();
1983
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001984 // MRR.
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001985 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00001986 return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong;
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001987
1988 return Qualifiers::OCL_None;
1989}
1990
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001991void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1992 Qualifiers::ObjCLifetime LifeTime,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00001993 CharUnits FieldOffset,
1994 CharUnits FieldSize) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001995 // __block variables are passed by their descriptor address.
1996 if (IsByref)
1997 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00001998 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001999 else if (LifeTime == Qualifiers::OCL_Strong)
2000 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002001 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002002 else if (LifeTime == Qualifiers::OCL_Weak)
2003 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002004 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002005 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2006 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002007 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002008 else
2009 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2010 FieldOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002011 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002012}
2013
2014void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2015 const RecordDecl *RD,
2016 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002017 CharUnits BytePos, bool &HasUnion,
2018 bool ByrefLayout) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002019 bool IsUnion = (RD && RD->isUnion());
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002020 CharUnits MaxUnionSize = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002021 const FieldDecl *MaxField = 0;
2022 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002023 CharUnits MaxFieldOffset = CharUnits::Zero();
2024 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002025
2026 if (RecFields.empty())
2027 return;
2028 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2029
2030 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2031 const FieldDecl *Field = RecFields[i];
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002032 // Note that 'i' here is actually the field index inside RD of Field,
2033 // although this dependency is hidden.
2034 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002035 CharUnits FieldOffset =
2036 CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002037
2038 // Skip over unnamed or bitfields
2039 if (!Field->getIdentifier() || Field->isBitField()) {
2040 LastFieldBitfieldOrUnnamed = Field;
2041 LastBitfieldOrUnnamedOffset = FieldOffset;
2042 continue;
2043 }
2044
2045 LastFieldBitfieldOrUnnamed = 0;
2046 QualType FQT = Field->getType();
2047 if (FQT->isRecordType() || FQT->isUnionType()) {
2048 if (FQT->isUnionType())
2049 HasUnion = true;
2050
2051 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2052 BytePos + FieldOffset, HasUnion);
2053 continue;
2054 }
2055
2056 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2057 const ConstantArrayType *CArray =
2058 dyn_cast_or_null<ConstantArrayType>(Array);
2059 uint64_t ElCount = CArray->getSize().getZExtValue();
2060 assert(CArray && "only array with known element size is supported");
2061 FQT = CArray->getElementType();
2062 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2063 const ConstantArrayType *CArray =
2064 dyn_cast_or_null<ConstantArrayType>(Array);
2065 ElCount *= CArray->getSize().getZExtValue();
2066 FQT = CArray->getElementType();
2067 }
2068
2069 assert(!FQT->isUnionType() &&
2070 "layout for array of unions not supported");
2071 if (FQT->isRecordType() && ElCount) {
2072 int OldIndex = RunSkipBlockVars.size() - 1;
2073 const RecordType *RT = FQT->getAs<RecordType>();
2074 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2075 HasUnion);
2076
2077 // Replicate layout information for each array element. Note that
2078 // one element is already done.
2079 uint64_t ElIx = 1;
2080 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002081 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002082 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2083 RunSkipBlockVars.push_back(
2084 RUN_SKIP(RunSkipBlockVars[i].opcode,
2085 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2086 RunSkipBlockVars[i].block_var_size));
2087 }
2088 continue;
2089 }
2090 }
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002091 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002092 if (IsUnion) {
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002093 CharUnits UnionIvarSize = FieldSize;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002094 if (UnionIvarSize > MaxUnionSize) {
2095 MaxUnionSize = UnionIvarSize;
2096 MaxField = Field;
2097 MaxFieldOffset = FieldOffset;
2098 }
2099 } else {
2100 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002101 getBlockCaptureLifetime(FQT, ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002102 BytePos + FieldOffset,
2103 FieldSize);
2104 }
2105 }
2106
2107 if (LastFieldBitfieldOrUnnamed) {
2108 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2109 // Last field was a bitfield. Must update the info.
2110 uint64_t BitFieldSize
2111 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002112 unsigned UnsSize = (BitFieldSize / ByteSizeInBits) +
Eli Friedmane5b46662012-11-06 22:15:52 +00002113 ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002114 CharUnits Size = CharUnits::fromQuantity(UnsSize);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002115 Size += LastBitfieldOrUnnamedOffset;
2116 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002117 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2118 ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002119 BytePos + LastBitfieldOrUnnamedOffset,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002120 Size);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002121 } else {
2122 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2123 // Last field was unnamed. Must update skip info.
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002124 CharUnits FieldSize
2125 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002126 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002127 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(),
2128 ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002129 BytePos + LastBitfieldOrUnnamedOffset,
2130 FieldSize);
2131 }
2132 }
2133
2134 if (MaxField)
2135 UpdateRunSkipBlockVars(false,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002136 getBlockCaptureLifetime(MaxField->getType(), ByrefLayout),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002137 BytePos + MaxFieldOffset,
2138 MaxUnionSize);
2139}
2140
2141void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002142 CharUnits BytePos,
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002143 bool &HasUnion,
2144 bool ByrefLayout) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002145 const RecordDecl *RD = RT->getDecl();
2146 SmallVector<const FieldDecl*, 16> Fields;
2147 for (RecordDecl::field_iterator i = RD->field_begin(),
2148 e = RD->field_end(); i != e; ++i)
2149 Fields.push_back(*i);
2150 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2151 const llvm::StructLayout *RecLayout =
2152 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2153
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002154 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002155}
2156
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002157/// InlineLayoutInstruction - This routine produce an inline instruction for the
2158/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2159/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2160/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2161/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2162/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2163/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2164/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2165/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2166uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2167 SmallVectorImpl<unsigned char> &Layout) {
2168 uint64_t Result = 0;
2169 if (Layout.size() <= 3) {
2170 unsigned size = Layout.size();
2171 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2172 unsigned char inst;
2173 enum BLOCK_LAYOUT_OPCODE opcode ;
2174 switch (size) {
2175 case 3:
2176 inst = Layout[0];
2177 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2178 if (opcode == BLOCK_LAYOUT_STRONG)
2179 strong_word_count = (inst & 0xF)+1;
2180 else
2181 return 0;
2182 inst = Layout[1];
2183 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2184 if (opcode == BLOCK_LAYOUT_BYREF)
2185 byref_word_count = (inst & 0xF)+1;
2186 else
2187 return 0;
2188 inst = Layout[2];
2189 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2190 if (opcode == BLOCK_LAYOUT_WEAK)
2191 weak_word_count = (inst & 0xF)+1;
2192 else
2193 return 0;
2194 break;
2195
2196 case 2:
2197 inst = Layout[0];
2198 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2199 if (opcode == BLOCK_LAYOUT_STRONG) {
2200 strong_word_count = (inst & 0xF)+1;
2201 inst = Layout[1];
2202 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2203 if (opcode == BLOCK_LAYOUT_BYREF)
2204 byref_word_count = (inst & 0xF)+1;
2205 else if (opcode == BLOCK_LAYOUT_WEAK)
2206 weak_word_count = (inst & 0xF)+1;
2207 else
2208 return 0;
2209 }
2210 else if (opcode == BLOCK_LAYOUT_BYREF) {
2211 byref_word_count = (inst & 0xF)+1;
2212 inst = Layout[1];
2213 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2214 if (opcode == BLOCK_LAYOUT_WEAK)
2215 weak_word_count = (inst & 0xF)+1;
2216 else
2217 return 0;
2218 }
2219 else
2220 return 0;
2221 break;
2222
2223 case 1:
2224 inst = Layout[0];
2225 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2226 if (opcode == BLOCK_LAYOUT_STRONG)
2227 strong_word_count = (inst & 0xF)+1;
2228 else if (opcode == BLOCK_LAYOUT_BYREF)
2229 byref_word_count = (inst & 0xF)+1;
2230 else if (opcode == BLOCK_LAYOUT_WEAK)
2231 weak_word_count = (inst & 0xF)+1;
2232 else
2233 return 0;
2234 break;
2235
2236 default:
2237 return 0;
2238 }
2239
2240 // Cannot inline when any of the word counts is 15. Because this is one less
2241 // than the actual work count (so 15 means 16 actual word counts),
2242 // and we can only display 0 thru 15 word counts.
2243 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2244 return 0;
2245
2246 unsigned count =
2247 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2248
2249 if (size == count) {
2250 if (strong_word_count)
2251 Result = strong_word_count;
2252 Result <<= 4;
2253 if (byref_word_count)
2254 Result += byref_word_count;
2255 Result <<= 4;
2256 if (weak_word_count)
2257 Result += weak_word_count;
2258 }
2259 }
2260 return Result;
2261}
2262
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002263llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) {
2264 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2265 if (RunSkipBlockVars.empty())
2266 return nullPtr;
2267 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2268 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2269 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2270
2271 // Sort on byte position; captures might not be allocated in order,
2272 // and unions can do funny things.
2273 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
2274 SmallVector<unsigned char, 16> Layout;
2275
2276 unsigned size = RunSkipBlockVars.size();
2277 for (unsigned i = 0; i < size; i++) {
2278 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2279 CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2280 CharUnits end_byte_pos = start_byte_pos;
2281 unsigned j = i+1;
2282 while (j < size) {
2283 if (opcode == RunSkipBlockVars[j].opcode) {
2284 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2285 i++;
2286 }
2287 else
2288 break;
2289 }
2290 CharUnits size_in_bytes =
2291 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
2292 if (j < size) {
2293 CharUnits gap =
2294 RunSkipBlockVars[j].block_var_bytepos -
2295 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2296 size_in_bytes += gap;
2297 }
2298 CharUnits residue_in_bytes = CharUnits::Zero();
2299 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2300 residue_in_bytes = size_in_bytes % WordSizeInBytes;
2301 size_in_bytes -= residue_in_bytes;
2302 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2303 }
2304
2305 unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes;
2306 while (size_in_words >= 16) {
2307 // Note that value in imm. is one less that the actual
2308 // value. So, 0xf means 16 words follow!
2309 unsigned char inst = (opcode << 4) | 0xf;
2310 Layout.push_back(inst);
2311 size_in_words -= 16;
2312 }
2313 if (size_in_words > 0) {
2314 // Note that value in imm. is one less that the actual
2315 // value. So, we subtract 1 away!
2316 unsigned char inst = (opcode << 4) | (size_in_words-1);
2317 Layout.push_back(inst);
2318 }
2319 if (residue_in_bytes > CharUnits::Zero()) {
2320 unsigned char inst =
2321 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1);
2322 Layout.push_back(inst);
2323 }
2324 }
2325
2326 int e = Layout.size()-1;
2327 while (e >= 0) {
2328 unsigned char inst = Layout[e--];
2329 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2330 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2331 Layout.pop_back();
2332 else
2333 break;
2334 }
2335
2336 uint64_t Result = InlineLayoutInstruction(Layout);
2337 if (Result != 0) {
2338 // Block variable layout instruction has been inlined.
2339 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2340 if (ComputeByrefLayout)
2341 printf("\n Inline instruction for BYREF variable layout: ");
2342 else
2343 printf("\n Inline instruction for block variable layout: ");
2344 printf("0x0%llx\n", (unsigned long long)Result);
2345 }
2346 if (WordSizeInBytes == 8) {
2347 const llvm::APInt Instruction(64, Result);
2348 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2349 }
2350 else {
2351 const llvm::APInt Instruction(32, Result);
2352 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2353 }
2354 }
2355
2356 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2357 Layout.push_back(inst);
2358 std::string BitMap;
2359 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2360 BitMap += Layout[i];
2361
2362 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2363 if (ComputeByrefLayout)
2364 printf("\n BYREF variable layout: ");
2365 else
2366 printf("\n block variable layout: ");
2367 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2368 unsigned char inst = BitMap[i];
2369 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2370 unsigned delta = 1;
2371 switch (opcode) {
2372 case BLOCK_LAYOUT_OPERATOR:
2373 printf("BL_OPERATOR:");
2374 delta = 0;
2375 break;
2376 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2377 printf("BL_NON_OBJECT_BYTES:");
2378 break;
2379 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2380 printf("BL_NON_OBJECT_WORD:");
2381 break;
2382 case BLOCK_LAYOUT_STRONG:
2383 printf("BL_STRONG:");
2384 break;
2385 case BLOCK_LAYOUT_BYREF:
2386 printf("BL_BYREF:");
2387 break;
2388 case BLOCK_LAYOUT_WEAK:
2389 printf("BL_WEAK:");
2390 break;
2391 case BLOCK_LAYOUT_UNRETAINED:
2392 printf("BL_UNRETAINED:");
2393 break;
2394 }
2395 // Actual value of word count is one more that what is in the imm.
2396 // field of the instruction
2397 printf("%d", (inst & 0xf) + delta);
2398 if (i < e-1)
2399 printf(", ");
2400 else
2401 printf("\n");
2402 }
2403 }
2404
2405 llvm::GlobalVariable * Entry =
2406 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2407 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2408 "__TEXT,__objc_classname,cstring_literals", 1, true);
2409 return getConstantGEP(VMContext, Entry, 0, 0);
2410}
2411
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002412llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2413 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002414 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2415
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002416 RunSkipBlockVars.clear();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002417 bool hasUnion = false;
2418
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002419 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2420 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2421 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2422
2423 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2424
2425 // Calculate the basic layout of the block structure.
2426 const llvm::StructLayout *layout =
2427 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2428
2429 // Ignore the optional 'this' capture: C++ objects are not assumed
2430 // to be GC'ed.
Fariborz Jahanianff685c52012-12-04 17:20:57 +00002431 if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero())
2432 UpdateRunSkipBlockVars(false, Qualifiers::OCL_None,
2433 blockInfo.BlockHeaderForcedGapOffset,
2434 blockInfo.BlockHeaderForcedGapSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002435 // Walk the captured variables.
2436 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2437 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2438 const VarDecl *variable = ci->getVariable();
2439 QualType type = variable->getType();
2440
2441 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2442
2443 // Ignore constant captures.
2444 if (capture.isConstant()) continue;
2445
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002446 CharUnits fieldOffset =
2447 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002448
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002449 assert(!type->isArrayType() && "array variable should not be caught");
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002450 if (!ci->isByRef())
2451 if (const RecordType *record = type->getAs<RecordType>()) {
2452 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
2453 continue;
2454 }
Fariborz Jahanian1da01d62012-11-07 20:00:32 +00002455 CharUnits fieldSize;
2456 if (ci->isByRef())
2457 fieldSize = CharUnits::fromQuantity(WordSizeInBytes);
2458 else
2459 fieldSize = CGM.getContext().getTypeSizeInChars(type);
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002460 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002461 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002462 }
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002463 return getBitmapBlockLayout(false);
2464}
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002465
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002466
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002467llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM,
2468 QualType T) {
2469 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2470 assert(!T->isArrayType() && "__block array variable should not be caught");
2471 CharUnits fieldOffset;
2472 RunSkipBlockVars.clear();
2473 bool hasUnion = false;
2474 if (const RecordType *record = T->getAs<RecordType>()) {
2475 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */);
2476 llvm::Constant *Result = getBitmapBlockLayout(true);
2477 return Result;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002478 }
Fariborz Jahanian3ca23d72012-11-14 17:15:51 +00002479 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2480 return nullPtr;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002481}
2482
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002483llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002484 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002485 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002486 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002487 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2488
Owen Anderson3c4972d2009-07-29 18:54:39 +00002489 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002490 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002491}
2492
Fariborz Jahanianda320092009-01-29 19:24:30 +00002493void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002494 // FIXME: We shouldn't need this, the protocol decl should contain enough
2495 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002496 DefinedProtocols.insert(PD->getIdentifier());
2497
2498 // If we have generated a forward reference to this protocol, emit
2499 // it now. Otherwise do nothing, the protocol objects are lazily
2500 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002501 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002502 GetOrEmitProtocol(PD);
2503}
2504
Fariborz Jahanianda320092009-01-29 19:24:30 +00002505llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002506 if (DefinedProtocols.count(PD->getIdentifier()))
2507 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002508
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002509 return GetOrEmitProtocolRef(PD);
2510}
2511
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002512/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2514struct _objc_protocol {
2515struct _objc_protocol_extension *isa;
2516char *protocol_name;
2517struct _objc_protocol_list *protocol_list;
2518struct _objc__method_prototype_list *instance_methods;
2519struct _objc__method_prototype_list *class_methods
2520};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002521
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002522See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002523*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002524llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002525 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002526
2527 // Early exit if a defining object has already been generated.
2528 if (Entry && Entry->hasInitializer())
2529 return Entry;
2530
Douglas Gregor1d784b22012-01-01 19:51:50 +00002531 // Use the protocol definition, if there is one.
2532 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2533 PD = Def;
2534
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002535 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002536 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002537 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2538
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002539 // Construct method lists.
2540 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2541 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002542 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002543 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002544 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002545 ObjCMethodDecl *MD = *i;
2546 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002547 if (!C)
2548 return GetOrEmitProtocolRef(PD);
2549
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002550 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2551 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002552 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002553 } else {
2554 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002555 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002556 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002557 }
2558
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002559 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002560 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002561 ObjCMethodDecl *MD = *i;
2562 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002563 if (!C)
2564 return GetOrEmitProtocolRef(PD);
2565
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002566 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2567 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002568 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002569 } else {
2570 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002571 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002572 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002573 }
2574
Bob Wilsondc8dab62011-11-30 01:57:58 +00002575 MethodTypesExt.insert(MethodTypesExt.end(),
2576 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2577
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002578 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002579 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2580 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002581 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002582 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002583 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002584 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002585 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002586 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002587 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002588 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002589 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002590 ClassMethods)
2591 };
Owen Anderson08e25242009-07-27 22:29:56 +00002592 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002593 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002594
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002595 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002596 // Already created, fix the linkage and update the initializer.
2597 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002598 Entry->setInitializer(Init);
2599 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002600 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002601 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002602 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002603 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002604 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002605 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002606 // FIXME: Is this necessary? Why only for protocol?
2607 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002608
2609 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002610 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002611 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002612
2613 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002614}
2615
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002616llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002617 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2618
2619 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002620 // We use the initializer as a marker of whether this is a forward
2621 // reference or not. At module finalization we add the empty
2622 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002623 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002624 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002625 llvm::GlobalValue::ExternalLinkage,
2626 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002627 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002628 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002629 // FIXME: Is this necessary? Why only for protocol?
2630 Entry->setAlignment(4);
2631 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002632
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633 return Entry;
2634}
2635
2636/*
2637 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002638 uint32_t size;
2639 struct objc_method_description_list *optional_instance_methods;
2640 struct objc_method_description_list *optional_class_methods;
2641 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002642 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002643 };
2644*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002645llvm::Constant *
2646CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002647 ArrayRef<llvm::Constant*> OptInstanceMethods,
2648 ArrayRef<llvm::Constant*> OptClassMethods,
2649 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002650 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002651 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002652 llvm::Constant *Values[] = {
2653 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002654 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002655 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002656 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002657 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002658 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002659 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002660 OptClassMethods),
2661 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002662 ObjCTypes),
2663 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2664 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002665 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002666
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002667 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002668 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002669 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002670 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002671
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002672 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002673 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002674
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002675 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002676 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002677 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002678 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002679}
2680
2681/*
2682 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002683 struct objc_protocol_list *next;
2684 long count;
2685 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002686 };
2687*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002688llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002689CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002690 ObjCProtocolDecl::protocol_iterator begin,
2691 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002692 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002693
Daniel Dunbardbc93372008-08-21 21:57:41 +00002694 for (; begin != end; ++begin)
2695 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002696
2697 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002698 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002699 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002700
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002701 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002702 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002703
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002704 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002705 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002706 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002707 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002708 ProtocolRefs.size() - 1);
2709 Values[2] =
2710 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2711 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002713
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002714 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002715 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002716 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002717 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002718 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002719}
2720
Bill Wendlingbb028552012-02-07 09:25:09 +00002721void CGObjCCommonMac::
2722PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002723 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002724 const Decl *Container,
2725 const ObjCProtocolDecl *PROTO,
2726 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002727 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2728 E = PROTO->protocol_end(); P != E; ++P)
2729 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2730 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2731 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002732 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002733 if (!PropertySet.insert(PD->getIdentifier()))
2734 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002735 llvm::Constant *Prop[] = {
2736 GetPropertyName(PD->getIdentifier()),
2737 GetPropertyTypeString(PD, Container)
2738 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002739 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2740 }
2741}
2742
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002743/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002744 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002745 const char * const name;
2746 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002747 };
2748
2749 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002750 uint32_t entsize; // sizeof (struct _objc_property)
2751 uint32_t prop_count;
2752 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002753 };
2754*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002755llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002756 const Decl *Container,
2757 const ObjCContainerDecl *OCD,
2758 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002759 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002760 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002761 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2762 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002763 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002764 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002765 llvm::Constant *Prop[] = {
2766 GetPropertyName(PD->getIdentifier()),
2767 GetPropertyTypeString(PD, Container)
2768 };
Owen Anderson08e25242009-07-27 22:29:56 +00002769 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002770 Prop));
2771 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002772 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002773 for (ObjCInterfaceDecl::all_protocol_iterator
2774 P = OID->all_referenced_protocol_begin(),
2775 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002776 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2777 ObjCTypes);
2778 }
2779 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2780 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2781 E = CD->protocol_end(); P != E; ++P)
2782 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2783 ObjCTypes);
2784 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002785
2786 // Return null for empty list.
2787 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002788 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002789
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002790 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002791 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002792 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002793 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2794 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002795 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002796 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002797 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002798 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002799
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002800 llvm::GlobalVariable *GV =
2801 CreateMetadataVar(Name, Init,
2802 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002803 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002804 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002805 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002806 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002807}
2808
Bill Wendlingbb028552012-02-07 09:25:09 +00002809llvm::Constant *
2810CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2811 ArrayRef<llvm::Constant*> MethodTypes,
2812 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002813 // Return null for empty list.
2814 if (MethodTypes.empty())
2815 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2816
2817 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2818 MethodTypes.size());
2819 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2820
2821 llvm::GlobalVariable *GV =
2822 CreateMetadataVar(Name, Init,
2823 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2824 (ObjCABI == 2) ? 8 : 4,
2825 true);
2826 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2827}
2828
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002829/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002830 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002831 int count;
2832 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002833 };
2834*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002835llvm::Constant *
2836CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002837 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002838 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002839 ObjCTypes.SelectorPtrTy),
2840 GetMethodVarType(MD)
2841 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002842 if (!Desc[1])
2843 return 0;
2844
Owen Anderson08e25242009-07-27 22:29:56 +00002845 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002846 Desc);
2847}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002848
Bill Wendlingbb028552012-02-07 09:25:09 +00002849llvm::Constant *
2850CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2851 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002852 // Return null for empty list.
2853 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002854 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002855
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002856 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002857 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002858 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002859 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002860 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002861 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002862
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002863 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002865 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002866}
2867
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002868/*
2869 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002870 char *category_name;
2871 char *class_name;
2872 struct _objc_method_list *instance_methods;
2873 struct _objc_method_list *class_methods;
2874 struct _objc_protocol_list *protocols;
2875 uint32_t size; // <rdar://4585769>
2876 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002877 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002878*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002879void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002880 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002881
Mike Stumpf5408fe2009-05-16 07:57:57 +00002882 // FIXME: This is poor design, the OCD should have a pointer to the category
2883 // decl. Additionally, note that Category can be null for the @implementation
2884 // w/o an @interface case. Sema should just create one for us as it does for
2885 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002886 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002887 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002888 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002889
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002890 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002891 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2892 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002893
Bill Wendling3964e622012-02-09 22:16:49 +00002894 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002895 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002896 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002897 // Instance methods should always be defined.
2898 InstanceMethods.push_back(GetMethodConstant(*i));
2899 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002901 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002902 // Class methods should always be defined.
2903 ClassMethods.push_back(GetMethodConstant(*i));
2904 }
2905
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002906 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002907 Values[0] = GetClassName(OCD->getIdentifier());
2908 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002909 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002910 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002911 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002912 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002913 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002915 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002916 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002917 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002918 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002919 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002920 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002921 Category->protocol_begin(),
2922 Category->protocol_end());
2923 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002924 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002925 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002926 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002927
2928 // If there is no category @interface then there can be no properties.
2929 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002930 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002931 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002932 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002933 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002934 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002935
Owen Anderson08e25242009-07-27 22:29:56 +00002936 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002937 Values);
2938
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002939 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002940 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002941 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002942 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002943 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002944 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002945 // method definition entries must be clear for next implementation.
2946 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002947}
2948
John McCall621915c2012-10-17 04:53:23 +00002949enum FragileClassFlags {
2950 FragileABI_Class_Factory = 0x00001,
2951 FragileABI_Class_Meta = 0x00002,
2952 FragileABI_Class_HasCXXStructors = 0x02000,
2953 FragileABI_Class_Hidden = 0x20000
2954};
2955
2956enum NonFragileClassFlags {
2957 /// Is a meta-class.
2958 NonFragileABI_Class_Meta = 0x00001,
2959
2960 /// Is a root class.
2961 NonFragileABI_Class_Root = 0x00002,
2962
2963 /// Has a C++ constructor and destructor.
2964 NonFragileABI_Class_HasCXXStructors = 0x00004,
2965
2966 /// Has hidden visibility.
2967 NonFragileABI_Class_Hidden = 0x00010,
2968
2969 /// Has the exception attribute.
2970 NonFragileABI_Class_Exception = 0x00020,
2971
2972 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2973 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2974
2975 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00002976 NonFragileABI_Class_CompiledByARC = 0x00080,
2977
2978 /// Class has non-trivial destructors, but zero-initialization is okay.
2979 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002980};
2981
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002982/*
2983 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002984 Class isa;
2985 Class super_class;
2986 const char *name;
2987 long version;
2988 long info;
2989 long instance_size;
2990 struct _objc_ivar_list *ivars;
2991 struct _objc_method_list *methods;
2992 struct _objc_cache *cache;
2993 struct _objc_protocol_list *protocols;
2994 // Objective-C 1.0 extensions (<rdr://4585769>)
2995 const char *ivar_layout;
2996 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002997 };
2998
2999 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003000*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003001void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003002 DefinedSymbols.insert(ID->getIdentifier());
3003
Chris Lattner8ec03f52008-11-24 03:54:41 +00003004 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003005 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003006 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003007 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003008 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003009 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00003010 Interface->all_referenced_protocol_begin(),
3011 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00003012 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00003013 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00003014 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003015 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00003016 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003017
3018 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00003019 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003020 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003021
Bill Wendling3964e622012-02-09 22:16:49 +00003022 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003023 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003024 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003025 // Instance methods should always be defined.
3026 InstanceMethods.push_back(GetMethodConstant(*i));
3027 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003028 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003029 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003030 // Class methods should always be defined.
3031 ClassMethods.push_back(GetMethodConstant(*i));
3032 }
3033
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003034 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003035 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00003036 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003037
3038 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
3039 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3040
3041 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3042 if (llvm::Constant *C = GetMethodConstant(MD))
3043 InstanceMethods.push_back(C);
3044 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3045 if (llvm::Constant *C = GetMethodConstant(MD))
3046 InstanceMethods.push_back(C);
3047 }
3048 }
3049
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003050 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003051 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003052 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003053 // Record a reference to the super class.
3054 LazySymbols.insert(Super->getIdentifier());
3055
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003056 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003057 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003058 ObjCTypes.ClassPtrTy);
3059 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003060 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003061 }
3062 Values[ 2] = GetClassName(ID->getIdentifier());
3063 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003064 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3065 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3066 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003067 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003068 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003069 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003070 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003071 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003072 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003073 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003074 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003075 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003076 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003077 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003078 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003079 std::string Name("\01L_OBJC_CLASS_");
3080 Name += ClassName;
3081 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3082 // Check for a forward reference.
3083 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3084 if (GV) {
3085 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3086 "Forward metaclass reference has incorrect type.");
3087 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3088 GV->setInitializer(Init);
3089 GV->setSection(Section);
3090 GV->setAlignment(4);
3091 CGM.AddUsedGlobal(GV);
3092 }
3093 else
3094 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003095 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003096 // method definition entries must be clear for next implementation.
3097 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003098}
3099
3100llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3101 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003102 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003103 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003104 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003105
John McCall1fb0caa2010-10-22 21:05:15 +00003106 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003107 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003108
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003109 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003110 // The isa for the metaclass is the root of the hierarchy.
3111 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3112 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3113 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003114 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003115 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003116 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003117 // The super class for the metaclass is emitted as the name of the
3118 // super class. The runtime fixes this up to point to the
3119 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003120 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003121 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003122 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003123 ObjCTypes.ClassPtrTy);
3124 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003125 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003126 }
3127 Values[ 2] = GetClassName(ID->getIdentifier());
3128 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003129 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3130 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3131 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003132 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003133 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003134 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003135 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003136 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003137 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003138 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003139 Values[ 9] = Protocols;
3140 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003141 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003142 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003143 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003144 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003145 Values);
3146
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003147 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003148 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003149
3150 // Check for a forward reference.
3151 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3152 if (GV) {
3153 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3154 "Forward metaclass reference has incorrect type.");
3155 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3156 GV->setInitializer(Init);
3157 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003158 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003159 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003160 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003161 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003162 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003163 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003164 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003165
3166 return GV;
3167}
3168
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003169llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003170 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003171
Mike Stumpf5408fe2009-05-16 07:57:57 +00003172 // FIXME: Should we look these up somewhere other than the module. Its a bit
3173 // silly since we only generate these while processing an implementation, so
3174 // exactly one pointer would work if know when we entered/exitted an
3175 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003176
3177 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003178 // Previously, metaclass with internal linkage may have been defined.
3179 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003180 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3181 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003182 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3183 "Forward metaclass reference has incorrect type.");
3184 return GV;
3185 } else {
3186 // Generate as an external reference to keep a consistent
3187 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003188 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003189 llvm::GlobalValue::ExternalLinkage,
3190 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003191 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003192 }
3193}
3194
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003195llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3196 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3197
3198 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3199 true)) {
3200 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3201 "Forward class metadata reference has incorrect type.");
3202 return GV;
3203 } else {
3204 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3205 llvm::GlobalValue::ExternalLinkage,
3206 0,
3207 Name);
3208 }
3209}
3210
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003211/*
3212 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213 uint32_t size;
3214 const char *weak_ivar_layout;
3215 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003216 };
3217*/
3218llvm::Constant *
3219CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003220 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003221 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003222
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003223 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003224 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003225 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003226 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003227 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003228
3229 // Return null if no extension bits are used.
3230 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003231 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003232
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003233 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003234 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003235 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003236 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003237 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003238}
3239
3240/*
3241 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003242 char *ivar_name;
3243 char *ivar_type;
3244 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003245 };
3246
3247 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003248 int ivar_count;
3249 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003250 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003251*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003252llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003253 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003254 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003255
3256 // When emitting the root class GCC emits ivar entries for the
3257 // actual class structure. It is not clear if we need to follow this
3258 // behavior; for now lets try and get away with not doing it. If so,
3259 // the cleanest solution would be to make up an ObjCInterfaceDecl
3260 // for the class.
3261 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003262 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003263
Jordy Rosedb8264e2011-07-22 02:08:32 +00003264 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003265
Jordy Rosedb8264e2011-07-22 02:08:32 +00003266 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003267 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003268 // Ignore unnamed bit-fields.
3269 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003270 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003271 llvm::Constant *Ivar[] = {
3272 GetMethodVarName(IVD->getIdentifier()),
3273 GetMethodVarType(IVD),
3274 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedmane5b46662012-11-06 22:15:52 +00003275 ComputeIvarBaseOffset(CGM, OID, IVD))
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003276 };
Owen Anderson08e25242009-07-27 22:29:56 +00003277 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003278 }
3279
3280 // Return null for empty list.
3281 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003282 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003283
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003284 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003285 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003286 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003287 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003288 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003289 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003290
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003291 llvm::GlobalVariable *GV;
3292 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003293 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003294 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003295 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003296 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003297 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003298 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003299 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003300 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003301}
3302
3303/*
3304 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003305 SEL method_name;
3306 char *method_types;
3307 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003308 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003309
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003310 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003311 struct objc_method_list *obsolete;
3312 int count;
3313 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003314 };
3315*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003316
3317/// GetMethodConstant - Return a struct objc_method constant for the
3318/// given method if it has been defined. The result is null if the
3319/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003320llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003321 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003322 if (!Fn)
3323 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003324
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003325 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003326 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003327 ObjCTypes.SelectorPtrTy),
3328 GetMethodVarType(MD),
3329 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3330 };
Owen Anderson08e25242009-07-27 22:29:56 +00003331 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003332}
3333
Chris Lattner5f9e2722011-07-23 10:55:15 +00003334llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003335 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003336 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003337 // Return null for empty list.
3338 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003339 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003340
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003341 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003342 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003343 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003344 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003345 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003346 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003347 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003348
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003349 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003350 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003351}
3352
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003353llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003354 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003355 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003356 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003357
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003358 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003359 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003360 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003361 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003362 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003363 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003364 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003365 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003366 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003367
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003368 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003369}
3370
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003371llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003372CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003373 llvm::Constant *Init,
3374 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003375 unsigned Align,
3376 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003377 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003378 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003379 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003380 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003381 if (Section)
3382 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003383 if (Align)
3384 GV->setAlignment(Align);
3385 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003386 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003387 return GV;
3388}
3389
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003390llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003391 // Abuse this interface function as a place to finalize.
3392 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003393 return NULL;
3394}
3395
Chris Lattner74391b42009-03-22 21:03:39 +00003396llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003397 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003398}
3399
Chris Lattner74391b42009-03-22 21:03:39 +00003400llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003401 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003402}
3403
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003404llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3405 bool copy) {
3406 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3407}
3408
David Chisnall8fac25d2010-12-26 22:13:16 +00003409llvm::Constant *CGObjCMac::GetGetStructFunction() {
3410 return ObjCTypes.getCopyStructFn();
3411}
3412llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003413 return ObjCTypes.getCopyStructFn();
3414}
3415
David Chisnalld397cfe2012-12-17 18:54:24 +00003416llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() {
3417 return ObjCTypes.getCppAtomicObjectFunction();
3418}
3419llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() {
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003420 return ObjCTypes.getCppAtomicObjectFunction();
3421}
3422
Chris Lattner74391b42009-03-22 21:03:39 +00003423llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003424 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003425}
3426
John McCallf1549f62010-07-06 01:34:17 +00003427void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3428 return EmitTryOrSynchronizedStmt(CGF, S);
3429}
3430
3431void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3432 const ObjCAtSynchronizedStmt &S) {
3433 return EmitTryOrSynchronizedStmt(CGF, S);
3434}
3435
John McCallcc505292010-07-21 06:59:36 +00003436namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003437 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003438 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003439 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003440 llvm::Value *CallTryExitVar;
3441 llvm::Value *ExceptionData;
3442 ObjCTypesHelper &ObjCTypes;
3443 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003444 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003445 llvm::Value *CallTryExitVar,
3446 llvm::Value *ExceptionData,
3447 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003448 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003449 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3450
John McCallad346f42011-07-12 20:27:29 +00003451 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003452 // Check whether we need to call objc_exception_try_exit.
3453 // In optimized code, this branch will always be folded.
3454 llvm::BasicBlock *FinallyCallExit =
3455 CGF.createBasicBlock("finally.call_exit");
3456 llvm::BasicBlock *FinallyNoCallExit =
3457 CGF.createBasicBlock("finally.no_call_exit");
3458 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3459 FinallyCallExit, FinallyNoCallExit);
3460
3461 CGF.EmitBlock(FinallyCallExit);
3462 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3463 ->setDoesNotThrow();
3464
3465 CGF.EmitBlock(FinallyNoCallExit);
3466
3467 if (isa<ObjCAtTryStmt>(S)) {
3468 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003469 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3470 // Save the current cleanup destination in case there's
3471 // control flow inside the finally statement.
3472 llvm::Value *CurCleanupDest =
3473 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3474
John McCallcc505292010-07-21 06:59:36 +00003475 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3476
John McCalld96a8e72010-08-11 00:16:14 +00003477 if (CGF.HaveInsertPoint()) {
3478 CGF.Builder.CreateStore(CurCleanupDest,
3479 CGF.getNormalCleanupDestSlot());
3480 } else {
3481 // Currently, the end of the cleanup must always exist.
3482 CGF.EnsureInsertPoint();
3483 }
3484 }
John McCallcc505292010-07-21 06:59:36 +00003485 } else {
3486 // Emit objc_sync_exit(expr); as finally's sole statement for
3487 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003488 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003489 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3490 ->setDoesNotThrow();
3491 }
3492 }
3493 };
John McCall87bb5822010-07-31 23:20:56 +00003494
3495 class FragileHazards {
3496 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003497 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003498 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3499
3500 llvm::InlineAsm *ReadHazard;
3501 llvm::InlineAsm *WriteHazard;
3502
3503 llvm::FunctionType *GetAsmFnType();
3504
3505 void collectLocals();
3506 void emitReadHazard(CGBuilderTy &Builder);
3507
3508 public:
3509 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003510
John McCall87bb5822010-07-31 23:20:56 +00003511 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003512 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003513 };
3514}
3515
3516/// Create the fragile-ABI read and write hazards based on the current
3517/// state of the function, which is presumed to be immediately prior
3518/// to a @try block. These hazards are used to maintain correct
3519/// semantics in the face of optimization and the fragile ABI's
3520/// cavalier use of setjmp/longjmp.
3521FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3522 collectLocals();
3523
3524 if (Locals.empty()) return;
3525
3526 // Collect all the blocks in the function.
3527 for (llvm::Function::iterator
3528 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3529 BlocksBeforeTry.insert(&*I);
3530
3531 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3532
3533 // Create a read hazard for the allocas. This inhibits dead-store
3534 // optimizations and forces the values to memory. This hazard is
3535 // inserted before any 'throwing' calls in the protected scope to
3536 // reflect the possibility that the variables might be read from the
3537 // catch block if the call throws.
3538 {
3539 std::string Constraint;
3540 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3541 if (I) Constraint += ',';
3542 Constraint += "*m";
3543 }
3544
3545 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3546 }
3547
3548 // Create a write hazard for the allocas. This inhibits folding
3549 // loads across the hazard. This hazard is inserted at the
3550 // beginning of the catch path to reflect the possibility that the
3551 // variables might have been written within the protected scope.
3552 {
3553 std::string Constraint;
3554 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3555 if (I) Constraint += ',';
3556 Constraint += "=*m";
3557 }
3558
3559 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3560 }
3561}
3562
3563/// Emit a write hazard at the current location.
3564void FragileHazards::emitWriteHazard() {
3565 if (Locals.empty()) return;
3566
Jay Foad4c7d9f12011-07-15 08:37:34 +00003567 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003568}
3569
John McCall87bb5822010-07-31 23:20:56 +00003570void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3571 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003572 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003573}
3574
3575/// Emit read hazards in all the protected blocks, i.e. all the blocks
3576/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003577void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003578 if (Locals.empty()) return;
3579
3580 CGBuilderTy Builder(CGF.getLLVMContext());
3581
3582 // Iterate through all blocks, skipping those prior to the try.
3583 for (llvm::Function::iterator
3584 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3585 llvm::BasicBlock &BB = *FI;
3586 if (BlocksBeforeTry.count(&BB)) continue;
3587
3588 // Walk through all the calls in the block.
3589 for (llvm::BasicBlock::iterator
3590 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3591 llvm::Instruction &I = *BI;
3592
3593 // Ignore instructions that aren't non-intrinsic calls.
3594 // These are the only calls that can possibly call longjmp.
3595 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3596 if (isa<llvm::IntrinsicInst>(I))
3597 continue;
3598
3599 // Ignore call sites marked nounwind. This may be questionable,
3600 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3601 llvm::CallSite CS(&I);
3602 if (CS.doesNotThrow()) continue;
3603
John McCall0b251722010-08-04 05:59:32 +00003604 // Insert a read hazard before the call. This will ensure that
3605 // any writes to the locals are performed before making the
3606 // call. If the call throws, then this is sufficient to
3607 // guarantee correctness as long as it doesn't also write to any
3608 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003609 Builder.SetInsertPoint(&BB, BI);
3610 emitReadHazard(Builder);
3611 }
3612 }
3613}
3614
3615static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3616 if (V) S.insert(V);
3617}
3618
3619void FragileHazards::collectLocals() {
3620 // Compute a set of allocas to ignore.
3621 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3622 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3623 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003624
3625 // Collect all the allocas currently in the function. This is
3626 // probably way too aggressive.
3627 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3628 for (llvm::BasicBlock::iterator
3629 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3630 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3631 Locals.push_back(&*I);
3632}
3633
3634llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003635 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003636 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3637 tys[i] = Locals[i]->getType();
3638 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003639}
3640
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003641/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003642
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003643 Objective-C setjmp-longjmp (sjlj) Exception Handling
3644 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003645
John McCallf1549f62010-07-06 01:34:17 +00003646 A catch buffer is a setjmp buffer plus:
3647 - a pointer to the exception that was caught
3648 - a pointer to the previous exception data buffer
3649 - two pointers of reserved storage
3650 Therefore catch buffers form a stack, with a pointer to the top
3651 of the stack kept in thread-local storage.
3652
3653 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3654 objc_exception_try_exit pops the given catch buffer, which is
3655 required to be the top of the EH stack.
3656 objc_exception_throw pops the top of the EH stack, writes the
3657 thrown exception into the appropriate field, and longjmps
3658 to the setjmp buffer. It crashes the process (with a printf
3659 and an abort()) if there are no catch buffers on the stack.
3660 objc_exception_extract just reads the exception pointer out of the
3661 catch buffer.
3662
3663 There's no reason an implementation couldn't use a light-weight
3664 setjmp here --- something like __builtin_setjmp, but API-compatible
3665 with the heavyweight setjmp. This will be more important if we ever
3666 want to implement correct ObjC/C++ exception interactions for the
3667 fragile ABI.
3668
3669 Note that for this use of setjmp/longjmp to be correct, we may need
3670 to mark some local variables volatile: if a non-volatile local
3671 variable is modified between the setjmp and the longjmp, it has
3672 indeterminate value. For the purposes of LLVM IR, it may be
3673 sufficient to make loads and stores within the @try (to variables
3674 declared outside the @try) volatile. This is necessary for
3675 optimized correctness, but is not currently being done; this is
3676 being tracked as rdar://problem/8160285
3677
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003678 The basic framework for a @try-catch-finally is as follows:
3679 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003680 objc_exception_data d;
3681 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003682 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003683
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003684 objc_exception_try_enter(&d);
3685 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003686 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003687 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003688 // exception path
3689 id _caught = objc_exception_extract(&d);
3690
3691 // enter new try scope for handlers
3692 if (!setjmp(d.jmp_buf)) {
3693 ... match exception and execute catch blocks ...
3694
3695 // fell off end, rethrow.
3696 _rethrow = _caught;
3697 ... jump-through-finally to finally_rethrow ...
3698 } else {
3699 // exception in catch block
3700 _rethrow = objc_exception_extract(&d);
3701 _call_try_exit = false;
3702 ... jump-through-finally to finally_rethrow ...
3703 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003704 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003705 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003706
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003707 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003708 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003709 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003710
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003711 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003712 ... dispatch to finally destination ...
3713
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003714 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003715 objc_exception_throw(_rethrow);
3716
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003717 finally_end:
3718 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003719
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003720 This framework differs slightly from the one gcc uses, in that gcc
3721 uses _rethrow to determine if objc_exception_try_exit should be called
3722 and if the object should be rethrown. This breaks in the face of
3723 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003724
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003725 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003726
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003727 - If there are no catch blocks, then we avoid emitting the second
3728 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003729
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003730 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3731 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003732
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003733 - FIXME: If there is no @finally block we can do a few more
3734 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003735
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003736 Rethrows and Jumps-Through-Finally
3737 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003738
John McCallf1549f62010-07-06 01:34:17 +00003739 '@throw;' is supported by pushing the currently-caught exception
3740 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003741
John McCallf1549f62010-07-06 01:34:17 +00003742 Branches through the @finally block are handled with an ordinary
3743 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3744 exceptions are not compatible with C++ exceptions, and this is
3745 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003746
John McCallf1549f62010-07-06 01:34:17 +00003747 @synchronized(expr) { stmt; } is emitted as if it were:
3748 id synch_value = expr;
3749 objc_sync_enter(synch_value);
3750 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003751*/
3752
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003753void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3754 const Stmt &S) {
3755 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003756
3757 // A destination for the fall-through edges of the catch handlers to
3758 // jump to.
3759 CodeGenFunction::JumpDest FinallyEnd =
3760 CGF.getJumpDestInCurrentScope("finally.end");
3761
3762 // A destination for the rethrow edge of the catch handlers to jump
3763 // to.
3764 CodeGenFunction::JumpDest FinallyRethrow =
3765 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003766
Daniel Dunbar1c566672009-02-24 01:43:46 +00003767 // For @synchronized, call objc_sync_enter(sync.expr). The
3768 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003769 // @synchronized. We can't avoid a temp here because we need the
3770 // value to be preserved. If the backend ever does liveness
3771 // correctly after setjmp, this will be unnecessary.
3772 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003773 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003774 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003775 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3776 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003777 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3778 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003779
3780 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3781 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003782 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003783
John McCall0b251722010-08-04 05:59:32 +00003784 // Allocate memory for the setjmp buffer. This needs to be kept
3785 // live throughout the try and catch blocks.
3786 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3787 "exceptiondata.ptr");
3788
John McCall87bb5822010-07-31 23:20:56 +00003789 // Create the fragile hazards. Note that this will not capture any
3790 // of the allocas required for exception processing, but will
3791 // capture the current basic block (which extends all the way to the
3792 // setjmp call) as "before the @try".
3793 FragileHazards Hazards(CGF);
3794
John McCallf1549f62010-07-06 01:34:17 +00003795 // Create a flag indicating whether the cleanup needs to call
3796 // objc_exception_try_exit. This is true except when
3797 // - no catches match and we're branching through the cleanup
3798 // just to rethrow the exception, or
3799 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003800 // The setjmp-safety rule here is that we should always store to this
3801 // variable in a place that dominates the branch through the cleanup
3802 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003803 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003804 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003805
John McCall9e2213d2010-10-04 23:42:51 +00003806 // A slot containing the exception to rethrow. Only needed when we
3807 // have both a @catch and a @finally.
3808 llvm::Value *PropagatingExnVar = 0;
3809
John McCallf1549f62010-07-06 01:34:17 +00003810 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003811 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003812 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003813 CallTryExitVar,
3814 ExceptionData,
3815 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003816
3817 // Enter a try block:
3818 // - Call objc_exception_try_enter to push ExceptionData on top of
3819 // the EH stack.
3820 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3821 ->setDoesNotThrow();
3822
3823 // - Call setjmp on the exception data buffer.
3824 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3825 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3826 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003827 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003828 llvm::CallInst *SetJmpResult =
3829 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3830 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003831 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003832
3833 // If setjmp returned 0, enter the protected block; otherwise,
3834 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003835 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3836 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003837 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003838 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3839 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003840
John McCallf1549f62010-07-06 01:34:17 +00003841 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003842 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003843 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003844 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003845 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003846
3847 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848
John McCallf1549f62010-07-06 01:34:17 +00003849 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003850 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003851
John McCall87bb5822010-07-31 23:20:56 +00003852 // Don't optimize loads of the in-scope locals across this point.
3853 Hazards.emitWriteHazard();
3854
John McCallf1549f62010-07-06 01:34:17 +00003855 // For a @synchronized (or a @try with no catches), just branch
3856 // through the cleanup to the rethrow block.
3857 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3858 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003859 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003860 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003861
3862 // Otherwise, we have to match against the caught exceptions.
3863 } else {
John McCall0b251722010-08-04 05:59:32 +00003864 // Retrieve the exception object. We may emit multiple blocks but
3865 // nothing can cross this so the value is already in SSA form.
3866 llvm::CallInst *Caught =
3867 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3868 ExceptionData, "caught");
3869 Caught->setDoesNotThrow();
3870
John McCallf1549f62010-07-06 01:34:17 +00003871 // Push the exception to rethrow onto the EH value stack for the
3872 // benefit of any @throws in the handlers.
3873 CGF.ObjCEHValueStack.push_back(Caught);
3874
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003875 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003876
John McCall0b251722010-08-04 05:59:32 +00003877 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003878
John McCall0b251722010-08-04 05:59:32 +00003879 llvm::BasicBlock *CatchBlock = 0;
3880 llvm::BasicBlock *CatchHandler = 0;
3881 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003882 // Save the currently-propagating exception before
3883 // objc_exception_try_enter clears the exception slot.
3884 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3885 "propagating_exception");
3886 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3887
John McCall0b251722010-08-04 05:59:32 +00003888 // Enter a new exception try block (in case a @catch block
3889 // throws an exception).
3890 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3891 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003892
John McCall0b251722010-08-04 05:59:32 +00003893 llvm::CallInst *SetJmpResult =
3894 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3895 "setjmp.result");
3896 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003897 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003898
John McCall0b251722010-08-04 05:59:32 +00003899 llvm::Value *Threw =
3900 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3901
3902 CatchBlock = CGF.createBasicBlock("catch");
3903 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3904 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3905
3906 CGF.EmitBlock(CatchBlock);
3907 }
3908
3909 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003910
Daniel Dunbar55e40722008-09-27 07:03:52 +00003911 // Handle catch list. As a special case we check if everything is
3912 // matched and avoid generating code for falling off the end if
3913 // so.
3914 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003915 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3916 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003917
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003918 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003919 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003920
Anders Carlsson80f25672008-09-09 17:59:25 +00003921 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003922 if (!CatchParam) {
3923 AllMatched = true;
3924 } else {
John McCall183700f2009-09-21 23:43:11 +00003925 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003926
John McCallf1549f62010-07-06 01:34:17 +00003927 // catch(id e) always matches under this ABI, since only
3928 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003929 // FIXME: For the time being we also match id<X>; this should
3930 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003931 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003932 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003933 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003934
John McCallf1549f62010-07-06 01:34:17 +00003935 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003936 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003937 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3938
Anders Carlssondde0a942008-09-11 09:15:33 +00003939 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003940 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003941 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003942
3943 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003944 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003945 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946
Anders Carlssondde0a942008-09-11 09:15:33 +00003947 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003948
3949 // The scope of the catch variable ends right here.
3950 CatchVarCleanups.ForceCleanup();
3951
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003952 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003953 break;
3954 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955
Steve Naroff14108da2009-07-10 23:34:53 +00003956 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003957 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003958
3959 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003960 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3961 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003962
3963 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003964 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003965
John McCallf1549f62010-07-06 01:34:17 +00003966 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003967 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3968 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003969 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003970
John McCallf1549f62010-07-06 01:34:17 +00003971 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3972 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003973
3974 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003975 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003976
Anders Carlsson80f25672008-09-09 17:59:25 +00003977 // Emit the @catch block.
3978 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003979
3980 // Collect any cleanups for the catch variable. The scope lasts until
3981 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003982 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003983
John McCallb6bbcc92010-10-15 04:57:14 +00003984 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003985 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003986
John McCallf1549f62010-07-06 01:34:17 +00003987 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003988 llvm::Value *Tmp =
3989 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003990 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003991 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003992
Anders Carlssondde0a942008-09-11 09:15:33 +00003993 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003994
3995 // We're done with the catch variable.
3996 CatchVarCleanups.ForceCleanup();
3997
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003998 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003999
Anders Carlsson80f25672008-09-09 17:59:25 +00004000 CGF.EmitBlock(NextCatchBlock);
4001 }
4002
John McCallf1549f62010-07-06 01:34:17 +00004003 CGF.ObjCEHValueStack.pop_back();
4004
John McCall0b251722010-08-04 05:59:32 +00004005 // If nothing wanted anything to do with the caught exception,
4006 // kill the extract call.
4007 if (Caught->use_empty())
4008 Caught->eraseFromParent();
4009
4010 if (!AllMatched)
4011 CGF.EmitBranchThroughCleanup(FinallyRethrow);
4012
4013 if (HasFinally) {
4014 // Emit the exception handler for the @catch blocks.
4015 CGF.EmitBlock(CatchHandler);
4016
4017 // In theory we might now need a write hazard, but actually it's
4018 // unnecessary because there's no local-accessing code between
4019 // the try's write hazard and here.
4020 //Hazards.emitWriteHazard();
4021
John McCall9e2213d2010-10-04 23:42:51 +00004022 // Extract the new exception and save it to the
4023 // propagating-exception slot.
4024 assert(PropagatingExnVar);
4025 llvm::CallInst *NewCaught =
4026 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4027 ExceptionData, "caught");
4028 NewCaught->setDoesNotThrow();
4029 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
4030
John McCall0b251722010-08-04 05:59:32 +00004031 // Don't pop the catch handler; the throw already did.
4032 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004033 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00004034 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004035 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004036
John McCall87bb5822010-07-31 23:20:56 +00004037 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00004038 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00004039
John McCallf1549f62010-07-06 01:34:17 +00004040 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00004041 CGF.Builder.restoreIP(TryFallthroughIP);
4042 if (CGF.HaveInsertPoint())
4043 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00004044 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00004045 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004046
John McCallf1549f62010-07-06 01:34:17 +00004047 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00004048 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004049 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004050 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004051 // If we have a propagating-exception variable, check it.
4052 llvm::Value *PropagatingExn;
4053 if (PropagatingExnVar) {
4054 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004055
John McCall9e2213d2010-10-04 23:42:51 +00004056 // Otherwise, just look in the buffer for the exception to throw.
4057 } else {
4058 llvm::CallInst *Caught =
4059 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4060 ExceptionData);
4061 Caught->setDoesNotThrow();
4062 PropagatingExn = Caught;
4063 }
4064
4065 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004066 ->setDoesNotThrow();
4067 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004068 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004069
John McCall87bb5822010-07-31 23:20:56 +00004070 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004071}
4072
4073void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00004074 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004075 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004076
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004077 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004078 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004079 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004080 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004081 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004082 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004083 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004084 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004085 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004086
John McCallf1549f62010-07-06 01:34:17 +00004087 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4088 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004089 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004090
4091 // Clear the insertion point to indicate we are in unreachable code.
4092 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004093}
4094
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004095/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004096/// object: objc_read_weak (id *src)
4097///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004098llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004099 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004100 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004101 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4102 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4103 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004104 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004105 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004106 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004107 return read_weak;
4108}
4109
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004110/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4111/// objc_assign_weak (id src, id *dst)
4112///
4113void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004114 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004115 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004116 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004117 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004118 assert(Size <= 8 && "does not support size > 8");
4119 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004120 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004121 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4122 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004123 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4124 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004125 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004126 src, dst, "weakassign");
4127 return;
4128}
4129
Fariborz Jahanian58626502008-11-19 00:59:10 +00004130/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4131/// objc_assign_global (id src, id *dst)
4132///
4133void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004134 llvm::Value *src, llvm::Value *dst,
4135 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004136 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004137 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004138 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004139 assert(Size <= 8 && "does not support size > 8");
4140 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004142 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4143 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004144 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4145 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004146 if (!threadlocal)
4147 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4148 src, dst, "globalassign");
4149 else
4150 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4151 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004152 return;
4153}
4154
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004155/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004156/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004157///
4158void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004159 llvm::Value *src, llvm::Value *dst,
4160 llvm::Value *ivarOffset) {
4161 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004162 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004163 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004164 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004165 assert(Size <= 8 && "does not support size > 8");
4166 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004168 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4169 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004170 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4171 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004172 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4173 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004174 return;
4175}
4176
Fariborz Jahanian58626502008-11-19 00:59:10 +00004177/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4178/// objc_assign_strongCast (id src, id *dst)
4179///
4180void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004181 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004182 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004183 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004184 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004185 assert(Size <= 8 && "does not support size > 8");
4186 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004187 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004188 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4189 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004190 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4191 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004192 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004193 src, dst, "weakassign");
4194 return;
4195}
4196
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004197void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198 llvm::Value *DestPtr,
4199 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004200 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004201 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4202 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004203 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004204 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004205 return;
4206}
4207
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004208/// EmitObjCValueForIvar - Code Gen for ivar reference.
4209///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004210LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4211 QualType ObjectTy,
4212 llvm::Value *BaseValue,
4213 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004214 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004215 const ObjCInterfaceDecl *ID =
4216 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004217 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4218 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004219}
4220
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004221llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004222 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004223 const ObjCIvarDecl *Ivar) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004224 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4225 return llvm::ConstantInt::get(
4226 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4227 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004228}
4229
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004230/* *** Private Interface *** */
4231
4232/// EmitImageInfo - Emit the image info marker used to encode some module
4233/// level information.
4234///
4235/// See: <rdr://4810609&4810587&4810587>
4236/// struct IMAGE_INFO {
4237/// unsigned version;
4238/// unsigned flags;
4239/// };
4240enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004241 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004242 eImageInfo_GarbageCollected = (1 << 1),
4243 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004244 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4245
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004246 // A flag indicating that the module has no instances of a @synthesize of a
4247 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004248 eImageInfo_CorrectedSynthesize = (1 << 4),
4249 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004250};
4251
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004252void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004253 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004254 const char *Section = (ObjCABI == 1) ?
4255 "__OBJC, __image_info,regular" :
4256 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004257
Bill Wendling3973acc2012-02-16 01:13:30 +00004258 // Generate module-level named metadata to convey this information to the
4259 // linker and code-gen.
4260 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004261
Bill Wendling3973acc2012-02-16 01:13:30 +00004262 // Add the ObjC ABI version to the module flags.
4263 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4264 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4265 version);
4266 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4267 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004268
David Blaikie4e4d0842012-03-11 07:00:24 +00004269 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004270 // Non-GC overrides those files which specify GC.
4271 Mod.addModuleFlag(llvm::Module::Override,
4272 "Objective-C Garbage Collection", (uint32_t)0);
4273 } else {
4274 // Add the ObjC garbage collection value.
4275 Mod.addModuleFlag(llvm::Module::Error,
4276 "Objective-C Garbage Collection",
4277 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004278
David Blaikie4e4d0842012-03-11 07:00:24 +00004279 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004280 // Add the ObjC GC Only value.
4281 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4282 eImageInfo_GCOnly);
4283
4284 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4285 llvm::Value *Ops[2] = {
4286 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4287 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4288 eImageInfo_GarbageCollected)
4289 };
4290 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4291 llvm::MDNode::get(VMContext, Ops));
4292 }
4293 }
Bill Wendling09822512012-04-24 11:04:57 +00004294
4295 // Indicate whether we're compiling this to run on a simulator.
4296 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4297 if (Triple.getOS() == llvm::Triple::IOS &&
4298 (Triple.getArch() == llvm::Triple::x86 ||
4299 Triple.getArch() == llvm::Triple::x86_64))
4300 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4301 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004302}
4303
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004304// struct objc_module {
4305// unsigned long version;
4306// unsigned long size;
4307// const char *name;
4308// Symtab symtab;
4309// };
4310
4311// FIXME: Get from somewhere
4312static const int ModuleVersion = 7;
4313
4314void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004315 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004316
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004317 llvm::Constant *Values[] = {
4318 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4319 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4320 // This used to be the filename, now it is unused. <rdr://4327263>
4321 GetClassName(&CGM.getContext().Idents.get("")),
4322 EmitModuleSymbols()
4323 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004324 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004325 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004326 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004327 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004328}
4329
4330llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004331 unsigned NumClasses = DefinedClasses.size();
4332 unsigned NumCategories = DefinedCategories.size();
4333
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004334 // Return null if no symbols were defined.
4335 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004336 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004337
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004338 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004339 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004340 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004341 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4342 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004343
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004344 // The runtime expects exactly the list of defined classes followed
4345 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004346 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004347 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004348 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004349 ObjCTypes.Int8PtrTy);
4350 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004351 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004352 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004353 ObjCTypes.Int8PtrTy);
4354
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004355 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004356 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004357 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004358 Symbols);
4359
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004360 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004361
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004362 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004363 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4364 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004365 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004366 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004367}
4368
John McCallf85e1932011-06-15 23:02:42 +00004369llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4370 IdentifierInfo *II) {
4371 LazySymbols.insert(II);
4372
4373 llvm::GlobalVariable *&Entry = ClassReferences[II];
4374
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004375 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004376 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004377 llvm::ConstantExpr::getBitCast(GetClassName(II),
4378 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004379 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004380 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4381 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4382 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004383 }
John McCallf85e1932011-06-15 23:02:42 +00004384
Benjamin Kramer578faa82011-09-27 21:06:10 +00004385 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004386}
4387
John McCallf85e1932011-06-15 23:02:42 +00004388llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4389 const ObjCInterfaceDecl *ID) {
4390 return EmitClassRefFromId(Builder, ID->getIdentifier());
4391}
4392
4393llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4394 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4395 return EmitClassRefFromId(Builder, II);
4396}
4397
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004398llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4399 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004400 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004401
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004402 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004404 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004405 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004406 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004407 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4408 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004409 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004410 }
4411
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004412 if (lvalue)
4413 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004414 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004415}
4416
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004417llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004418 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004419
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004420 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004421 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004422 llvm::ConstantDataArray::getString(VMContext,
4423 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004424 ((ObjCABI == 2) ?
4425 "__TEXT,__objc_classname,cstring_literals" :
4426 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004427 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004428
Owen Andersona1cf15f2009-07-14 23:10:40 +00004429 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004430}
4431
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004432llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4433 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4434 I = MethodDefinitions.find(MD);
4435 if (I != MethodDefinitions.end())
4436 return I->second;
4437
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004438 return NULL;
4439}
4440
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004441/// GetIvarLayoutName - Returns a unique constant for the given
4442/// ivar layout bitmap.
4443llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004445 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004446}
4447
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004448void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedmane5b46662012-11-06 22:15:52 +00004449 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004450 bool ForStrongLayout,
4451 bool &HasUnion) {
4452 const RecordDecl *RD = RT->getDecl();
4453 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004454 SmallVector<const FieldDecl*, 16> Fields;
4455 for (RecordDecl::field_iterator i = RD->field_begin(),
4456 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004457 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004458 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004459 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004460 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004461
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004462 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4463 ForStrongLayout, HasUnion);
4464}
4465
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004466void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004467 const llvm::StructLayout *Layout,
4468 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004469 ArrayRef<const FieldDecl*> RecFields,
Eli Friedmane5b46662012-11-06 22:15:52 +00004470 unsigned int BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004471 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004472 bool IsUnion = (RD && RD->isUnion());
Eli Friedmane5b46662012-11-06 22:15:52 +00004473 uint64_t MaxUnionIvarSize = 0;
4474 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00004475 const FieldDecl *MaxField = 0;
4476 const FieldDecl *MaxSkippedField = 0;
4477 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedmane5b46662012-11-06 22:15:52 +00004478 uint64_t MaxFieldOffset = 0;
4479 uint64_t MaxSkippedFieldOffset = 0;
4480 uint64_t LastBitfieldOrUnnamedOffset = 0;
4481 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004482
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004483 if (RecFields.empty())
4484 return;
Eli Friedmane5b46662012-11-06 22:15:52 +00004485 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004486 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004487 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004488 const FieldDecl *FirstField = RecFields[0];
4489 FirstFieldDelta =
4490 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
John McCallf85e1932011-06-15 23:02:42 +00004491 }
4492
Chris Lattnerf1690852009-03-31 08:48:01 +00004493 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004494 const FieldDecl *Field = RecFields[i];
Eli Friedmane5b46662012-11-06 22:15:52 +00004495 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004496 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004497 // Note that 'i' here is actually the field index inside RD of Field,
4498 // although this dependency is hidden.
4499 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedmane5b46662012-11-06 22:15:52 +00004500 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004501 } else
John McCallf85e1932011-06-15 23:02:42 +00004502 FieldOffset =
4503 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004504
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004505 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004506 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004507 LastFieldBitfieldOrUnnamed = Field;
4508 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004509 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004510 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004511
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004512 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004513 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004514 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004515 if (FQT->isUnionType())
4516 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004517
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004518 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004519 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004520 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004521 continue;
4522 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004523
Chris Lattnerf1690852009-03-31 08:48:01 +00004524 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004525 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004526 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004527 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004528 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004529 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004530 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4531 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004532 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004533 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004534 FQT = CArray->getElementType();
4535 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
4537 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004538 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00004539 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004540 int OldIndex = IvarsInfo.size() - 1;
4541 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004542
Ted Kremenek6217b802009-07-29 21:53:49 +00004543 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004544 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004545 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004546
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004547 // Replicate layout information for each array element. Note that
4548 // one element is already done.
4549 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004550 for (int FirstIndex = IvarsInfo.size() - 1,
4551 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004552 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004553 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4554 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4555 IvarsInfo[i].ivar_size));
4556 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4557 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4558 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004559 }
4560 continue;
4561 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004562 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004563 // At this point, we are done with Record/Union and array there of.
4564 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00004565 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004566
Eli Friedmane5b46662012-11-06 22:15:52 +00004567 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00004568 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4569 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004570 if (IsUnion) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004571 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004572 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004573 MaxUnionIvarSize = UnionIvarSize;
4574 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004575 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004576 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004577 } else {
Eli Friedmane5b46662012-11-06 22:15:52 +00004578 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
4579 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004580 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004581 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004582 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4583 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004584 if (IsUnion) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004585 // FIXME: Why the asymmetry? We divide by word size in bits on other
4586 // side.
4587 uint64_t UnionIvarSize = FieldSize / ByteSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004588 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004589 MaxSkippedUnionIvarSize = UnionIvarSize;
4590 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004591 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004592 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004593 } else {
Eli Friedmane5b46662012-11-06 22:15:52 +00004594 // FIXME: Why the asymmetry, we divide by byte size in bits here?
4595 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
4596 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004597 }
4598 }
4599 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004600
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004601 if (LastFieldBitfieldOrUnnamed) {
4602 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4603 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004604 uint64_t BitFieldSize
4605 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004606 GC_IVAR skivar;
4607 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
Eli Friedmane5b46662012-11-06 22:15:52 +00004608 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4609 + ((BitFieldSize % ByteSizeInBits) != 0);
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004610 SkipIvars.push_back(skivar);
4611 } else {
4612 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4613 // Last field was unnamed. Must update skip info.
Eli Friedmane5b46662012-11-06 22:15:52 +00004614 unsigned FieldSize
4615 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004616 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
Eli Friedmane5b46662012-11-06 22:15:52 +00004617 FieldSize / ByteSizeInBits));
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004618 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004621 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004622 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004623 MaxUnionIvarSize));
4624 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004625 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004626 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004627}
4628
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004629/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4630/// the computations and returning the layout bitmap (for ivar or blocks) in
4631/// the given argument BitMap string container. Routine reads
4632/// two containers, IvarsInfo and SkipIvars which are assumed to be
4633/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004634llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004635 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004636 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004637
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004638 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004639 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedmane5b46662012-11-06 22:15:52 +00004640 unsigned int WordSize =
4641 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
4642 if (IvarsInfo[0].ivar_bytepos == 0) {
4643 WordsToSkip = 0;
4644 WordsToScan = IvarsInfo[0].ivar_size;
4645 } else {
4646 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4647 WordsToScan = IvarsInfo[0].ivar_size;
4648 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004649 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004650 unsigned int TailPrevGCObjC =
4651 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004652 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004653 // consecutive 'scanned' object pointers.
Eli Friedmane5b46662012-11-06 22:15:52 +00004654 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004655 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004656 // Skip over 'gc'able object pointer which lay over each other.
4657 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4658 continue;
4659 // Must skip over 1 or more words. We save current skip/scan values
4660 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004661 SKIP_SCAN SkScan;
4662 SkScan.skip = WordsToSkip;
4663 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004664 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004665
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004666 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004667 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4668 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004669 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004670 WordsToSkip = 0;
Eli Friedmane5b46662012-11-06 22:15:52 +00004671 WordsToScan = IvarsInfo[i].ivar_size;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004672 }
4673 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004674 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004675 SKIP_SCAN SkScan;
4676 SkScan.skip = WordsToSkip;
4677 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004678 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004679 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004680
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004681 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004682 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedmane5b46662012-11-06 22:15:52 +00004683 int LastByteSkipped =
4684 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004685 LastIndex = IvarsInfo.size()-1;
Eli Friedmane5b46662012-11-06 22:15:52 +00004686 int LastByteScanned =
4687 IvarsInfo[LastIndex].ivar_bytepos +
4688 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004689 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004690 if (LastByteSkipped > LastByteScanned) {
Eli Friedmane5b46662012-11-06 22:15:52 +00004691 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004692 SKIP_SCAN SkScan;
Eli Friedmane5b46662012-11-06 22:15:52 +00004693 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004694 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004695 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004696 }
4697 }
4698 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4699 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004700 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004701 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004702 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4703 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4704 // 0xM0 followed by 0x0N detected.
4705 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4706 for (int j = i+1; j < SkipScan; j++)
4707 SkipScanIvars[j] = SkipScanIvars[j+1];
4708 --SkipScan;
4709 }
4710 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004711
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004712 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004713 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004714 unsigned char byte;
4715 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4716 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4717 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4718 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004719
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004720 // first skip big.
4721 for (unsigned int ix = 0; ix < skip_big; ix++)
4722 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004723
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004724 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004725 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004726 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004727 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004728 byte |= 0xf;
4729 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004730 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004731 byte |= scan_small;
4732 scan_small = 0;
4733 }
4734 BitMap += byte;
4735 }
4736 // next scan big
4737 for (unsigned int ix = 0; ix < scan_big; ix++)
4738 BitMap += (unsigned char)(0x0f);
4739 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004740 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004741 byte = scan_small;
4742 BitMap += byte;
4743 }
4744 }
4745 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004746 unsigned char zero = 0;
4747 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004748
4749 llvm::GlobalVariable * Entry =
4750 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004751 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004752 ((ObjCABI == 2) ?
4753 "__TEXT,__objc_classname,cstring_literals" :
4754 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004755 1, true);
4756 return getConstantGEP(VMContext, Entry, 0, 0);
4757}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004758
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004759/// BuildIvarLayout - Builds ivar layout bitmap for the class
4760/// implementation for the __strong or __weak case.
4761/// The layout map displays which words in ivar list must be skipped
4762/// and which must be scanned by GC (see below). String is built of bytes.
4763/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4764/// of words to skip and right nibble is count of words to scan. So, each
4765/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4766/// represented by a 0x00 byte which also ends the string.
4767/// 1. when ForStrongLayout is true, following ivars are scanned:
4768/// - id, Class
4769/// - object *
4770/// - __strong anything
4771///
4772/// 2. When ForStrongLayout is false, following ivars are scanned:
4773/// - __weak anything
4774///
4775llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4776 const ObjCImplementationDecl *OMD,
4777 bool ForStrongLayout) {
4778 bool hasUnion = false;
4779
Chris Lattner8b418682012-02-07 00:39:47 +00004780 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004781 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4782 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004783 return llvm::Constant::getNullValue(PtrTy);
4784
Jordy Rosedb8264e2011-07-22 02:08:32 +00004785 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4786 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004787 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004788 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004789 IVD; IVD = IVD->getNextIvar())
4790 RecFields.push_back(cast<FieldDecl>(IVD));
4791 }
4792 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004793 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004794 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004795
Jordy Rosedb8264e2011-07-22 02:08:32 +00004796 // FIXME: This is not ideal; we shouldn't have to do this copy.
4797 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004798 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004799
4800 if (RecFields.empty())
4801 return llvm::Constant::getNullValue(PtrTy);
4802
4803 SkipIvars.clear();
4804 IvarsInfo.clear();
4805
Eli Friedmane5b46662012-11-06 22:15:52 +00004806 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004807 if (IvarsInfo.empty())
4808 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004809 // Sort on byte position in case we encounterred a union nested in
4810 // the ivar list.
4811 if (hasUnion && !IvarsInfo.empty())
4812 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4813 if (hasUnion && !SkipIvars.empty())
4814 std::sort(SkipIvars.begin(), SkipIvars.end());
4815
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004816 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004817 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004818
David Blaikie4e4d0842012-03-11 07:00:24 +00004819 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004820 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004821 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004822 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004823 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004824 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004825 if (!(s[i] & 0xf0))
4826 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4827 else
4828 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4829 printf("\n");
4830 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004831 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004832}
4833
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004834llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004835 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4836
Chris Lattner0b239712012-02-06 22:16:34 +00004837 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004838 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004839 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004840 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004841 ((ObjCABI == 2) ?
4842 "__TEXT,__objc_methname,cstring_literals" :
4843 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004844 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004845
Owen Andersona1cf15f2009-07-14 23:10:40 +00004846 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004847}
4848
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004849// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004850llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004851 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4852}
4853
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004854llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004855 std::string TypeStr;
4856 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4857
4858 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004859
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004860 if (!Entry)
4861 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004862 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004863 ((ObjCABI == 2) ?
4864 "__TEXT,__objc_methtype,cstring_literals" :
4865 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004866 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004867
Owen Andersona1cf15f2009-07-14 23:10:40 +00004868 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004869}
4870
Bob Wilsondc8dab62011-11-30 01:57:58 +00004871llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4872 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004873 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004874 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004875 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004876
4877 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4878
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004879 if (!Entry)
4880 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004881 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004882 ((ObjCABI == 2) ?
4883 "__TEXT,__objc_methtype,cstring_literals" :
4884 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004885 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004886
Owen Andersona1cf15f2009-07-14 23:10:40 +00004887 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004888}
4889
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004890// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004891llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004892 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004893
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004894 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004895 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004896 llvm::ConstantDataArray::getString(VMContext,
4897 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004898 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004899 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004900
Owen Andersona1cf15f2009-07-14 23:10:40 +00004901 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004902}
4903
4904// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004905// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004906llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004907CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4908 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004909 std::string TypeStr;
4910 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004911 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4912}
4913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004914void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004915 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004916 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004917 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004918 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004919 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4920 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004921 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004922 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004923 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004924 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004925}
4926
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004927void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004928 EmitModuleInfo();
4929
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004930 // Emit the dummy bodies for any protocols which were referenced but
4931 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004932 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004933 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4934 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004935 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004936
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004937 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004938 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004939 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004940 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004941 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004942 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004943 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004944 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004945 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004946 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004947 }
4948
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004949 // Add assembler directives to add lazy undefined symbol references
4950 // for classes which are referenced but not defined. This is
4951 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004952 //
4953 // FIXME: It would be nice if we had an LLVM construct for this.
4954 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004955 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004956 Asm += CGM.getModule().getModuleInlineAsm();
4957 if (!Asm.empty() && Asm.back() != '\n')
4958 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004959
Daniel Dunbar33063492009-09-07 00:20:42 +00004960 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004961 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4962 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004963 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4964 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004965 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004966 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004967 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004968 }
4969
Bill Wendling13562a12012-02-07 09:06:01 +00004970 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004971 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4972 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4973 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004974
Daniel Dunbar33063492009-09-07 00:20:42 +00004975 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004976 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004977}
4978
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004979CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004980 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004981 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004982 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004983 ObjCABI = 2;
4984}
4985
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004986/* *** */
4987
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004988ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004989 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4990{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004991 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4992 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004994 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004995 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004996 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004997 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004998 Int8PtrTy = CGM.Int8PtrTy;
4999 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005000
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005001 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005002 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005003 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005004
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005005 // I'm not sure I like this. The implicit coordination is a bit
5006 // gross. We should solve this in a reasonable fashion because this
5007 // is a pretty common task (match some runtime data structure with
5008 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005010 // FIXME: This is leaked.
5011 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005013 // struct _objc_super {
5014 // id self;
5015 // Class cls;
5016 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005017 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005018 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005019 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005020 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005021 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005022 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005023 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005024 Ctx.getObjCClassType(), 0, 0, false,
5025 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005026 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005027
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005028 SuperCTy = Ctx.getTagDeclType(RD);
5029 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005030
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005031 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005032 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
5033
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005034 // struct _prop_t {
5035 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005037 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005038 PropertyTy = llvm::StructType::create("struct._prop_t",
5039 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005041 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005042 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005043 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005044 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005045 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005046 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005047 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
5048 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005049 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005050 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005051
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005052 // struct _objc_method {
5053 // SEL _cmd;
5054 // char *method_type;
5055 // char *_imp;
5056 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005057 MethodTy = llvm::StructType::create("struct._objc_method",
5058 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5059 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005060
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005061 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005062 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005063 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005064
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005065}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005066
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005067ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005068 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005069 // struct _objc_method_description {
5070 // SEL name;
5071 // char *types;
5072 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005073 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005074 llvm::StructType::create("struct._objc_method_description",
5075 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005076
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005077 // struct _objc_method_description_list {
5078 // int count;
5079 // struct _objc_method_description[1];
5080 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005082 llvm::StructType::create("struct._objc_method_description_list",
5083 IntTy,
5084 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005086 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005087 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005088 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005089
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005090 // Protocol description structures
5091
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005092 // struct _objc_protocol_extension {
5093 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5094 // struct _objc_method_description_list *optional_instance_methods;
5095 // struct _objc_method_description_list *optional_class_methods;
5096 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005097 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005098 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005099 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005100 llvm::StructType::create("struct._objc_protocol_extension",
5101 IntTy, MethodDescriptionListPtrTy,
5102 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005103 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005104
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005105 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005106 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005107
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005108 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005109
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005110 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005111 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005112
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005113 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005114 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005115 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005116 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005117 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005118 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005119
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005120 // struct _objc_protocol {
5121 // struct _objc_protocol_extension *isa;
5122 // char *protocol_name;
5123 // struct _objc_protocol **_objc_protocol_list;
5124 // struct _objc_method_description_list *instance_methods;
5125 // struct _objc_method_description_list *class_methods;
5126 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005127 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5128 llvm::PointerType::getUnqual(ProtocolListTy),
5129 MethodDescriptionListPtrTy,
5130 MethodDescriptionListPtrTy,
5131 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005132
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005133 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005134 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005135
Owen Anderson96e0fc72009-07-29 22:16:19 +00005136 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005137
5138 // Class description structures
5139
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005140 // struct _objc_ivar {
5141 // char *ivar_name;
5142 // char *ivar_type;
5143 // int ivar_offset;
5144 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005145 IvarTy = llvm::StructType::create("struct._objc_ivar",
5146 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005147
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005148 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005149 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005150 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005151 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005152
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005153 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005154 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005155 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005156 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005157
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005158 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005159 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005160 llvm::StructType::create("struct._objc_class_extension",
5161 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005162 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005163
Chris Lattnerc1c20112011-08-12 17:43:31 +00005164 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005165
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005166 // struct _objc_class {
5167 // Class isa;
5168 // Class super_class;
5169 // char *name;
5170 // long version;
5171 // long info;
5172 // long instance_size;
5173 // struct _objc_ivar_list *ivars;
5174 // struct _objc_method_list *methods;
5175 // struct _objc_cache *cache;
5176 // struct _objc_protocol_list *protocols;
5177 // char *ivar_layout;
5178 // struct _objc_class_ext *ext;
5179 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005180 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5181 llvm::PointerType::getUnqual(ClassTy),
5182 Int8PtrTy,
5183 LongTy,
5184 LongTy,
5185 LongTy,
5186 IvarListPtrTy,
5187 MethodListPtrTy,
5188 CachePtrTy,
5189 ProtocolListPtrTy,
5190 Int8PtrTy,
5191 ClassExtensionPtrTy,
5192 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005193
Owen Anderson96e0fc72009-07-29 22:16:19 +00005194 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005195
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005196 // struct _objc_category {
5197 // char *category_name;
5198 // char *class_name;
5199 // struct _objc_method_list *instance_method;
5200 // struct _objc_method_list *class_method;
5201 // uint32_t size; // sizeof(struct _objc_category)
5202 // struct _objc_property_list *instance_properties;// category's @property
5203 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005204 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005205 llvm::StructType::create("struct._objc_category",
5206 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5207 MethodListPtrTy, ProtocolListPtrTy,
5208 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005209
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005210 // Global metadata structures
5211
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005212 // struct _objc_symtab {
5213 // long sel_ref_cnt;
5214 // SEL *refs;
5215 // short cls_def_cnt;
5216 // short cat_def_cnt;
5217 // char *defs[cls_def_cnt + cat_def_cnt];
5218 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005219 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005220 llvm::StructType::create("struct._objc_symtab",
5221 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5222 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005223 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005224
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005225 // struct _objc_module {
5226 // long version;
5227 // long size; // sizeof(struct _objc_module)
5228 // char *name;
5229 // struct _objc_symtab* symtab;
5230 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005231 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005232 llvm::StructType::create("struct._objc_module",
5233 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005234
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005235
Mike Stumpf5408fe2009-05-16 07:57:57 +00005236 // FIXME: This is the size of the setjmp buffer and should be target
5237 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005238 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005239
Anders Carlsson124526b2008-09-09 10:10:21 +00005240 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005241 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
5243 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005244 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005245 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5246 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005247
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005248}
5249
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005250ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005251 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005252 // struct _method_list_t {
5253 // uint32_t entsize; // sizeof(struct _objc_method)
5254 // uint32_t method_count;
5255 // struct _objc_method method_list[method_count];
5256 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005257 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005258 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5259 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005260 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005261 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005262
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005263 // struct _protocol_t {
5264 // id isa; // NULL
5265 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005266 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005267 // const struct method_list_t * const instance_methods;
5268 // const struct method_list_t * const class_methods;
5269 // const struct method_list_t *optionalInstanceMethods;
5270 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005271 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005272 // const uint32_t size; // sizeof(struct _protocol_t)
5273 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005274 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005275 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005276
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005277 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005278 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005279 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005281 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005282 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5283 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5284 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5285 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005286 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5287 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005288
5289 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005290 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291
Fariborz Jahanianda320092009-01-29 19:24:30 +00005292 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005293 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005294 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005295 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005296 ProtocolListnfABITy->setBody(LongTy,
5297 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5298 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005300 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005301 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005302
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005303 // struct _ivar_t {
5304 // unsigned long int *offset; // pointer to ivar offset location
5305 // char *name;
5306 // char *type;
5307 // uint32_t alignment;
5308 // uint32_t size;
5309 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005310 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005311 llvm::StructType::create("struct._ivar_t",
5312 llvm::PointerType::getUnqual(LongTy),
5313 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005314
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005315 // struct _ivar_list_t {
5316 // uint32 entsize; // sizeof(struct _ivar_t)
5317 // uint32 count;
5318 // struct _iver_t list[count];
5319 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005320 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005321 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5322 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005323
Owen Anderson96e0fc72009-07-29 22:16:19 +00005324 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005325
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005326 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005327 // uint32_t const flags;
5328 // uint32_t const instanceStart;
5329 // uint32_t const instanceSize;
5330 // uint32_t const reserved; // only when building for 64bit targets
5331 // const uint8_t * const ivarLayout;
5332 // const char *const name;
5333 // const struct _method_list_t * const baseMethods;
5334 // const struct _objc_protocol_list *const baseProtocols;
5335 // const struct _ivar_list_t *const ivars;
5336 // const uint8_t * const weakIvarLayout;
5337 // const struct _prop_list_t * const properties;
5338 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005339
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005340 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005341 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5342 IntTy, IntTy, IntTy, Int8PtrTy,
5343 Int8PtrTy, MethodListnfABIPtrTy,
5344 ProtocolListnfABIPtrTy,
5345 IvarListnfABIPtrTy,
5346 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005348 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005349 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005350 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5351 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005352
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005353 // struct _class_t {
5354 // struct _class_t *isa;
5355 // struct _class_t * const superclass;
5356 // void *cache;
5357 // IMP *vtable;
5358 // struct class_ro_t *ro;
5359 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360
Chris Lattnerc1c20112011-08-12 17:43:31 +00005361 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005362 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5363 llvm::PointerType::getUnqual(ClassnfABITy),
5364 CachePtrTy,
5365 llvm::PointerType::getUnqual(ImpnfABITy),
5366 llvm::PointerType::getUnqual(ClassRonfABITy),
5367 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005368
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005369 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005370 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005371
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005372 // struct _category_t {
5373 // const char * const name;
5374 // struct _class_t *const cls;
5375 // const struct _method_list_t * const instance_methods;
5376 // const struct _method_list_t * const class_methods;
5377 // const struct _protocol_list_t * const protocols;
5378 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005379 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005380 CategorynfABITy = llvm::StructType::create("struct._category_t",
5381 Int8PtrTy, ClassnfABIPtrTy,
5382 MethodListnfABIPtrTy,
5383 MethodListnfABIPtrTy,
5384 ProtocolListnfABIPtrTy,
5385 PropertyListPtrTy,
5386 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005387
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005388 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005389 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5390 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005391
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005392 // MessageRefTy - LLVM for:
5393 // struct _message_ref_t {
5394 // IMP messenger;
5395 // SEL name;
5396 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005398 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005399 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005400 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005401 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005402 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005403 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005404 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005405 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005406 Ctx.getObjCSelType(), 0, 0, false,
5407 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005408 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005410 MessageRefCTy = Ctx.getTagDeclType(RD);
5411 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5412 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005413
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005414 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005415 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005416
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005417 // SuperMessageRefTy - LLVM for:
5418 // struct _super_message_ref_t {
5419 // SUPER_IMP messenger;
5420 // SEL name;
5421 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005422 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005423 llvm::StructType::create("struct._super_message_ref_t",
5424 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005426 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005427 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005428
Daniel Dunbare588b992009-03-01 04:46:24 +00005429
5430 // struct objc_typeinfo {
5431 // const void** vtable; // objc_ehtype_vtable + 2
5432 // const char* name; // c++ typeinfo string
5433 // Class cls;
5434 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005435 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005436 llvm::StructType::create("struct._objc_typeinfo",
5437 llvm::PointerType::getUnqual(Int8PtrTy),
5438 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005439 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005440}
5441
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005442llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005443 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005444
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005445 return NULL;
5446}
5447
Bill Wendlingbb028552012-02-07 09:25:09 +00005448void CGObjCNonFragileABIMac::
5449AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5450 const char *SymbolName,
5451 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005452 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005453
Daniel Dunbar463b8762009-05-15 21:48:48 +00005454 if (!NumClasses)
5455 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005456
Chris Lattner0b239712012-02-06 22:16:34 +00005457 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005458 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005459 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005460 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005461 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005462 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005463 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005464 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005465
Daniel Dunbar463b8762009-05-15 21:48:48 +00005466 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005467 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005468 llvm::GlobalValue::InternalLinkage,
5469 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005470 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005471 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005472 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005473 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005474}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005475
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005476void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5477 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478
Daniel Dunbar463b8762009-05-15 21:48:48 +00005479 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005480 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005481 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005482 "\01L_OBJC_LABEL_CLASS_$",
5483 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005484
Bill Wendling13562a12012-02-07 09:06:01 +00005485 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005486 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5487 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5488 continue;
5489 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005490 }
5491
Bill Wendling13562a12012-02-07 09:06:01 +00005492 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005493 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5494 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5495 continue;
5496 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5497 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005498
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005500 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5501 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005502
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005503 // Build list of all implemented category addresses in array
5504 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005505 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005506 "\01L_OBJC_LABEL_CATEGORY_$",
5507 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005508 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005509 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5510 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005512 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005513}
5514
John McCall944c8432011-05-14 03:10:52 +00005515/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5516/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005517/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005518/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005519bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5520 // At various points we've experimented with using vtable-based
5521 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005522 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005523 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005524 return false;
John McCall944c8432011-05-14 03:10:52 +00005525 case CodeGenOptions::NonLegacy:
5526 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005527 case CodeGenOptions::Mixed:
5528 break;
5529 }
5530
5531 // If so, see whether this selector is in the white-list of things which must
5532 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005533 if (VTableDispatchMethods.empty()) {
5534 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5535 VTableDispatchMethods.insert(GetNullarySelector("class"));
5536 VTableDispatchMethods.insert(GetNullarySelector("self"));
5537 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5538 VTableDispatchMethods.insert(GetNullarySelector("length"));
5539 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005540
John McCall944c8432011-05-14 03:10:52 +00005541 // These are vtable-based if GC is disabled.
5542 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005543 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005544 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5545 VTableDispatchMethods.insert(GetNullarySelector("release"));
5546 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5547 }
5548
5549 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5550 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5551 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5552 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5553 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5554 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5555 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5556
5557 // These are vtable-based if GC is enabled.
5558 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005559 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005560 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5561 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5562
5563 // "countByEnumeratingWithState:objects:count"
5564 IdentifierInfo *KeyIdents[] = {
5565 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5566 &CGM.getContext().Idents.get("objects"),
5567 &CGM.getContext().Idents.get("count")
5568 };
5569 VTableDispatchMethods.insert(
5570 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5571 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005572 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005573
John McCall944c8432011-05-14 03:10:52 +00005574 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005575}
5576
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005577/// BuildClassRoTInitializer - generate meta-data for:
5578/// struct _class_ro_t {
5579/// uint32_t const flags;
5580/// uint32_t const instanceStart;
5581/// uint32_t const instanceSize;
5582/// uint32_t const reserved; // only when building for 64bit targets
5583/// const uint8_t * const ivarLayout;
5584/// const char *const name;
5585/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005586/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005587/// const struct _ivar_list_t *const ivars;
5588/// const uint8_t * const weakIvarLayout;
5589/// const struct _prop_list_t * const properties;
5590/// }
5591///
5592llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005593 unsigned flags,
5594 unsigned InstanceStart,
5595 unsigned InstanceSize,
5596 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005597 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005598 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005599
David Blaikie4e4d0842012-03-11 07:00:24 +00005600 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005601 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005602
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005603 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5604 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5605 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005606 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005607 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5608 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005610 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005611 // const struct _method_list_t * const baseMethods;
5612 std::vector<llvm::Constant*> Methods;
5613 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005614 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005615 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005616 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005617 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005618 // Class methods should always be defined.
5619 Methods.push_back(GetMethodConstant(*i));
5620 }
5621 } else {
5622 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005623 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005624 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005625 // Instance methods should always be defined.
5626 Methods.push_back(GetMethodConstant(*i));
5627 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005628 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005629 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005630 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005631
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005632 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5633 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005634
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005635 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5636 if (llvm::Constant *C = GetMethodConstant(MD))
5637 Methods.push_back(C);
5638 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5639 if (llvm::Constant *C = GetMethodConstant(MD))
5640 Methods.push_back(C);
5641 }
5642 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005643 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005644 Values[ 5] = EmitMethodList(MethodListName,
5645 "__DATA, __objc_const", Methods);
5646
Fariborz Jahanianda320092009-01-29 19:24:30 +00005647 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5648 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005649 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005650 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005651 OID->all_referenced_protocol_begin(),
5652 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005653
John McCall621915c2012-10-17 04:53:23 +00005654 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005655 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005656 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005657 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005658 } else {
5659 Values[ 7] = EmitIvarList(ID);
5660 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005661 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5662 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005663 }
Owen Anderson08e25242009-07-27 22:29:56 +00005664 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005665 Values);
5666 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5668 llvm::GlobalValue::InternalLinkage,
5669 Init,
John McCall621915c2012-10-17 04:53:23 +00005670 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005671 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5672 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005673 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005674 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005675 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005676 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005677
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005678}
5679
5680/// BuildClassMetaData - This routine defines that to-level meta-data
5681/// for the given ClassName for:
5682/// struct _class_t {
5683/// struct _class_t *isa;
5684/// struct _class_t * const superclass;
5685/// void *cache;
5686/// IMP *vtable;
5687/// struct class_ro_t *ro;
5688/// }
5689///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005690llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005691 std::string &ClassName,
5692 llvm::Constant *IsAGV,
5693 llvm::Constant *SuperClassGV,
5694 llvm::Constant *ClassRoGV,
5695 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005696 llvm::Constant *Values[] = {
5697 IsAGV,
5698 SuperClassGV,
5699 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5700 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5701 ClassRoGV // &CLASS_RO_GV
5702 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703 if (!Values[1])
5704 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005706 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005707 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5708 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005709 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005710 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005711 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005712 if (HiddenVisibility)
5713 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005714 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005715}
5716
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005717bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005718CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005719 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005720}
5721
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005722void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005723 uint32_t &InstanceStart,
5724 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005725 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005726 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005727
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005728 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005729 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005730
5731 // If there are no fields, the start is the same as the end.
5732 if (!RL.getFieldCount())
5733 InstanceStart = InstanceSize;
5734 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005735 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005736}
5737
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005738void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5739 std::string ClassName = ID->getNameAsString();
5740 if (!ObjCEmptyCacheVar) {
5741 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005742 CGM.getModule(),
5743 ObjCTypes.CacheTy,
5744 false,
5745 llvm::GlobalValue::ExternalLinkage,
5746 0,
5747 "_objc_empty_cache");
5748
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005749 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005750 CGM.getModule(),
5751 ObjCTypes.ImpnfABITy,
5752 false,
5753 llvm::GlobalValue::ExternalLinkage,
5754 0,
5755 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005756 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005757 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005758 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005759 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005760 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005761 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005762 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005763 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005764 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5765 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005766
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005767 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005768
John McCallb03527a2012-10-17 04:53:31 +00005769 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005770 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005771 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005772 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005773 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005774
5775 // FIXME: why is this flag set on the metaclass?
5776 // ObjC metaclasses have no fields and don't really get constructed.
5777 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005778 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005779 if (!ID->hasNonZeroConstructors())
5780 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5781 }
5782
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005783 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005784 // class is root
John McCall621915c2012-10-17 04:53:23 +00005785 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005786 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005787 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005788 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005789 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005790 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5791 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5792 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005793 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005794 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005795 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005796 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005797 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005798 ObjCMetaClassName +
5799 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005800 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005801 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005802 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005803 }
5804 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5805 InstanceStart,
5806 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005807 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005808 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005809 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5810 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005811 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005812
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005813 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005814 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005815 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005816 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005817
5818 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005819 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005820
John McCallb03527a2012-10-17 04:53:31 +00005821 // Set a flag to enable a runtime optimization when a class has
5822 // fields that require destruction but which don't require
5823 // anything except zero-initialization during construction. This
5824 // is most notably true of __strong and __weak types, but you can
5825 // also imagine there being C++ types with non-trivial default
5826 // constructors that merely set all fields to null.
5827 if (!ID->hasNonZeroConstructors())
5828 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5829 }
5830
Douglas Gregor68584ed2009-06-18 16:11:24 +00005831 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005832 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005833
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005834 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005835 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005836 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005837 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005838 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005839 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005840 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005841 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005842 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005843 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005844 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005845 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005846 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005847 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005848 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005849 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005851 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005852 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005853 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5854 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005855 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005856
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005857 // Determine if this class is also "non-lazy".
5858 if (ImplementationIsNonLazy(ID))
5859 DefinedNonLazyClasses.push_back(ClassMD);
5860
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005861 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005862 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005863 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005864 // Make sure method definition entries are all clear for next implementation.
5865 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005866}
5867
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005868/// GenerateProtocolRef - This routine is called to generate code for
5869/// a protocol reference expression; as in:
5870/// @code
5871/// @protocol(Proto1);
5872/// @endcode
5873/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5874/// which will hold address of the protocol meta-data.
5875///
5876llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005877 const ObjCProtocolDecl *PD) {
5878
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005879 // This routine is called for @protocol only. So, we must build definition
5880 // of protocol's meta-data (not a reference to it!)
5881 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005882 llvm::Constant *Init =
5883 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005884 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005885
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005886 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005887 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005888
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005889 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5890 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005891 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005892 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005893 CGM.getModule(),
5894 Init->getType(), false,
5895 llvm::GlobalValue::WeakAnyLinkage,
5896 Init,
5897 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005898 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5899 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005900 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005901 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005902}
5903
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005904/// GenerateCategory - Build metadata for a category implementation.
5905/// struct _category_t {
5906/// const char * const name;
5907/// struct _class_t *const cls;
5908/// const struct _method_list_t * const instance_methods;
5909/// const struct _method_list_t * const class_methods;
5910/// const struct _protocol_list_t * const protocols;
5911/// const struct _prop_list_t * const properties;
5912/// }
5913///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005914void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005915 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005916 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005917 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5918 "_$_" + OCD->getNameAsString());
5919 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005920 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005922 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005923 Values[0] = GetClassName(OCD->getIdentifier());
5924 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005925 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005926 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005927 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5928
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005929 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005930 std::vector<llvm::Constant*> Methods;
5931 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005932 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005933 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005934
5935 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005936 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005937 // Instance methods should always be defined.
5938 Methods.push_back(GetMethodConstant(*i));
5939 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005940
5941 Values[2] = EmitMethodList(MethodListName,
5942 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005943 Methods);
5944
5945 MethodListName = Prefix;
5946 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5947 OCD->getNameAsString();
5948 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005950 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005951 // Class methods should always be defined.
5952 Methods.push_back(GetMethodConstant(*i));
5953 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005954
5955 Values[3] = EmitMethodList(MethodListName,
5956 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005957 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005958 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005959 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005960 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005961 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005962 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5963 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005964 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005965 + Interface->getName() + "_$_"
5966 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005967 Category->protocol_begin(),
5968 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005969 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5970 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005971 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005972 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5973 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005974 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005975
5976 llvm::Constant *Init =
5977 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005978 Values);
5979 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005980 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005981 false,
5982 llvm::GlobalValue::InternalLinkage,
5983 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005984 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005985 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005986 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005987 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005988 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005989 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005990
5991 // Determine if this category is also "non-lazy".
5992 if (ImplementationIsNonLazy(OCD))
5993 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005994 // method definition entries must be clear for next implementation.
5995 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005996}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005997
5998/// GetMethodConstant - Return a struct objc_method constant for the
5999/// given method if it has been defined. The result is null if the
6000/// method has not been defined. The return value has type MethodPtrTy.
6001llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006002 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00006003 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006004 if (!Fn)
6005 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006007 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00006008 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006009 ObjCTypes.SelectorPtrTy),
6010 GetMethodVarType(MD),
6011 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
6012 };
Owen Anderson08e25242009-07-27 22:29:56 +00006013 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006014}
6015
6016/// EmitMethodList - Build meta-data for method declarations
6017/// struct _method_list_t {
6018/// uint32_t entsize; // sizeof(struct _objc_method)
6019/// uint32_t method_count;
6020/// struct _objc_method method_list[method_count];
6021/// }
6022///
Bill Wendlingbb028552012-02-07 09:25:09 +00006023llvm::Constant *
6024CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
6025 const char *Section,
6026 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006027 // Return null for empty list.
6028 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006029 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006030
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006031 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006032 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00006033 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006034 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006035 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006036 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006037 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006038 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006039 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006040 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006041
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006042 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006043 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006044 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00006045 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006046 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00006047 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006048 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006049}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006050
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006051/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6052/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00006053llvm::GlobalVariable *
6054CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6055 const ObjCIvarDecl *Ivar) {
6056 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006057 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006058 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006059 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006060 CGM.getModule().getGlobalVariable(Name);
6061 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006062 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006063 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006064 false,
6065 llvm::GlobalValue::ExternalLinkage,
6066 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006067 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006068 return IvarOffsetGV;
6069}
6070
Daniel Dunbare83be122010-04-02 21:14:02 +00006071llvm::Constant *
6072CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6073 const ObjCIvarDecl *Ivar,
Eli Friedmane5b46662012-11-06 22:15:52 +00006074 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006075 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006076 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedmane5b46662012-11-06 22:15:52 +00006077 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006078 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006079 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006080
Mike Stumpf5408fe2009-05-16 07:57:57 +00006081 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6082 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006083 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6084 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006085 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006086 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006087 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006088 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006089 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006090 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006091}
6092
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006093/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006094/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006095/// IvarListnfABIPtrTy.
6096/// struct _ivar_t {
6097/// unsigned long int *offset; // pointer to ivar offset location
6098/// char *name;
6099/// char *type;
6100/// uint32_t alignment;
6101/// uint32_t size;
6102/// }
6103/// struct _ivar_list_t {
6104/// uint32 entsize; // sizeof(struct _ivar_t)
6105/// uint32 count;
6106/// struct _iver_t list[count];
6107/// }
6108///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006109
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006110llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006111 const ObjCImplementationDecl *ID) {
6112
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006113 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006114
Jordy Rosedb8264e2011-07-22 02:08:32 +00006115 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006116 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006117
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006118 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006119
Jordy Rosedb8264e2011-07-22 02:08:32 +00006120 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006121 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006122 // Ignore unnamed bit-fields.
6123 if (!IVD->getDeclName())
6124 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006125 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006126 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006127 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006128 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6129 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006130 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006131 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006132 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006133 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006134 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006135 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006136 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006137 // NOTE. Size of a bitfield does not match gcc's, because of the
6138 // way bitfields are treated special in each. But I am told that
6139 // 'size' for bitfield ivars is ignored by the runtime so it does
6140 // not matter. If it matters, there is enough info to get the
6141 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006142 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006143 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006144 }
6145 // Return null for empty list.
6146 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006147 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006148
6149 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006150 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006151 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6152 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006153 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006154 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006155 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006156 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006157 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6158 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006159 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006160 llvm::GlobalValue::InternalLinkage,
6161 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006162 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006163 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006164 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006165 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006166
Chris Lattnerad64e022009-07-17 23:57:13 +00006167 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006168 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006169}
6170
6171llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006172 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006173 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006174
Fariborz Jahanianda320092009-01-29 19:24:30 +00006175 if (!Entry) {
6176 // We use the initializer as a marker of whether this is a forward
6177 // reference or not. At module finalization we add the empty
6178 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006179 Entry =
6180 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6181 llvm::GlobalValue::ExternalLinkage,
6182 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006183 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006184 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006186
Fariborz Jahanianda320092009-01-29 19:24:30 +00006187 return Entry;
6188}
6189
6190/// GetOrEmitProtocol - Generate the protocol meta-data:
6191/// @code
6192/// struct _protocol_t {
6193/// id isa; // NULL
6194/// const char * const protocol_name;
6195/// const struct _protocol_list_t * protocol_list; // super protocols
6196/// const struct method_list_t * const instance_methods;
6197/// const struct method_list_t * const class_methods;
6198/// const struct method_list_t *optionalInstanceMethods;
6199/// const struct method_list_t *optionalClassMethods;
6200/// const struct _prop_list_t * properties;
6201/// const uint32_t size; // sizeof(struct _protocol_t)
6202/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006203/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006204/// }
6205/// @endcode
6206///
6207
6208llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006209 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006210 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006211
Fariborz Jahanianda320092009-01-29 19:24:30 +00006212 // Early exit if a defining object has already been generated.
6213 if (Entry && Entry->hasInitializer())
6214 return Entry;
6215
Douglas Gregor1d784b22012-01-01 19:51:50 +00006216 // Use the protocol definition, if there is one.
6217 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6218 PD = Def;
6219
Fariborz Jahanianda320092009-01-29 19:24:30 +00006220 // Construct method lists.
6221 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6222 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006223 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006224 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006225 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006226 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006227 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006228 if (!C)
6229 return GetOrEmitProtocolRef(PD);
6230
Fariborz Jahanianda320092009-01-29 19:24:30 +00006231 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6232 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006233 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006234 } else {
6235 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006236 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006237 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006238 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006239
6240 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006241 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006242 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006243 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006244 if (!C)
6245 return GetOrEmitProtocolRef(PD);
6246
Fariborz Jahanianda320092009-01-29 19:24:30 +00006247 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6248 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006249 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006250 } else {
6251 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006252 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006253 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006254 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006255
Bob Wilsondc8dab62011-11-30 01:57:58 +00006256 MethodTypesExt.insert(MethodTypesExt.end(),
6257 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6258
6259 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006260 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006261 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006262 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006263 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6264 PD->protocol_begin(),
6265 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006266
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006267 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006268 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006269 "__DATA, __objc_const",
6270 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006271 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006272 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006273 "__DATA, __objc_const",
6274 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006275 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006276 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006277 "__DATA, __objc_const",
6278 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006279 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006280 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006281 "__DATA, __objc_const",
6282 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006283 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006284 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006285 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006286 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006287 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006288 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006289 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6290 + PD->getName(),
6291 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006292 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006293 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006294
Fariborz Jahanianda320092009-01-29 19:24:30 +00006295 if (Entry) {
6296 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006297 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006298 Entry->setInitializer(Init);
6299 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006300 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006301 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6302 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6303 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006304 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006305 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006306 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006307
6308 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006309 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006310 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006311 CGM.AddUsedGlobal(Entry);
6312
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006313 // Use this protocol meta-data to build protocol list table in section
6314 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006315 llvm::GlobalVariable *PTGV =
6316 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6317 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6318 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006319 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006320 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006321 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006322 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006323 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006324 return Entry;
6325}
6326
6327/// EmitProtocolList - Generate protocol list meta-data:
6328/// @code
6329/// struct _protocol_list_t {
6330/// long protocol_count; // Note, this is 32/64 bit
6331/// struct _protocol_t[protocol_count];
6332/// }
6333/// @endcode
6334///
6335llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006336CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006337 ObjCProtocolDecl::protocol_iterator begin,
6338 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00006339 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006340
Fariborz Jahanianda320092009-01-29 19:24:30 +00006341 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006342 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006343 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006344
Daniel Dunbar948e2582009-02-15 07:36:20 +00006345 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006346 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006347 Name.toVector(TmpName);
6348 llvm::GlobalVariable *GV =
6349 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006350 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006351 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006352
Daniel Dunbar948e2582009-02-15 07:36:20 +00006353 for (; begin != end; ++begin)
6354 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6355
Fariborz Jahanianda320092009-01-29 19:24:30 +00006356 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006357 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006358 ObjCTypes.ProtocolnfABIPtrTy));
6359
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006360 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006361 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006362 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006363 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006364 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6365 ProtocolRefs.size()),
6366 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006367
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006368 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006369 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006370 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006371 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006372 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006373 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006374 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006375 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006376 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006377 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006378}
6379
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006380/// GetMethodDescriptionConstant - This routine build following meta-data:
6381/// struct _objc_method {
6382/// SEL _cmd;
6383/// char *method_type;
6384/// char *_imp;
6385/// }
6386
6387llvm::Constant *
6388CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006389 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006390 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006391 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6392 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006393 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006394 if (!Desc[1])
6395 return 0;
6396
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006397 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006398 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006399 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006400}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006401
6402/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6403/// This code gen. amounts to generating code for:
6404/// @code
6405/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6406/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006407///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006408LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006409 CodeGen::CodeGenFunction &CGF,
6410 QualType ObjectTy,
6411 llvm::Value *BaseValue,
6412 const ObjCIvarDecl *Ivar,
6413 unsigned CVRQualifiers) {
6414 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006415 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6416 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6417 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6418 llvm::MDNode::get(VMContext,
6419 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006420 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006421 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006422}
6423
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006424llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006425 CodeGen::CodeGenFunction &CGF,
6426 const ObjCInterfaceDecl *Interface,
6427 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006428 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006429}
6430
John McCallb1e81442011-05-13 23:16:18 +00006431static void appendSelectorForMessageRefTable(std::string &buffer,
6432 Selector selector) {
6433 if (selector.isUnarySelector()) {
6434 buffer += selector.getNameForSlot(0);
6435 return;
6436 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006437
John McCallb1e81442011-05-13 23:16:18 +00006438 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6439 buffer += selector.getNameForSlot(i);
6440 buffer += '_';
6441 }
6442}
6443
John McCall944c8432011-05-14 03:10:52 +00006444/// Emit a "v-table" message send. We emit a weak hidden-visibility
6445/// struct, initially containing the selector pointer and a pointer to
6446/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6447/// load and call the function pointer, passing the address of the
6448/// struct as the second parameter. The runtime determines whether
6449/// the selector is currently emitted using vtable dispatch; if so, it
6450/// substitutes a stub function which simply tail-calls through the
6451/// appropriate vtable slot, and if not, it substitues a stub function
6452/// which tail-calls objc_msgSend. Both stubs adjust the selector
6453/// argument to correctly point to the selector.
6454RValue
6455CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6456 ReturnValueSlot returnSlot,
6457 QualType resultType,
6458 Selector selector,
6459 llvm::Value *arg0,
6460 QualType arg0Type,
6461 bool isSuper,
6462 const CallArgList &formalArgs,
6463 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006464 // Compute the actual arguments.
6465 CallArgList args;
6466
John McCall944c8432011-05-14 03:10:52 +00006467 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006468 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006469 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6470 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006471
John McCall944c8432011-05-14 03:10:52 +00006472 // Second argument: a pointer to the message ref structure. Leave
6473 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006474 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6475
6476 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6477
John McCallde5d3c72012-02-17 03:33:10 +00006478 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006479
John McCallcba681a2011-05-14 21:12:11 +00006480 NullReturnState nullReturn;
6481
John McCall944c8432011-05-14 03:10:52 +00006482 // Find the function to call and the mangled name for the message
6483 // ref structure. Using a different mangled name wouldn't actually
6484 // be a problem; it would just be a waste.
6485 //
6486 // The runtime currently never uses vtable dispatch for anything
6487 // except normal, non-super message-sends.
6488 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006489 llvm::Constant *fn = 0;
6490 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006491 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006492 if (isSuper) {
6493 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6494 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006495 } else {
John McCallcba681a2011-05-14 21:12:11 +00006496 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006497 fn = ObjCTypes.getMessageSendStretFixupFn();
6498 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006499 }
John McCallb1e81442011-05-13 23:16:18 +00006500 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6501 fn = ObjCTypes.getMessageSendFpretFixupFn();
6502 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006503 } else {
John McCallb1e81442011-05-13 23:16:18 +00006504 if (isSuper) {
6505 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6506 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006507 } else {
John McCallb1e81442011-05-13 23:16:18 +00006508 fn = ObjCTypes.getMessageSendFixupFn();
6509 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006510 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006511 }
John McCallb1e81442011-05-13 23:16:18 +00006512 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6513 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006514
John McCallb1e81442011-05-13 23:16:18 +00006515 // Append the selector name, except use underscores anywhere we
6516 // would have used colons.
6517 appendSelectorForMessageRefTable(messageRefName, selector);
6518
6519 llvm::GlobalVariable *messageRef
6520 = CGM.getModule().getGlobalVariable(messageRefName);
6521 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006522 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006523 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006524 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006525 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6526 init->getType(),
6527 /*constant*/ false,
6528 llvm::GlobalValue::WeakAnyLinkage,
6529 init,
6530 messageRefName);
6531 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6532 messageRef->setAlignment(16);
6533 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6534 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006535
6536 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006537 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006538 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6539 e = method->param_end(); i != e; ++i) {
6540 const ParmVarDecl *ParamDecl = (*i);
6541 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6542 if (!nullReturn.NullBB)
6543 nullReturn.init(CGF, arg0);
6544 requiresnullCheck = true;
6545 break;
6546 }
6547 }
6548
John McCallb1e81442011-05-13 23:16:18 +00006549 llvm::Value *mref =
6550 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6551
John McCall944c8432011-05-14 03:10:52 +00006552 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006553 args[1].RV = RValue::get(mref);
6554
6555 // Load the function to call from the message ref table.
6556 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6557 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6558
John McCallde5d3c72012-02-17 03:33:10 +00006559 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006560
John McCallde5d3c72012-02-17 03:33:10 +00006561 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006562 return nullReturn.complete(CGF, result, resultType, formalArgs,
6563 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006564}
6565
6566/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006567CodeGen::RValue
6568CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006569 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006570 QualType ResultType,
6571 Selector Sel,
6572 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006573 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006574 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006575 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006576 return isVTableDispatchedSelector(Sel)
6577 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006578 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006579 false, CallArgs, Method)
6580 : EmitMessageSend(CGF, Return, ResultType,
6581 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006582 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006583 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006584}
6585
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006586llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006587CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006588 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6589
Daniel Dunbardfff2302009-03-02 05:18:14 +00006590 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006591 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006592 false, llvm::GlobalValue::ExternalLinkage,
6593 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006594 }
6595
6596 return GV;
6597}
6598
John McCallf85e1932011-06-15 23:02:42 +00006599llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6600 IdentifierInfo *II) {
6601 llvm::GlobalVariable *&Entry = ClassReferences[II];
6602
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006603 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006604 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006605 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006606 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006607 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6608 false, llvm::GlobalValue::InternalLinkage,
6609 ClassGV,
6610 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006611 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006612 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006613 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006614 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006615 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006616 }
John McCallf85e1932011-06-15 23:02:42 +00006617
Benjamin Kramer578faa82011-09-27 21:06:10 +00006618 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006619}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006620
John McCallf85e1932011-06-15 23:02:42 +00006621llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6622 const ObjCInterfaceDecl *ID) {
6623 return EmitClassRefFromId(Builder, ID->getIdentifier());
6624}
6625
6626llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6627 CGBuilderTy &Builder) {
6628 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6629 return EmitClassRefFromId(Builder, II);
6630}
6631
Daniel Dunbar11394522009-04-18 08:51:00 +00006632llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006633CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006634 const ObjCInterfaceDecl *ID) {
6635 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006636
Daniel Dunbar11394522009-04-18 08:51:00 +00006637 if (!Entry) {
6638 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6639 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006640 Entry =
6641 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006642 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006643 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006644 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006645 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006646 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006647 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006648 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006649 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006650 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006651
Benjamin Kramer578faa82011-09-27 21:06:10 +00006652 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006653}
6654
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006655/// EmitMetaClassRef - Return a Value * of the address of _class_t
6656/// meta-data
6657///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006658llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6659 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006660 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6661 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006662 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006663
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006664 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006665 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006666 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006667 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006668 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006669 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006670 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006671 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006672 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006673 ObjCTypes.ClassnfABIPtrTy));
6674
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006675 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006676 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006677
Benjamin Kramer578faa82011-09-27 21:06:10 +00006678 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006679}
6680
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006681/// GetClass - Return a reference to the class for the given interface
6682/// decl.
6683llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6684 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006685 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006686 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6687 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6688 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6689 }
6690
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006691 return EmitClassRef(Builder, ID);
6692}
6693
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006694/// Generates a message send where the super is the receiver. This is
6695/// a message send to self with special delivery semantics indicating
6696/// which class's method should be called.
6697CodeGen::RValue
6698CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006699 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006700 QualType ResultType,
6701 Selector Sel,
6702 const ObjCInterfaceDecl *Class,
6703 bool isCategoryImpl,
6704 llvm::Value *Receiver,
6705 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006706 const CodeGen::CallArgList &CallArgs,
6707 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006708 // ...
6709 // Create and init a super structure; this is a (receiver, class)
6710 // pair we will pass to objc_msgSendSuper.
6711 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006712 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006713
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006714 llvm::Value *ReceiverAsObject =
6715 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6716 CGF.Builder.CreateStore(ReceiverAsObject,
6717 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006718
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006719 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006720 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006721 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006722 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006723 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006724 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006725
Mike Stumpf5408fe2009-05-16 07:57:57 +00006726 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6727 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006728 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006729 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6730 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6731 CGF.Builder.CreateStore(Target,
6732 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006733
John McCall944c8432011-05-14 03:10:52 +00006734 return (isVTableDispatchedSelector(Sel))
6735 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006736 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006737 true, CallArgs, Method)
6738 : EmitMessageSend(CGF, Return, ResultType,
6739 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006740 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006741 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006742}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006743
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006744llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006745 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006746 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006747
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006748 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006749 llvm::Constant *Casted =
6750 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6751 ObjCTypes.SelectorPtrTy);
6752 Entry =
6753 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6754 llvm::GlobalValue::InternalLinkage,
6755 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006756 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006757 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006758 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006759
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006760 if (lval)
6761 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006762 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6763
6764 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6765 llvm::MDNode::get(VMContext,
6766 ArrayRef<llvm::Value*>()));
6767 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006768}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006769/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006770/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006771///
6772void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006773 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006774 llvm::Value *dst,
6775 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006776 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006777 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006778 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006779 assert(Size <= 8 && "does not support size > 8");
6780 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6781 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006782 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6783 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006784 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6785 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006786 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6787 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006788 return;
6789}
6790
6791/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6792/// objc_assign_strongCast (id src, id *dst)
6793///
6794void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006795 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006796 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006797 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006798 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006799 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006800 assert(Size <= 8 && "does not support size > 8");
6801 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006802 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006803 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6804 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006805 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6806 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006807 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006808 src, dst, "weakassign");
6809 return;
6810}
6811
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006812void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006813 CodeGen::CodeGenFunction &CGF,
6814 llvm::Value *DestPtr,
6815 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006816 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006817 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6818 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006819 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006820 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006821 return;
6822}
6823
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006824/// EmitObjCWeakRead - Code gen for loading value of a __weak
6825/// object: objc_read_weak (id *src)
6826///
6827llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006828 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006829 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006830 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006831 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6832 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006833 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006834 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006835 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006836 return read_weak;
6837}
6838
6839/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6840/// objc_assign_weak (id src, id *dst)
6841///
6842void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006843 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006844 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006845 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006846 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006847 assert(Size <= 8 && "does not support size > 8");
6848 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6849 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006850 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6851 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006852 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6853 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006854 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006855 src, dst, "weakassign");
6856 return;
6857}
6858
6859/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6860/// objc_assign_global (id src, id *dst)
6861///
6862void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006863 llvm::Value *src, llvm::Value *dst,
6864 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006865 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006866 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006867 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006868 assert(Size <= 8 && "does not support size > 8");
6869 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6870 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006871 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6872 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006873 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6874 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006875 if (!threadlocal)
6876 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6877 src, dst, "globalassign");
6878 else
6879 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6880 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006881 return;
6882}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006883
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006884void
John McCallf1549f62010-07-06 01:34:17 +00006885CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6886 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006887 EmitAtSynchronizedStmt(CGF, S,
6888 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6889 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006890}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006891
John McCall5a180392010-07-24 00:37:23 +00006892llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006893CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006894 // There's a particular fixed type info for 'id'.
6895 if (T->isObjCIdType() ||
6896 T->isObjCQualifiedIdType()) {
6897 llvm::Constant *IDEHType =
6898 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6899 if (!IDEHType)
6900 IDEHType =
6901 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6902 false,
6903 llvm::GlobalValue::ExternalLinkage,
6904 0, "OBJC_EHTYPE_id");
6905 return IDEHType;
6906 }
6907
6908 // All other types should be Objective-C interface pointer types.
6909 const ObjCObjectPointerType *PT =
6910 T->getAs<ObjCObjectPointerType>();
6911 assert(PT && "Invalid @catch type.");
6912 const ObjCInterfaceType *IT = PT->getInterfaceType();
6913 assert(IT && "Invalid @catch type.");
6914 return GetInterfaceEHType(IT->getDecl(), false);
6915}
6916
John McCallf1549f62010-07-06 01:34:17 +00006917void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6918 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006919 EmitTryCatchStmt(CGF, S,
6920 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6921 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6922 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006923}
6924
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006925/// EmitThrowStmt - Generate code for a throw statement.
6926void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6927 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006928 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006929 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006930 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006931 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006932 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006933 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006934 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006935 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006936 }
6937
John McCall7ec404c2010-10-16 08:21:07 +00006938 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006939 CGF.Builder.ClearInsertionPoint();
6940}
Daniel Dunbare588b992009-03-01 04:46:24 +00006941
John McCall5a180392010-07-24 00:37:23 +00006942llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006943CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006944 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006945 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006946
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006947 // If we don't need a definition, return the entry if found or check
6948 // if we use an external reference.
6949 if (!ForDefinition) {
6950 if (Entry)
6951 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006952
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006953 // If this type (or a super class) has the __objc_exception__
6954 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006955 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006956 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006957 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006958 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006959 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006960 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006961 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006962 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006963
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006964 // Otherwise we need to either make a new entry or fill in the
6965 // initializer.
6966 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006967 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006968 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006969 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006970 CGM.getModule().getGlobalVariable(VTableName);
6971 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006972 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6973 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006974 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006975 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006976
Chris Lattner8b418682012-02-07 00:39:47 +00006977 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006978
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006979 llvm::Constant *Values[] = {
6980 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6981 GetClassName(ID->getIdentifier()),
6982 GetClassGlobal(ClassName)
6983 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006984 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006985 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006986
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006987 if (Entry) {
6988 Entry->setInitializer(Init);
6989 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006990 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006991 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006992 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006993 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006994 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006995 }
6996
David Blaikie4e4d0842012-03-11 07:00:24 +00006997 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006998 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006999 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00007000 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00007001
7002 if (ForDefinition) {
7003 Entry->setSection("__DATA,__objc_const");
7004 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
7005 } else {
7006 Entry->setSection("__DATA,__datacoal_nt,coalesced");
7007 }
Daniel Dunbare588b992009-03-01 04:46:24 +00007008
7009 return Entry;
7010}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00007011
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00007012/* *** */
7013
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00007014CodeGen::CGObjCRuntime *
7015CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00007016 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
7017 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00007018 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00007019
7020 case ObjCRuntime::MacOSX:
7021 case ObjCRuntime::iOS:
7022 return new CGObjCNonFragileABIMac(CGM);
7023
David Chisnall11d3f4c2012-07-03 20:49:52 +00007024 case ObjCRuntime::GNUstep:
7025 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00007026 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00007027 llvm_unreachable("these runtimes are not Mac runtimes");
7028 }
7029 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00007030}