blob: 281bb7e1db48c09ccf091a74c5e4067fe2cb2373 [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"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000039#include "llvm/DataLayout.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046
Daniel Dunbar6bff2512009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000049
Fariborz Jahanianee0af742009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000053
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000054private:
John McCall0774cb82011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCallf85e1932011-06-15 23:02:42 +000068 "objc_msgSend",
Bill Wendling50e6b182012-10-15 04:47:45 +000069 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +000070 llvm::Attributes::NonLazyBind));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000071 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000072
John McCall0774cb82011-05-15 01:53:33 +000073 /// void objc_msgSend_stret (id, SEL, ...)
74 ///
75 /// The messenger used when the return value is an aggregate returned
76 /// by indirect reference in the first argument, and therefore the
77 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000078 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000079 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000080 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
81 params, true),
82 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000083
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000084 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
John McCall0774cb82011-05-15 01:53:33 +000086 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
87 ///
88 /// The messenger used when the return value is returned on the x87
89 /// floating-point stack; without a special entrypoint, the nil case
90 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000092 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000093 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
94 params, true),
John McCall0774cb82011-05-15 01:53:33 +000095 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000096
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000097 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000098
Anders Carlssoneea64802011-10-31 16:27:11 +000099 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
100 ///
101 /// The messenger used when the return value is returned in two values on the
102 /// x87 floating point stack; without a special entrypoint, the nil case
103 /// would be unbalanced. Only used on 64-bit X86.
104 llvm::Constant *getMessageSendFp2retFn() const {
105 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
106 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
107 llvm::Type *resultType =
108 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
109
110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
111 params, true),
112 "objc_msgSend_fp2ret");
113 }
114
John McCall0774cb82011-05-15 01:53:33 +0000115 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
116 ///
117 /// The messenger used for super calls, which have different dispatch
118 /// semantics. The class passed is the superclass of the current
119 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000120 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000121 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000122 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000123 params, true),
124 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000126
John McCall0774cb82011-05-15 01:53:33 +0000127 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
128 ///
129 /// A slightly different messenger used for super calls. The class
130 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000131 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000132 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000133 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000134 params, true),
135 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000137
John McCall0774cb82011-05-15 01:53:33 +0000138 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
139 /// SEL op, ...)
140 ///
141 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000142 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000143 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000144 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000145 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000148
John McCall0774cb82011-05-15 01:53:33 +0000149 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
150 /// SEL op, ...)
151 ///
152 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000153 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000155 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000156 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000158 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160 llvm::Constant *getMessageSendSuperFpretFn() const {
161 // There is no objc_msgSendSuper_fpret? How can that work?
162 return getMessageSendSuperFn();
163 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000164
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 llvm::Constant *getMessageSendSuperFpretFn2() const {
166 // There is no objc_msgSendSuper_fpret? How can that work?
167 return getMessageSendSuperFn2();
168 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000170protected:
171 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000173public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000175 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000176
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000177 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000178 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000180 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000181 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000183 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000185
186private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000187 /// ProtocolPtrTy - LLVM type for external protocol handles
188 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000189 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000190
191public:
192 llvm::Type *getExternalProtocolPtrTy() {
193 if (!ExternalProtocolPtrTy) {
194 // FIXME: It would be nice to unify this with the opaque type, so that the
195 // IR comes out a bit cleaner.
196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
197 ASTContext &Ctx = CGM.getContext();
198 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
199 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
200 }
201
202 return ExternalProtocolPtrTy;
203 }
204
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000205 // SuperCTy - clang type for struct objc_super.
206 QualType SuperCTy;
207 // SuperPtrCTy - clang type for struct objc_super *.
208 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000209
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000210 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000211 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000212 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
216 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000217 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000219 /// PropertyListTy - LLVM type for struct objc_property_list
220 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000221 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000224
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000225 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000226 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000228 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000230 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000232
Chris Lattner72db6c32009-04-22 02:44:54 +0000233 llvm::Constant *getGetPropertyFn() {
234 CodeGen::CodeGenTypes &Types = CGM.getTypes();
235 ASTContext &Ctx = CGM.getContext();
236 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000238 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
239 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000240 Params.push_back(IdType);
241 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000242 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000243 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000244 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000245 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
246 FunctionType::ExtInfo(),
247 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Chris Lattner72db6c32009-04-22 02:44:54 +0000251 llvm::Constant *getSetPropertyFn() {
252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
253 ASTContext &Ctx = CGM.getContext();
254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000256 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
257 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000258 Params.push_back(IdType);
259 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000260 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(Ctx.BoolTy);
263 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000264 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000265 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
266 FunctionType::ExtInfo(),
267 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000268 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
269 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000271 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_setProperty_atomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_nonatomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282
283 SmallVector<CanQualType,4> Params;
284 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
285 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
286 Params.push_back(IdType);
287 Params.push_back(SelType);
288 Params.push_back(IdType);
289 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
290 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000291 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
292 FunctionType::ExtInfo(),
293 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000294 const char *name;
295 if (atomic && copy)
296 name = "objc_setProperty_atomic_copy";
297 else if (atomic && !copy)
298 name = "objc_setProperty_atomic";
299 else if (!atomic && copy)
300 name = "objc_setProperty_nonatomic_copy";
301 else
302 name = "objc_setProperty_nonatomic";
303
304 return CGM.CreateRuntimeFunction(FTy, name);
305 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000306
307 llvm::Constant *getCopyStructFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000318 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
319 FunctionType::ExtInfo(),
320 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
322 }
323
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000324 /// This routine declares and returns address of:
325 /// void objc_copyCppObjectAtomic(
326 /// void *dest, const void *src,
327 /// void (*copyHelper) (void *dest, const void *source));
328 llvm::Constant *getCppAtomicObjectFunction() {
329 CodeGen::CodeGenTypes &Types = CGM.getTypes();
330 ASTContext &Ctx = CGM.getContext();
331 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
332 SmallVector<CanQualType,3> Params;
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000337 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
338 FunctionType::ExtInfo(),
339 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
341 }
342
Chris Lattner72db6c32009-04-22 02:44:54 +0000343 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000346 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000348 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000349 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000350 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000351 FunctionType::ExtInfo(),
352 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
354 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000355
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000357 llvm::Constant *getGcReadWeakFn() {
358 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000361 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000363 }
364
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000365 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000366 llvm::Constant *getGcAssignWeakFn() {
367 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000368 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000369 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000370 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignGlobalFn() {
376 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
381 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000383 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
384 llvm::Constant *getGcAssignThreadLocalFn() {
385 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000387 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000388 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
390 }
391
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000392 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000393 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000394 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000395 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
396 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000398 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000402 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
403 llvm::Constant *GcMemmoveCollectableFn() {
404 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000406 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000410 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000411 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000412 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000413 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000414 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000415 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
417 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000418
419 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000420 llvm::Constant *getExceptionThrowFn() {
421 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000422 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000424 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
426 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000427
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000428 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
429 llvm::Constant *getExceptionRethrowFn() {
430 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000431 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
433 }
434
Daniel Dunbar1c566672009-02-24 01:43:46 +0000435 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000436 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000437 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000439 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000440 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000443
Daniel Dunbar1c566672009-02-24 01:43:46 +0000444 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000446 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000449 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
451 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000453 llvm::Constant *getSendFn(bool IsSuper) const {
454 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000456
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000457 llvm::Constant *getSendFn2(bool IsSuper) const {
458 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000461 llvm::Constant *getSendStretFn(bool IsSuper) const {
462 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
463 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000464
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000465 llvm::Constant *getSendStretFn2(bool IsSuper) const {
466 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
467 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000468
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000469 llvm::Constant *getSendFpretFn(bool IsSuper) const {
470 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000473 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
474 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
475 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000476
Anders Carlssoneea64802011-10-31 16:27:11 +0000477 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
478 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
479 }
480
481 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
482 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
483 }
484
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000485 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
486 ~ObjCCommonTypesHelper(){}
487};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000488
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000489/// ObjCTypesHelper - Helper class that encapsulates lazy
490/// construction of varies types used during ObjC generation.
491class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000492public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000493 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000497 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000499
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000500 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000503 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504 /// ProtocolExtensionTy - LLVM type for struct
505 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 /// ProtocolExtensionTy - LLVM type for struct
508 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000510 /// MethodDescriptionTy - LLVM type for struct
511 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000513 /// MethodDescriptionListTy - LLVM type for struct
514 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// MethodDescriptionListPtrTy - LLVM type for struct
517 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000523 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Anders Carlsson124526b2008-09-09 10:10:21 +0000544 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000546
Anders Carlsson124526b2008-09-09 10:10:21 +0000547 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000548 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000549 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000550 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000551 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000553 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000554
555 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000558 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000559 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000561 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000562
563 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000564 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000566 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000567 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000568 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Anders Carlsson124526b2008-09-09 10:10:21 +0000571 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000572 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000573 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000575 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 "objc_exception_match");
577
Chris Lattner34b02a12009-04-22 02:26:14 +0000578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Anders Carlsson124526b2008-09-09 10:10:21 +0000580 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000582 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000584 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
585 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000586 "_setjmp",
Bill Wendling50e6b182012-10-15 04:47:45 +0000587 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +0000588 llvm::Attributes::NonLazyBind));
Chris Lattner34b02a12009-04-22 02:26:14 +0000589 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000591public:
592 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000593 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000594};
595
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000596/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000599public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000601 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000604 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000605 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000607 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000608 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Daniel Dunbar948e2582009-02-15 07:36:20 +0000610 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000611 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000612
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000613 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000614 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000615
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000616 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000617 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000619 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000620 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000622 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000623 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000625 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000626 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000628 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000629 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000632 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000635 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000638 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000640 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000641 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000643 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000645 // MessageRefTy - LLVM for:
646 // struct _message_ref_t {
647 // IMP messenger;
648 // SEL name;
649 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000650 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000651 // MessageRefCTy - clang type for struct _message_ref_t
652 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000654 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000655 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000656 // MessageRefCPtrTy - clang type for struct _message_ref_t*
657 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahanianef163782009-02-05 01:13:09 +0000659 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000660 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000662 // SuperMessageRefTy - LLVM for:
663 // struct _super_message_ref_t {
664 // SUPER_IMP messenger;
665 // SEL name;
666 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000667 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000669 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000670 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000671
Chris Lattner1c02f862009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendFixupFn() {
673 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000674 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000676 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000677 "objc_msgSend_fixup");
678 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Chris Lattner1c02f862009-04-22 02:53:24 +0000680 llvm::Constant *getMessageSendFpretFixupFn() {
681 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000682 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000684 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000685 "objc_msgSend_fpret_fixup");
686 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000687
Chris Lattner1c02f862009-04-22 02:53:24 +0000688 llvm::Constant *getMessageSendStretFixupFn() {
689 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000692 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000693 "objc_msgSend_stret_fixup");
694 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000699 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000700 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000701 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 "objc_msgSendSuper2_fixup");
703 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Chris Lattner1c02f862009-04-22 02:53:24 +0000705 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000708 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000710 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000711 "objc_msgSendSuper2_stret_fixup");
712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000713
Chris Lattner8a569112009-04-22 02:15:23 +0000714 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000716 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner8a569112009-04-22 02:15:23 +0000720 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000721 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000723 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000724 "objc_begin_catch");
725 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000726
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000727 llvm::StructType *EHTypeTy;
728 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000729
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000730 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
731 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000733
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000735public:
736 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000737 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000738 public:
Eli Friedman38ad4f42012-11-06 01:51:20 +0000739 CharUnits ivar_bytepos;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000740 unsigned ivar_size;
Eli Friedman38ad4f42012-11-06 01:51:20 +0000741 GC_IVAR(CharUnits bytepos = CharUnits::Zero(), unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000743
744 // Allow sorting based on byte pos.
745 bool operator<(const GC_IVAR &b) const {
746 return ivar_bytepos < b.ivar_bytepos;
747 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000748 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000750 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000751 public:
752 unsigned skip;
753 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000754 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000755 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000756 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000758 /// opcode for captured block variables layout 'instructions'.
759 /// In the following descriptions, 'I' is the value of the immediate field.
760 /// (field following the opcode).
761 ///
762 enum BLOCK_LAYOUT_OPCODE {
763 /// An operator which affects how the following layout should be
764 /// interpreted.
765 /// I == 0: Halt interpretation and treat everything else as
766 /// a non-pointer. Note that this instruction is equal
767 /// to '\0'.
768 /// I != 0: Currently unused.
769 BLOCK_LAYOUT_OPERATOR = 0,
770
771 /// The next I+1 bytes do not contain a value of object pointer type.
772 /// Note that this can leave the stream unaligned, meaning that
773 /// subsequent word-size instructions do not begin at a multiple of
774 /// the pointer size.
775 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
776
777 /// The next I+1 words do not contain a value of object pointer type.
778 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
779 /// when the required skip quantity is a multiple of the pointer size.
780 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
781
782 /// The next I+1 words are __strong pointers to Objective-C
783 /// objects or blocks.
784 BLOCK_LAYOUT_STRONG = 3,
785
786 /// The next I+1 words are pointers to __block variables.
787 BLOCK_LAYOUT_BYREF = 4,
788
789 /// The next I+1 words are __weak pointers to Objective-C
790 /// objects or blocks.
791 BLOCK_LAYOUT_WEAK = 5,
792
793 /// The next I+1 words are __unsafe_unretained pointers to
794 /// Objective-C objects or blocks.
795 BLOCK_LAYOUT_UNRETAINED = 6
796
797 /// The next I+1 words are block or object pointers with some
798 /// as-yet-unspecified ownership semantics. If we add more
799 /// flavors of ownership semantics, values will be taken from
800 /// this range.
801 ///
802 /// This is included so that older tools can at least continue
803 /// processing the layout past such things.
804 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
805
806 /// All other opcodes are reserved. Halt interpretation and
807 /// treat everything else as opaque.
808 };
809
810 class RUN_SKIP {
811 public:
812 enum BLOCK_LAYOUT_OPCODE opcode;
813 unsigned block_var_bytepos;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000814 unsigned block_var_size;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000815 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000816 unsigned BytePos = 0, unsigned Size = 0)
817 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000818
819 // Allow sorting based on byte pos.
820 bool operator<(const RUN_SKIP &b) const {
821 return block_var_bytepos < b.block_var_bytepos;
822 }
823 };
824
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000825protected:
Owen Anderson69243822009-07-13 04:10:07 +0000826 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000827 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000828 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000829
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000830 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000831 SmallVector<GC_IVAR, 16> SkipIvars;
832 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000833
834 // arc/mrr layout of captured block literal variables.
835 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000836
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000837 /// LazySymbols - Symbols to generate a lazy reference for. See
838 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000839 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000841 /// DefinedSymbols - External symbols which are defined by this
842 /// module. The symbols in this list and LazySymbols are used to add
843 /// special linker symbols which ensure that Objective-C modules are
844 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000845 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000847 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000848 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000850 /// MethodVarNames - uniqued method variable names.
851 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000853 /// DefinedCategoryNames - list of category names in form Class_Category.
854 llvm::SetVector<std::string> DefinedCategoryNames;
855
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000856 /// MethodVarTypes - uniqued method type signatures. We have to use
857 /// a StringMap here because have no other unique reference.
858 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000859
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000860 /// MethodDefinitions - map of methods which have been defined in
861 /// this translation unit.
862 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000863
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000864 /// PropertyNames - uniqued method variable names.
865 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000867 /// ClassReferences - uniqued class references.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000869
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000870 /// SelectorReferences - uniqued selector references.
871 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000873 /// Protocols - Protocols for which an objc_protocol structure has
874 /// been emitted. Forward declarations are handled by creating an
875 /// empty structure whose initializer is filled in when/if defined.
876 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000878 /// DefinedProtocols - Protocols which have actually been
879 /// defined. We should not need this, see FIXME in GenerateProtocol.
880 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000883 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000884
885 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000886 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000888 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000889 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000891 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000892 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000894 /// GetNameForMethod - Return a name for the given method.
895 /// \param[out] NameOut - The return value.
896 void GetNameForMethod(const ObjCMethodDecl *OMD,
897 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000898 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000899
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000900 /// GetMethodVarName - Return a unique constant for the given
901 /// selector's name. The return value has type char *.
902 llvm::Constant *GetMethodVarName(Selector Sel);
903 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000905 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000906 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000909 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
910 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000911 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000912
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000913 /// GetPropertyName - Return a unique constant for the given
914 /// name. The return value has type char *.
915 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000916
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000917 // FIXME: This can be dropped once string functions are unified.
918 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
919 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000921 /// GetClassName - Return a unique constant for the given selector's
922 /// name. The return value has type char *.
923 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000924
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000925 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
926
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000927 /// BuildIvarLayout - Builds ivar layout bitmap for the class
928 /// implementation for the __strong or __weak case.
929 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000930 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
931 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000932
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000933 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000934
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000935 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman38ad4f42012-11-06 01:51:20 +0000936 CharUnits BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000937 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000938 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000939 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000940 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000941 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman38ad4f42012-11-06 01:51:20 +0000942 CharUnits BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000943 bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000944
Fariborz Jahanianc441cd32012-11-04 18:19:40 +0000945 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT);
Fariborz Jahanian44fcff92012-11-02 22:51:18 +0000946
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000947 void UpdateRunSkipBlockVars(bool IsByref,
948 Qualifiers::ObjCLifetime LifeTime,
949 unsigned FieldOffset,
950 unsigned FieldSize);
951
952 void BuildRCBlockVarRecordLayout(const RecordType *RT,
953 unsigned int BytePos, bool &HasUnion);
954
955 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
956 const RecordDecl *RD,
957 ArrayRef<const FieldDecl*> RecFields,
958 unsigned int BytePos, bool &HasUnion);
959
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000960 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
961
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000962
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000963 /// GetIvarLayoutName - Returns a unique constant for the given
964 /// ivar layout bitmap.
965 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
966 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000967
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000968 /// EmitPropertyList - Emit the given property list. The return
969 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000970 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000971 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000972 const ObjCContainerDecl *OCD,
973 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000974
Bob Wilsondc8dab62011-11-30 01:57:58 +0000975 /// EmitProtocolMethodTypes - Generate the array of extended method type
976 /// strings. The return value has type Int8PtrPtrTy.
977 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000978 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000979 const ObjCCommonTypesHelper &ObjCTypes);
980
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000981 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000982 void PushProtocolProperties(
983 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
984 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
985 const Decl *Container,
986 const ObjCProtocolDecl *PROTO,
987 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000988
Fariborz Jahanianda320092009-01-29 19:24:30 +0000989 /// GetProtocolRef - Return a reference to the internal protocol
990 /// description, creating an empty one if it has not been
991 /// defined. The return value has type ProtocolPtrTy.
992 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000993
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000994 /// CreateMetadataVar - Create a global variable with internal
995 /// linkage for use by the Objective-C runtime.
996 ///
997 /// This is a convenience wrapper which not only creates the
998 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000999 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001000 ///
1001 /// \param Name - The variable name.
1002 /// \param Init - The variable initializer; this is also used to
1003 /// define the type of the variable.
1004 /// \param Section - The section the variable should go into, or 0.
1005 /// \param Align - The alignment for the variable, or 0.
1006 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +00001007 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +00001008 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001009 llvm::Constant *Init,
1010 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001011 unsigned Align,
1012 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001013
John McCall944c8432011-05-14 03:10:52 +00001014 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1015 ReturnValueSlot Return,
1016 QualType ResultType,
1017 llvm::Value *Sel,
1018 llvm::Value *Arg0,
1019 QualType Arg0Ty,
1020 bool IsSuper,
1021 const CallArgList &CallArgs,
1022 const ObjCMethodDecl *OMD,
1023 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001024
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001025 /// EmitImageInfo - Emit the image info marker used to encode some module
1026 /// level information.
1027 void EmitImageInfo();
1028
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001029public:
Owen Anderson69243822009-07-13 04:10:07 +00001030 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +00001031 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001032
David Chisnall0d13f6f2010-01-23 02:40:42 +00001033 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001034
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001035 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1036 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001037
Fariborz Jahanianda320092009-01-29 19:24:30 +00001038 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001039
Fariborz Jahanianda320092009-01-29 19:24:30 +00001040 /// GetOrEmitProtocol - Get the protocol object for the given
1041 /// declaration, emitting it if necessary. The return value has type
1042 /// ProtocolPtrTy.
1043 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001044
Fariborz Jahanianda320092009-01-29 19:24:30 +00001045 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1046 /// object for the given declaration, emitting it if needed. These
1047 /// forward references will be filled in with empty bodies if no
1048 /// definition is seen. The return value has type ProtocolPtrTy.
1049 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001050 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1051 const CGBlockInfo &blockInfo);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001052 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1053 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001054
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001055};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001056
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001057class CGObjCMac : public CGObjCCommonMac {
1058private:
1059 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001060
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001061 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001062 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001063 void EmitModuleInfo();
1064
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065 /// EmitModuleSymols - Emit module symbols, the list of defined
1066 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001067 llvm::Constant *EmitModuleSymbols();
1068
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001069 /// FinishModule - Write out global data structures at the end of
1070 /// processing a translation unit.
1071 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 /// EmitClassExtension - Generate the class extension structure used
1074 /// to store the weak ivar layout and properties. The return value
1075 /// has type ClassExtensionPtrTy.
1076 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1077
1078 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1079 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001080 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001081 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001082
John McCallf85e1932011-06-15 23:02:42 +00001083 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1084 IdentifierInfo *II);
1085
1086 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1087
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001088 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1089 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001090
1091 /// EmitIvarList - Emit the ivar list for the given
1092 /// implementation. If ForClass is true the list of class ivars
1093 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1094 /// interface ivars will be emitted. The return value has type
1095 /// IvarListPtrTy.
1096 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001097 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001098
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001099 /// EmitMetaClass - Emit a forward reference to the class structure
1100 /// for the metaclass of the given interface. The return value has
1101 /// type ClassPtrTy.
1102 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1103
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001104 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001105 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001106 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1107 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001108 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001109
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001110 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001111
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001112 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113
1114 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001115 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001116 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001117 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001118 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001119
1120 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001121 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001122 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001123 /// - IsProtocol: True iff these methods are for a protocol.
1124 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001125 /// - Required: When true, only "required" methods are
1126 /// listed. Similarly, when false only "optional" methods are
1127 /// listed. For classes this should always be true.
1128 /// - begin, end: The method list to output.
1129 ///
1130 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001131 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001132 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001133 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001135 /// GetOrEmitProtocol - Get the protocol object for the given
1136 /// declaration, emitting it if necessary. The return value has type
1137 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001138 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001139
1140 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1141 /// object for the given declaration, emitting it if needed. These
1142 /// forward references will be filled in with empty bodies if no
1143 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001144 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001145
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146 /// EmitProtocolExtension - Generate the protocol extension
1147 /// structure used to store optional instance and class methods, and
1148 /// protocol properties. The return value has type
1149 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001150 llvm::Constant *
1151 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001152 ArrayRef<llvm::Constant*> OptInstanceMethods,
1153 ArrayRef<llvm::Constant*> OptClassMethods,
1154 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001155
1156 /// EmitProtocolList - Generate the list of referenced
1157 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001159 ObjCProtocolDecl::protocol_iterator begin,
1160 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001161
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001162 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1163 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001164 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1165 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001166
1167public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001168 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001169
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001170 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001171
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001172 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001173 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001174 QualType ResultType,
1175 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001176 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001177 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001178 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001179 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001180
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001182 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001183 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001184 QualType ResultType,
1185 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001186 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001187 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001188 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001189 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001190 const CallArgList &CallArgs,
1191 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001192
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001193 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001194 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001195
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001196 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1197 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001198
1199 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1200 /// untyped one.
1201 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1202 const ObjCMethodDecl *Method);
1203
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001204 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001205
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001206 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001207
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001208 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001209
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001210 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001211
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001212 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001213 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214
Chris Lattner74391b42009-03-22 21:03:39 +00001215 virtual llvm::Constant *GetPropertyGetFunction();
1216 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001217 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1218 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001219 virtual llvm::Constant *GetGetStructFunction();
1220 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001221 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001222 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001223
John McCallf1549f62010-07-06 01:34:17 +00001224 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1225 const ObjCAtTryStmt &S);
1226 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1227 const ObjCAtSynchronizedStmt &S);
1228 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001229 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1230 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001231 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001232 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001233 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001235 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001236 llvm::Value *src, llvm::Value *dest,
1237 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001238 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001239 llvm::Value *src, llvm::Value *dest,
1240 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001241 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1242 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001243 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1244 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001245 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001247 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1248 QualType ObjectTy,
1249 llvm::Value *BaseValue,
1250 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001251 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001252 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001253 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001254 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001255
1256 /// GetClassGlobal - Return the global variable for the Objective-C
1257 /// class of the given name.
1258 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001259 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001260 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001261};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001262
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001263class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001264private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001265 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001266 llvm::GlobalVariable* ObjCEmptyCacheVar;
1267 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Daniel Dunbar11394522009-04-18 08:51:00 +00001269 /// SuperClassReferences - uniqued super class references.
1270 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001271
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001272 /// MetaClassReferences - uniqued meta class references.
1273 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001274
1275 /// EHTypeReferences - uniqued class ehtype references.
1276 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
John McCall944c8432011-05-14 03:10:52 +00001278 /// VTableDispatchMethods - List of methods for which we generate
1279 /// vtable-based message dispatch.
1280 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001282 /// DefinedMetaClasses - List of defined meta-classes.
1283 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1284
John McCall944c8432011-05-14 03:10:52 +00001285 /// isVTableDispatchedSelector - Returns true if SEL is a
1286 /// vtable-based selector.
1287 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001288
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001289 /// FinishNonFragileABIModule - Write out global data structures at the end of
1290 /// processing a translation unit.
1291 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001292
Daniel Dunbar463b8762009-05-15 21:48:48 +00001293 /// AddModuleClassList - Add the given list of class pointers to the
1294 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001295 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001296 const char *SymbolName,
1297 const char *SectionName);
1298
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1300 unsigned InstanceStart,
1301 unsigned InstanceSize,
1302 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001303 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001304 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001305 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001306 llvm::Constant *ClassRoGV,
1307 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001309 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001311 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001313 /// EmitMethodList - Emit the method list for the given
1314 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001315 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001316 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001317 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001318 /// EmitIvarList - Emit the ivar list for the given
1319 /// implementation. If ForClass is true the list of class ivars
1320 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1321 /// interface ivars will be emitted. The return value has type
1322 /// IvarListnfABIPtrTy.
1323 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001325 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001326 const ObjCIvarDecl *Ivar,
Eli Friedman38ad4f42012-11-06 01:51:20 +00001327 CharUnits offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001328
Fariborz Jahanianda320092009-01-29 19:24:30 +00001329 /// GetOrEmitProtocol - Get the protocol object for the given
1330 /// declaration, emitting it if necessary. The return value has type
1331 /// ProtocolPtrTy.
1332 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001333
Fariborz Jahanianda320092009-01-29 19:24:30 +00001334 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1335 /// object for the given declaration, emitting it if needed. These
1336 /// forward references will be filled in with empty bodies if no
1337 /// definition is seen. The return value has type ProtocolPtrTy.
1338 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001339
Fariborz Jahanianda320092009-01-29 19:24:30 +00001340 /// EmitProtocolList - Generate the list of referenced
1341 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001342 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001343 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001344 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001345
John McCall944c8432011-05-14 03:10:52 +00001346 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1347 ReturnValueSlot Return,
1348 QualType ResultType,
1349 Selector Sel,
1350 llvm::Value *Receiver,
1351 QualType Arg0Ty,
1352 bool IsSuper,
1353 const CallArgList &CallArgs,
1354 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001355
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001356 /// GetClassGlobal - Return the global variable for the Objective-C
1357 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001358 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001359
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001360 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001361 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001362 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001363 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001364
1365 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1366 IdentifierInfo *II);
1367
1368 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001369
Daniel Dunbar11394522009-04-18 08:51:00 +00001370 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1371 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001372 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1373 const ObjCInterfaceDecl *ID);
1374
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001375 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1376 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001377 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001378 const ObjCInterfaceDecl *ID);
1379
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001380 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1381 /// the given ivar.
1382 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001383 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001384 const ObjCInterfaceDecl *ID,
1385 const ObjCIvarDecl *Ivar);
1386
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001387 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1388 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001389 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1390 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001391
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001392 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001393 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001394 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001395 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001396
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001398 return "OBJC_METACLASS_$_";
1399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001400
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001401 const char *getClassSymbolPrefix() const {
1402 return "OBJC_CLASS_$_";
1403 }
1404
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001405 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001406 uint32_t &InstanceStart,
1407 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001409 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001410 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001411 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1412 return CGM.getContext().Selectors.getSelector(0, &II);
1413 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001414
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001415 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001416 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1417 return CGM.getContext().Selectors.getSelector(1, &II);
1418 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001419
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001420 /// ImplementationIsNonLazy - Check whether the given category or
1421 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001422 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001423
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001424public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001425 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426 // FIXME. All stubs for now!
1427 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001428
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001429 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001430 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001431 QualType ResultType,
1432 Selector Sel,
1433 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001434 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001435 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001436 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001437
1438 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001439 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001440 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001441 QualType ResultType,
1442 Selector Sel,
1443 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001444 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001445 llvm::Value *Receiver,
1446 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001447 const CallArgList &CallArgs,
1448 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001450 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001451 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001452
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001453 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1454 bool lvalue = false)
1455 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001456
1457 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1458 /// untyped one.
1459 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1460 const ObjCMethodDecl *Method)
1461 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001462
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001463 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001464
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001465 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001466
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001467 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001468
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001469 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001470 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001471
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001472 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001473
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001474 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001475 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001476 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477 virtual llvm::Constant *GetPropertySetFunction() {
1478 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001479 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001480
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001481 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1482 bool copy) {
1483 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1484 }
1485
David Chisnall8fac25d2010-12-26 22:13:16 +00001486 virtual llvm::Constant *GetSetStructFunction() {
1487 return ObjCTypes.getCopyStructFn();
1488 }
1489 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001490 return ObjCTypes.getCopyStructFn();
1491 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001492 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1493 return ObjCTypes.getCppAtomicObjectFunction();
1494 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001495
Chris Lattner74391b42009-03-22 21:03:39 +00001496 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001497 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001498 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001499
John McCallf1549f62010-07-06 01:34:17 +00001500 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1501 const ObjCAtTryStmt &S);
1502 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1503 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001504 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001505 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001506 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001507 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001508 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001509 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001510 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001511 llvm::Value *src, llvm::Value *dest,
1512 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001513 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001514 llvm::Value *src, llvm::Value *dest,
1515 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001516 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001517 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001518 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1519 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001520 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001521 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1522 QualType ObjectTy,
1523 llvm::Value *BaseValue,
1524 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001525 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001526 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001527 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001528 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001529};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001530
1531/// A helper class for performing the null-initialization of a return
1532/// value.
1533struct NullReturnState {
1534 llvm::BasicBlock *NullBB;
1535 llvm::BasicBlock *callBB;
1536 NullReturnState() : NullBB(0), callBB(0) {}
1537
1538 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1539 // Make blocks for the null-init and call edges.
1540 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1541 callBB = CGF.createBasicBlock("msgSend.call");
1542
1543 // Check for a null receiver and, if there is one, jump to the
1544 // null-init test.
1545 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1546 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1547
1548 // Otherwise, start performing the call.
1549 CGF.EmitBlock(callBB);
1550 }
1551
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001552 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1553 const CallArgList &CallArgs,
1554 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001555 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001556
1557 llvm::Value *NullInitPtr = 0;
1558 if (result.isScalar() && !resultType->isVoidType()) {
1559 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1560 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1561 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001562
1563 // Finish the call path.
1564 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1565 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1566
1567 // Emit the null-init block and perform the null-initialization there.
1568 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001569
1570 // Release consumed arguments along the null-receiver path.
1571 if (Method) {
1572 CallArgList::const_iterator I = CallArgs.begin();
1573 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1574 e = Method->param_end(); i != e; ++i, ++I) {
1575 const ParmVarDecl *ParamDecl = (*i);
1576 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1577 RValue RV = I->RV;
1578 assert(RV.isScalar() &&
1579 "NullReturnState::complete - arg not on object");
1580 CGF.EmitARCRelease(RV.getScalarVal(), true);
1581 }
1582 }
1583 }
1584
1585 if (result.isScalar()) {
1586 if (NullInitPtr)
1587 CGF.EmitNullInitialization(NullInitPtr, resultType);
1588 // Jump to the continuation block.
1589 CGF.EmitBlock(contBB);
1590 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1591 : result;
1592 }
1593
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001594 if (!resultType->isAnyComplexType()) {
1595 assert(result.isAggregate() && "null init of non-aggregate result?");
1596 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1597 // Jump to the continuation block.
1598 CGF.EmitBlock(contBB);
1599 return result;
1600 }
1601
1602 // _Complex type
1603 // FIXME. Now easy to handle any other scalar type whose result is returned
1604 // in memory due to ABI limitations.
1605 CGF.EmitBlock(contBB);
1606 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1607 llvm::Type *MemberType = CallCV.first->getType();
1608 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1609 // Create phi instruction for scalar complex value.
1610 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1611 PHIReal->addIncoming(ZeroCV, NullBB);
1612 PHIReal->addIncoming(CallCV.first, callBB);
1613 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1614 PHIImag->addIncoming(ZeroCV, NullBB);
1615 PHIImag->addIncoming(CallCV.second, callBB);
1616 return RValue::getComplex(PHIReal, PHIImag);
1617 }
1618};
1619
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001620} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001621
1622/* *** Helper Functions *** */
1623
1624/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001625static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001626 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001627 unsigned idx0,
1628 unsigned idx1) {
1629 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001630 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1631 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001632 };
Jay Foada5c04342011-07-21 14:31:17 +00001633 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001634}
1635
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001636/// hasObjCExceptionAttribute - Return true if this class or any super
1637/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001638static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001639 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001640 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001641 return true;
1642 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001643 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001644 return false;
1645}
1646
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001647/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001648
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001649CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001650 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001651 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001652 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001653}
1654
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001655/// GetClass - Return a reference to the class for the given interface
1656/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001657llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001658 const ObjCInterfaceDecl *ID) {
1659 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001660}
1661
1662/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001663llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1664 bool lval) {
1665 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001666}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001667llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001668 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001669 return EmitSelector(Builder, Method->getSelector());
1670}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001671
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001672llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001673 if (T->isObjCIdType() ||
1674 T->isObjCQualifiedIdType()) {
1675 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001676 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001677 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001678 if (T->isObjCClassType() ||
1679 T->isObjCQualifiedClassType()) {
1680 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001681 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001682 }
1683 if (T->isObjCObjectPointerType())
1684 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1685
John McCall5a180392010-07-24 00:37:23 +00001686 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001687}
1688
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001689/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001690/*
1691 struct __builtin_CFString {
1692 const int *isa; // point to __CFConstantStringClassReference
1693 int flags;
1694 const char *str;
1695 long length;
1696 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001697*/
1698
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001699/// or Generate a constant NSString object.
1700/*
1701 struct __builtin_NSString {
1702 const int *isa; // point to __NSConstantStringClassReference
1703 const char *str;
1704 unsigned int length;
1705 };
1706*/
1707
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001708llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001709 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001710 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001711 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001712 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001713}
1714
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001715enum {
1716 kCFTaggedObjectID_Integer = (1 << 1) + 1
1717};
1718
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001719/// Generates a message send where the super is the receiver. This is
1720/// a message send to self with special delivery semantics indicating
1721/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001722CodeGen::RValue
1723CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001724 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001725 QualType ResultType,
1726 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001727 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001728 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001729 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001730 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001731 const CodeGen::CallArgList &CallArgs,
1732 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001733 // Create and init a super structure; this is a (receiver, class)
1734 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001735 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001736 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001737 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001738 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001740 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001741
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001742 // If this is a class message the metaclass is passed as the target.
1743 llvm::Value *Target;
1744 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001745 if (isCategoryImpl) {
1746 // Message sent to 'super' in a class method defined in a category
1747 // implementation requires an odd treatment.
1748 // If we are in a class method, we must retrieve the
1749 // _metaclass_ for the current class, pointed at by
1750 // the class's "isa" pointer. The following assumes that
1751 // isa" is the first ivar in a class (which it must be).
1752 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1753 Target = CGF.Builder.CreateStructGEP(Target, 0);
1754 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001755 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001756 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1757 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1758 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1759 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001760 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001761 }
1762 else if (isCategoryImpl)
1763 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1764 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001765 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1766 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1767 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001768 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001769 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1770 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001771 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001772 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001773 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001774 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001775 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001776 return EmitMessageSend(CGF, Return, ResultType,
1777 EmitSelector(CGF.Builder, Sel),
1778 ObjCSuper, ObjCTypes.SuperPtrCTy,
1779 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001780}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781
1782/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001783CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001784 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001785 QualType ResultType,
1786 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001787 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001788 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001789 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001790 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001791 return EmitMessageSend(CGF, Return, ResultType,
1792 EmitSelector(CGF.Builder, Sel),
1793 Receiver, CGF.getContext().getObjCIdType(),
1794 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001795}
1796
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001797CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001798CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1799 ReturnValueSlot Return,
1800 QualType ResultType,
1801 llvm::Value *Sel,
1802 llvm::Value *Arg0,
1803 QualType Arg0Ty,
1804 bool IsSuper,
1805 const CallArgList &CallArgs,
1806 const ObjCMethodDecl *Method,
1807 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001808 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001809 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001810 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001811 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1812 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001813 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814
John McCallde5d3c72012-02-17 03:33:10 +00001815 // If we're calling a method, use the formal signature.
1816 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001817
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001818 if (Method)
1819 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1820 CGM.getContext().getCanonicalType(ResultType) &&
1821 "Result type mismatch!");
1822
John McCallcba681a2011-05-14 21:12:11 +00001823 NullReturnState nullReturn;
1824
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001825 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001826 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001827 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001828 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001829 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001830 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1831 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1832 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001833 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1834 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1835 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001836 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001837 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001839 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001840
1841 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001842 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001843 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1844 e = Method->param_end(); i != e; ++i) {
1845 const ParmVarDecl *ParamDecl = (*i);
1846 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1847 if (!nullReturn.NullBB)
1848 nullReturn.init(CGF, Arg0);
1849 requiresnullCheck = true;
1850 break;
1851 }
1852 }
1853
John McCallde5d3c72012-02-17 03:33:10 +00001854 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1855 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001856 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1857 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001858}
1859
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001860static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1861 if (FQT.isObjCGCStrong())
1862 return Qualifiers::Strong;
1863
John McCallf85e1932011-06-15 23:02:42 +00001864 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001865 return Qualifiers::Weak;
1866
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001867 // check for __unsafe_unretained
1868 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1869 return Qualifiers::GCNone;
1870
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001871 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1872 return Qualifiers::Strong;
1873
1874 if (const PointerType *PT = FQT->getAs<PointerType>())
1875 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1876
1877 return Qualifiers::GCNone;
1878}
1879
John McCall6b5a61b2011-02-07 10:33:21 +00001880llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1881 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001882
Chris Lattner8b418682012-02-07 00:39:47 +00001883 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikie4e4d0842012-03-11 07:00:24 +00001884 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1885 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001886 return nullPtr;
1887
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001888 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001889 SkipIvars.clear();
1890 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001891 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1892 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001893
Fariborz Jahanian81979822010-09-09 00:21:45 +00001894 // __isa is the first field in block descriptor and must assume by runtime's
1895 // convention that it is GC'able.
Eli Friedman38ad4f42012-11-06 01:51:20 +00001896 IvarsInfo.push_back(GC_IVAR(CharUnits::Zero(), 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001897
1898 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1899
1900 // Calculate the basic layout of the block structure.
1901 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001902 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001903
1904 // Ignore the optional 'this' capture: C++ objects are not assumed
1905 // to be GC'ed.
1906
1907 // Walk the captured variables.
1908 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1909 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1910 const VarDecl *variable = ci->getVariable();
1911 QualType type = variable->getType();
1912
1913 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1914
1915 // Ignore constant captures.
1916 if (capture.isConstant()) continue;
1917
Eli Friedman38ad4f42012-11-06 01:51:20 +00001918 CharUnits fieldOffset =
1919 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
John McCall6b5a61b2011-02-07 10:33:21 +00001920
1921 // __block variables are passed by their descriptor address.
1922 if (ci->isByRef()) {
1923 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001924 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001925 }
1926
1927 assert(!type->isArrayType() && "array variable should not be caught");
1928 if (const RecordType *record = type->getAs<RecordType>()) {
1929 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001930 continue;
1931 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001932
John McCall6b5a61b2011-02-07 10:33:21 +00001933 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1934 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1935
1936 if (GCAttr == Qualifiers::Strong)
1937 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1938 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001939 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001940 SkipIvars.push_back(GC_IVAR(fieldOffset,
1941 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001942 }
1943
1944 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001945 return nullPtr;
1946
1947 // Sort on byte position; captures might not be allocated in order,
1948 // and unions can do funny things.
1949 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1950 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001951
1952 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001953 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001954 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001955 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001956 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001957 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001958 if (!(s[i] & 0xf0))
1959 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1960 else
1961 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1962 printf("\n");
1963 }
1964
1965 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001966}
1967
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001968/// getBlockCaptureLifetime - This routine returns life time of the captured
1969/// block variable for the purpose of block layout meta-data generation. FQT is
1970/// the type of the variable captured in the block.
1971Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT) {
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001972 if (CGM.getLangOpts().ObjCAutoRefCount)
1973 return FQT.getObjCLifetime();
1974
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001975 // MRR.
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001976 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001977 return Qualifiers::OCL_ExplicitNone;
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001978
1979 return Qualifiers::OCL_None;
1980}
1981
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001982void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1983 Qualifiers::ObjCLifetime LifeTime,
1984 unsigned FieldOffset,
1985 unsigned FieldSize) {
1986 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
1987 unsigned FieldSizeInBytes = FieldSize/ByteSizeInBits;
1988
1989 // __block variables are passed by their descriptor address.
1990 if (IsByref)
1991 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
1992 FieldSizeInBytes));
1993 else if (LifeTime == Qualifiers::OCL_Strong)
1994 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
1995 FieldSizeInBytes));
1996 else if (LifeTime == Qualifiers::OCL_Weak)
1997 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
1998 FieldSizeInBytes));
1999 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2000 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
2001 FieldSizeInBytes));
2002 else
2003 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2004 FieldOffset,
2005 FieldSizeInBytes));
2006}
2007
2008void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2009 const RecordDecl *RD,
2010 ArrayRef<const FieldDecl*> RecFields,
2011 unsigned int BytePos, bool &HasUnion) {
2012 bool IsUnion = (RD && RD->isUnion());
2013 uint64_t MaxUnionSize = 0;
2014 const FieldDecl *MaxField = 0;
2015 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
2016 uint64_t MaxFieldOffset = 0;
2017 uint64_t LastBitfieldOrUnnamedOffset = 0;
2018
2019 if (RecFields.empty())
2020 return;
2021 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2022
2023 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2024 const FieldDecl *Field = RecFields[i];
2025 uint64_t FieldOffset;
2026 // Note that 'i' here is actually the field index inside RD of Field,
2027 // although this dependency is hidden.
2028 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2029 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits);
2030
2031 // Skip over unnamed or bitfields
2032 if (!Field->getIdentifier() || Field->isBitField()) {
2033 LastFieldBitfieldOrUnnamed = Field;
2034 LastBitfieldOrUnnamedOffset = FieldOffset;
2035 continue;
2036 }
2037
2038 LastFieldBitfieldOrUnnamed = 0;
2039 QualType FQT = Field->getType();
2040 if (FQT->isRecordType() || FQT->isUnionType()) {
2041 if (FQT->isUnionType())
2042 HasUnion = true;
2043
2044 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2045 BytePos + FieldOffset, HasUnion);
2046 continue;
2047 }
2048
2049 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2050 const ConstantArrayType *CArray =
2051 dyn_cast_or_null<ConstantArrayType>(Array);
2052 uint64_t ElCount = CArray->getSize().getZExtValue();
2053 assert(CArray && "only array with known element size is supported");
2054 FQT = CArray->getElementType();
2055 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2056 const ConstantArrayType *CArray =
2057 dyn_cast_or_null<ConstantArrayType>(Array);
2058 ElCount *= CArray->getSize().getZExtValue();
2059 FQT = CArray->getElementType();
2060 }
2061
2062 assert(!FQT->isUnionType() &&
2063 "layout for array of unions not supported");
2064 if (FQT->isRecordType() && ElCount) {
2065 int OldIndex = RunSkipBlockVars.size() - 1;
2066 const RecordType *RT = FQT->getAs<RecordType>();
2067 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2068 HasUnion);
2069
2070 // Replicate layout information for each array element. Note that
2071 // one element is already done.
2072 uint64_t ElIx = 1;
2073 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
2074 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
2075 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2076 RunSkipBlockVars.push_back(
2077 RUN_SKIP(RunSkipBlockVars[i].opcode,
2078 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2079 RunSkipBlockVars[i].block_var_size));
2080 }
2081 continue;
2082 }
2083 }
2084 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
2085 if (IsUnion) {
2086 uint64_t UnionIvarSize = FieldSize;
2087 if (UnionIvarSize > MaxUnionSize) {
2088 MaxUnionSize = UnionIvarSize;
2089 MaxField = Field;
2090 MaxFieldOffset = FieldOffset;
2091 }
2092 } else {
2093 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002094 getBlockCaptureLifetime(FQT),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002095 BytePos + FieldOffset,
2096 FieldSize);
2097 }
2098 }
2099
2100 if (LastFieldBitfieldOrUnnamed) {
2101 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2102 // Last field was a bitfield. Must update the info.
2103 uint64_t BitFieldSize
2104 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
2105 unsigned Size = (BitFieldSize / ByteSizeInBits) +
2106 ((BitFieldSize % ByteSizeInBits) != 0);
2107 Size += LastBitfieldOrUnnamedOffset;
2108 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002109 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002110 BytePos + LastBitfieldOrUnnamedOffset,
2111 Size*ByteSizeInBits);
2112 } else {
2113 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2114 // Last field was unnamed. Must update skip info.
2115 unsigned FieldSize
2116 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
2117 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002118 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002119 BytePos + LastBitfieldOrUnnamedOffset,
2120 FieldSize);
2121 }
2122 }
2123
2124 if (MaxField)
2125 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002126 getBlockCaptureLifetime(MaxField->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002127 BytePos + MaxFieldOffset,
2128 MaxUnionSize);
2129}
2130
2131void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
2132 unsigned int BytePos,
2133 bool &HasUnion) {
2134 const RecordDecl *RD = RT->getDecl();
2135 SmallVector<const FieldDecl*, 16> Fields;
2136 for (RecordDecl::field_iterator i = RD->field_begin(),
2137 e = RD->field_end(); i != e; ++i)
2138 Fields.push_back(*i);
2139 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2140 const llvm::StructLayout *RecLayout =
2141 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2142
2143 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion);
2144}
2145
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002146/// InlineLayoutInstruction - This routine produce an inline instruction for the
2147/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2148/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2149/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2150/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2151/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2152/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2153/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2154/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2155uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2156 SmallVectorImpl<unsigned char> &Layout) {
2157 uint64_t Result = 0;
2158 if (Layout.size() <= 3) {
2159 unsigned size = Layout.size();
2160 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2161 unsigned char inst;
2162 enum BLOCK_LAYOUT_OPCODE opcode ;
2163 switch (size) {
2164 case 3:
2165 inst = Layout[0];
2166 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2167 if (opcode == BLOCK_LAYOUT_STRONG)
2168 strong_word_count = (inst & 0xF)+1;
2169 else
2170 return 0;
2171 inst = Layout[1];
2172 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2173 if (opcode == BLOCK_LAYOUT_BYREF)
2174 byref_word_count = (inst & 0xF)+1;
2175 else
2176 return 0;
2177 inst = Layout[2];
2178 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2179 if (opcode == BLOCK_LAYOUT_WEAK)
2180 weak_word_count = (inst & 0xF)+1;
2181 else
2182 return 0;
2183 break;
2184
2185 case 2:
2186 inst = Layout[0];
2187 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2188 if (opcode == BLOCK_LAYOUT_STRONG) {
2189 strong_word_count = (inst & 0xF)+1;
2190 inst = Layout[1];
2191 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2192 if (opcode == BLOCK_LAYOUT_BYREF)
2193 byref_word_count = (inst & 0xF)+1;
2194 else if (opcode == BLOCK_LAYOUT_WEAK)
2195 weak_word_count = (inst & 0xF)+1;
2196 else
2197 return 0;
2198 }
2199 else if (opcode == BLOCK_LAYOUT_BYREF) {
2200 byref_word_count = (inst & 0xF)+1;
2201 inst = Layout[1];
2202 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2203 if (opcode == BLOCK_LAYOUT_WEAK)
2204 weak_word_count = (inst & 0xF)+1;
2205 else
2206 return 0;
2207 }
2208 else
2209 return 0;
2210 break;
2211
2212 case 1:
2213 inst = Layout[0];
2214 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2215 if (opcode == BLOCK_LAYOUT_STRONG)
2216 strong_word_count = (inst & 0xF)+1;
2217 else if (opcode == BLOCK_LAYOUT_BYREF)
2218 byref_word_count = (inst & 0xF)+1;
2219 else if (opcode == BLOCK_LAYOUT_WEAK)
2220 weak_word_count = (inst & 0xF)+1;
2221 else
2222 return 0;
2223 break;
2224
2225 default:
2226 return 0;
2227 }
2228
2229 // Cannot inline when any of the word counts is 15. Because this is one less
2230 // than the actual work count (so 15 means 16 actual word counts),
2231 // and we can only display 0 thru 15 word counts.
2232 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2233 return 0;
2234
2235 unsigned count =
2236 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2237
2238 if (size == count) {
2239 if (strong_word_count)
2240 Result = strong_word_count;
2241 Result <<= 4;
2242 if (byref_word_count)
2243 Result += byref_word_count;
2244 Result <<= 4;
2245 if (weak_word_count)
2246 Result += weak_word_count;
2247 }
2248 }
2249 return Result;
2250}
2251
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002252llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2253 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002254 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2255
2256 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2257
2258 RunSkipBlockVars.clear();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002259 bool hasUnion = false;
2260
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002261 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2262 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2263 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2264
2265 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2266
2267 // Calculate the basic layout of the block structure.
2268 const llvm::StructLayout *layout =
2269 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2270
2271 // Ignore the optional 'this' capture: C++ objects are not assumed
2272 // to be GC'ed.
2273
2274 // Walk the captured variables.
2275 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2276 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2277 const VarDecl *variable = ci->getVariable();
2278 QualType type = variable->getType();
2279
2280 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2281
2282 // Ignore constant captures.
2283 if (capture.isConstant()) continue;
2284
2285 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
2286
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002287 assert(!type->isArrayType() && "array variable should not be caught");
2288 if (const RecordType *record = type->getAs<RecordType>()) {
2289 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002290 continue;
2291 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002292 unsigned fieldSize = ci->isByRef() ? WordSizeInBits
2293 : CGM.getContext().getTypeSize(type);
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002294 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002295 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002296 }
2297
2298 if (RunSkipBlockVars.empty())
2299 return nullPtr;
2300
2301 // Sort on byte position; captures might not be allocated in order,
2302 // and unions can do funny things.
2303 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002304 SmallVector<unsigned char, 16> Layout;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002305
2306 unsigned size = RunSkipBlockVars.size();
2307 unsigned int shift = (WordSizeInBytes == 8) ? 3 : 2;
2308 unsigned int mask = (WordSizeInBytes == 8) ? 0x7 : 0x3;
2309 for (unsigned i = 0; i < size; i++) {
2310 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2311 unsigned start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2312 unsigned end_byte_pos = start_byte_pos;
2313 unsigned j = i+1;
2314 while (j < size) {
2315 if (opcode == RunSkipBlockVars[j].opcode) {
2316 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2317 i++;
2318 }
2319 else
2320 break;
2321 }
2322 unsigned size_in_bytes =
2323 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002324 if (j < size) {
2325 unsigned gap =
2326 RunSkipBlockVars[j].block_var_bytepos -
2327 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2328 size_in_bytes += gap;
2329 }
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002330 unsigned residue_in_bytes = 0;
2331 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2332 residue_in_bytes = size_in_bytes & mask;
2333 size_in_bytes -= residue_in_bytes;
2334 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2335 }
2336
2337 unsigned size_in_words = size_in_bytes >> shift;
2338 while (size_in_words >= 16) {
2339 // Note that value in imm. is one less that the actual
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002340 // value. So, 0xf means 16 words follow!
2341 unsigned char inst = (opcode << 4) | 0xf;
2342 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002343 size_in_words -= 16;
2344 }
2345 if (size_in_words > 0) {
2346 // Note that value in imm. is one less that the actual
2347 // value. So, we subtract 1 away!
2348 unsigned char inst = (opcode << 4) | (size_in_words-1);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002349 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002350 }
2351 if (residue_in_bytes > 0) {
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002352 unsigned char inst =
2353 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes-1);
2354 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002355 }
2356 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002357
2358 int e = Layout.size()-1;
2359 while (e >= 0) {
2360 unsigned char inst = Layout[e--];
2361 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2362 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2363 Layout.pop_back();
2364 else
2365 break;
2366 }
2367
2368 uint64_t Result = InlineLayoutInstruction(Layout);
2369 if (Result != 0) {
2370 // Block variable layout instruction has been inlined.
2371 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2372 printf("\n Inline instruction for block variable layout: ");
Matt Beaumont-Gay00a85412012-11-01 20:26:42 +00002373 printf("0x0%llx\n", (unsigned long long)Result);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002374 }
2375 if (WordSizeInBytes == 8) {
2376 const llvm::APInt Instruction(64, Result);
2377 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2378 }
2379 else {
2380 const llvm::APInt Instruction(32, Result);
2381 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2382 }
2383 }
2384
2385 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2386 Layout.push_back(inst);
2387 std::string BitMap;
2388 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2389 BitMap += Layout[i];
2390
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002391 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2392 printf("\n block variable layout: ");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002393 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2394 unsigned char inst = BitMap[i];
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002395 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2396 unsigned delta = 1;
2397 switch (opcode) {
2398 case BLOCK_LAYOUT_OPERATOR:
2399 printf("BL_OPERATOR:");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002400 delta = 0;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002401 break;
2402 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2403 printf("BL_NON_OBJECT_BYTES:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002404 break;
2405 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2406 printf("BL_NON_OBJECT_WORD:");
2407 break;
2408 case BLOCK_LAYOUT_STRONG:
2409 printf("BL_STRONG:");
2410 break;
2411 case BLOCK_LAYOUT_BYREF:
2412 printf("BL_BYREF:");
2413 break;
2414 case BLOCK_LAYOUT_WEAK:
2415 printf("BL_WEAK:");
2416 break;
2417 case BLOCK_LAYOUT_UNRETAINED:
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002418 printf("BL_UNRETAINED:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002419 break;
2420 }
2421 // Actual value of word count is one more that what is in the imm.
2422 // field of the instruction
2423 printf("%d", (inst & 0xf) + delta);
2424 if (i < e-1)
2425 printf(", ");
2426 else
2427 printf("\n");
2428 }
2429 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002430
2431 llvm::GlobalVariable * Entry =
2432 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2433 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2434 "__TEXT,__objc_classname,cstring_literals", 1, true);
2435 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002436}
2437
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002438llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002439 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002440 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002441 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002442 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2443
Owen Anderson3c4972d2009-07-29 18:54:39 +00002444 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002445 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002446}
2447
Fariborz Jahanianda320092009-01-29 19:24:30 +00002448void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002449 // FIXME: We shouldn't need this, the protocol decl should contain enough
2450 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002451 DefinedProtocols.insert(PD->getIdentifier());
2452
2453 // If we have generated a forward reference to this protocol, emit
2454 // it now. Otherwise do nothing, the protocol objects are lazily
2455 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002456 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002457 GetOrEmitProtocol(PD);
2458}
2459
Fariborz Jahanianda320092009-01-29 19:24:30 +00002460llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002461 if (DefinedProtocols.count(PD->getIdentifier()))
2462 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002463
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002464 return GetOrEmitProtocolRef(PD);
2465}
2466
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002467/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002468// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2469struct _objc_protocol {
2470struct _objc_protocol_extension *isa;
2471char *protocol_name;
2472struct _objc_protocol_list *protocol_list;
2473struct _objc__method_prototype_list *instance_methods;
2474struct _objc__method_prototype_list *class_methods
2475};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002476
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002478*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002479llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002480 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002481
2482 // Early exit if a defining object has already been generated.
2483 if (Entry && Entry->hasInitializer())
2484 return Entry;
2485
Douglas Gregor1d784b22012-01-01 19:51:50 +00002486 // Use the protocol definition, if there is one.
2487 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2488 PD = Def;
2489
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002490 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002491 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002492 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2493
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002494 // Construct method lists.
2495 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2496 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002497 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002498 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002499 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002500 ObjCMethodDecl *MD = *i;
2501 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002502 if (!C)
2503 return GetOrEmitProtocolRef(PD);
2504
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002505 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2506 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002507 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002508 } else {
2509 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002510 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002511 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002512 }
2513
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002514 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002515 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002516 ObjCMethodDecl *MD = *i;
2517 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002518 if (!C)
2519 return GetOrEmitProtocolRef(PD);
2520
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002521 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2522 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002523 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002524 } else {
2525 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002526 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002527 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002528 }
2529
Bob Wilsondc8dab62011-11-30 01:57:58 +00002530 MethodTypesExt.insert(MethodTypesExt.end(),
2531 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2532
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002533 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002534 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2535 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002536 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002537 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002538 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002539 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002540 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002541 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002542 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002543 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002544 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002545 ClassMethods)
2546 };
Owen Anderson08e25242009-07-27 22:29:56 +00002547 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002548 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002549
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002550 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002551 // Already created, fix the linkage and update the initializer.
2552 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002553 Entry->setInitializer(Init);
2554 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002555 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002556 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002557 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002558 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002559 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002560 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002561 // FIXME: Is this necessary? Why only for protocol?
2562 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002563
2564 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002565 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002566 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002567
2568 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002569}
2570
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002571llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002572 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2573
2574 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002575 // We use the initializer as a marker of whether this is a forward
2576 // reference or not. At module finalization we add the empty
2577 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002578 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002579 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002580 llvm::GlobalValue::ExternalLinkage,
2581 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002582 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002583 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002584 // FIXME: Is this necessary? Why only for protocol?
2585 Entry->setAlignment(4);
2586 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002587
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002588 return Entry;
2589}
2590
2591/*
2592 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002593 uint32_t size;
2594 struct objc_method_description_list *optional_instance_methods;
2595 struct objc_method_description_list *optional_class_methods;
2596 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002597 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002598 };
2599*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002600llvm::Constant *
2601CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002602 ArrayRef<llvm::Constant*> OptInstanceMethods,
2603 ArrayRef<llvm::Constant*> OptClassMethods,
2604 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002605 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002606 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002607 llvm::Constant *Values[] = {
2608 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002609 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002610 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002611 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002612 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002613 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002614 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002615 OptClassMethods),
2616 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002617 ObjCTypes),
2618 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2619 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002620 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002621
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002622 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002623 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002624 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002625 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002626
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002627 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002628 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002629
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002630 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002631 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002632 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002633 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002634}
2635
2636/*
2637 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002638 struct objc_protocol_list *next;
2639 long count;
2640 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002641 };
2642*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002643llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002644CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002645 ObjCProtocolDecl::protocol_iterator begin,
2646 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002647 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002648
Daniel Dunbardbc93372008-08-21 21:57:41 +00002649 for (; begin != end; ++begin)
2650 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002651
2652 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002653 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002654 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002655
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002656 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002657 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002658
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002659 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002660 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002661 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002662 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002663 ProtocolRefs.size() - 1);
2664 Values[2] =
2665 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2666 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002667 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002668
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002669 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002670 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002671 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002672 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002673 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002674}
2675
Bill Wendlingbb028552012-02-07 09:25:09 +00002676void CGObjCCommonMac::
2677PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002678 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002679 const Decl *Container,
2680 const ObjCProtocolDecl *PROTO,
2681 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002682 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2683 E = PROTO->protocol_end(); P != E; ++P)
2684 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2685 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2686 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002687 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002688 if (!PropertySet.insert(PD->getIdentifier()))
2689 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002690 llvm::Constant *Prop[] = {
2691 GetPropertyName(PD->getIdentifier()),
2692 GetPropertyTypeString(PD, Container)
2693 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002694 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2695 }
2696}
2697
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002698/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002699 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002700 const char * const name;
2701 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002702 };
2703
2704 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002705 uint32_t entsize; // sizeof (struct _objc_property)
2706 uint32_t prop_count;
2707 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002708 };
2709*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002710llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002711 const Decl *Container,
2712 const ObjCContainerDecl *OCD,
2713 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002714 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002715 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002716 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2717 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002718 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002719 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002720 llvm::Constant *Prop[] = {
2721 GetPropertyName(PD->getIdentifier()),
2722 GetPropertyTypeString(PD, Container)
2723 };
Owen Anderson08e25242009-07-27 22:29:56 +00002724 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002725 Prop));
2726 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002727 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002728 for (ObjCInterfaceDecl::all_protocol_iterator
2729 P = OID->all_referenced_protocol_begin(),
2730 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002731 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2732 ObjCTypes);
2733 }
2734 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2735 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2736 E = CD->protocol_end(); P != E; ++P)
2737 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2738 ObjCTypes);
2739 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002740
2741 // Return null for empty list.
2742 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002743 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002744
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002745 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002746 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002747 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002748 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2749 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002750 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002751 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002752 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002753 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002754
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002755 llvm::GlobalVariable *GV =
2756 CreateMetadataVar(Name, Init,
2757 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002758 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002759 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002760 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002761 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002762}
2763
Bill Wendlingbb028552012-02-07 09:25:09 +00002764llvm::Constant *
2765CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2766 ArrayRef<llvm::Constant*> MethodTypes,
2767 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002768 // Return null for empty list.
2769 if (MethodTypes.empty())
2770 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2771
2772 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2773 MethodTypes.size());
2774 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2775
2776 llvm::GlobalVariable *GV =
2777 CreateMetadataVar(Name, Init,
2778 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2779 (ObjCABI == 2) ? 8 : 4,
2780 true);
2781 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2782}
2783
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002784/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002785 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002786 int count;
2787 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002788 };
2789*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002790llvm::Constant *
2791CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002792 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002793 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002794 ObjCTypes.SelectorPtrTy),
2795 GetMethodVarType(MD)
2796 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002797 if (!Desc[1])
2798 return 0;
2799
Owen Anderson08e25242009-07-27 22:29:56 +00002800 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002801 Desc);
2802}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002803
Bill Wendlingbb028552012-02-07 09:25:09 +00002804llvm::Constant *
2805CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2806 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002807 // Return null for empty list.
2808 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002809 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002810
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002811 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002812 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002813 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002814 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002815 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002816 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002817
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002818 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002819 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002820 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002821}
2822
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002823/*
2824 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002825 char *category_name;
2826 char *class_name;
2827 struct _objc_method_list *instance_methods;
2828 struct _objc_method_list *class_methods;
2829 struct _objc_protocol_list *protocols;
2830 uint32_t size; // <rdar://4585769>
2831 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002832 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002833*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002834void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002835 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002836
Mike Stumpf5408fe2009-05-16 07:57:57 +00002837 // FIXME: This is poor design, the OCD should have a pointer to the category
2838 // decl. Additionally, note that Category can be null for the @implementation
2839 // w/o an @interface case. Sema should just create one for us as it does for
2840 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002841 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002842 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002843 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002844
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002845 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002846 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2847 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002848
Bill Wendling3964e622012-02-09 22:16:49 +00002849 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002850 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002851 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002852 // Instance methods should always be defined.
2853 InstanceMethods.push_back(GetMethodConstant(*i));
2854 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002855 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002856 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002857 // Class methods should always be defined.
2858 ClassMethods.push_back(GetMethodConstant(*i));
2859 }
2860
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002861 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002862 Values[0] = GetClassName(OCD->getIdentifier());
2863 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002864 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002865 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002866 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002867 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002868 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002869 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002870 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002871 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002872 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002873 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002874 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002875 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002876 Category->protocol_begin(),
2877 Category->protocol_end());
2878 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002879 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002880 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002881 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002882
2883 // If there is no category @interface then there can be no properties.
2884 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002885 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002886 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002887 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002888 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002889 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002890
Owen Anderson08e25242009-07-27 22:29:56 +00002891 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002892 Values);
2893
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002894 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002895 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002896 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002897 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002898 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002899 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002900 // method definition entries must be clear for next implementation.
2901 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002902}
2903
John McCall621915c2012-10-17 04:53:23 +00002904enum FragileClassFlags {
2905 FragileABI_Class_Factory = 0x00001,
2906 FragileABI_Class_Meta = 0x00002,
2907 FragileABI_Class_HasCXXStructors = 0x02000,
2908 FragileABI_Class_Hidden = 0x20000
2909};
2910
2911enum NonFragileClassFlags {
2912 /// Is a meta-class.
2913 NonFragileABI_Class_Meta = 0x00001,
2914
2915 /// Is a root class.
2916 NonFragileABI_Class_Root = 0x00002,
2917
2918 /// Has a C++ constructor and destructor.
2919 NonFragileABI_Class_HasCXXStructors = 0x00004,
2920
2921 /// Has hidden visibility.
2922 NonFragileABI_Class_Hidden = 0x00010,
2923
2924 /// Has the exception attribute.
2925 NonFragileABI_Class_Exception = 0x00020,
2926
2927 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2928 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2929
2930 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00002931 NonFragileABI_Class_CompiledByARC = 0x00080,
2932
2933 /// Class has non-trivial destructors, but zero-initialization is okay.
2934 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002935};
2936
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002937/*
2938 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002939 Class isa;
2940 Class super_class;
2941 const char *name;
2942 long version;
2943 long info;
2944 long instance_size;
2945 struct _objc_ivar_list *ivars;
2946 struct _objc_method_list *methods;
2947 struct _objc_cache *cache;
2948 struct _objc_protocol_list *protocols;
2949 // Objective-C 1.0 extensions (<rdr://4585769>)
2950 const char *ivar_layout;
2951 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002952 };
2953
2954 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002955*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002956void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002957 DefinedSymbols.insert(ID->getIdentifier());
2958
Chris Lattner8ec03f52008-11-24 03:54:41 +00002959 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002960 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002961 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002962 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002963 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002964 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002965 Interface->all_referenced_protocol_begin(),
2966 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00002967 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00002968 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00002969 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002970 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002971 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002972
2973 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002974 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00002975 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002976
Bill Wendling3964e622012-02-09 22:16:49 +00002977 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002978 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002979 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002980 // Instance methods should always be defined.
2981 InstanceMethods.push_back(GetMethodConstant(*i));
2982 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002983 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002984 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002985 // Class methods should always be defined.
2986 ClassMethods.push_back(GetMethodConstant(*i));
2987 }
2988
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002989 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002990 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002991 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002992
2993 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2994 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2995
2996 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2997 if (llvm::Constant *C = GetMethodConstant(MD))
2998 InstanceMethods.push_back(C);
2999 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3000 if (llvm::Constant *C = GetMethodConstant(MD))
3001 InstanceMethods.push_back(C);
3002 }
3003 }
3004
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003005 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003006 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003007 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003008 // Record a reference to the super class.
3009 LazySymbols.insert(Super->getIdentifier());
3010
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003011 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003012 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003013 ObjCTypes.ClassPtrTy);
3014 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003015 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003016 }
3017 Values[ 2] = GetClassName(ID->getIdentifier());
3018 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003019 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3020 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3021 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003022 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003023 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003024 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003025 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003026 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003027 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003028 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003029 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003030 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003031 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003032 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003033 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003034 std::string Name("\01L_OBJC_CLASS_");
3035 Name += ClassName;
3036 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3037 // Check for a forward reference.
3038 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3039 if (GV) {
3040 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3041 "Forward metaclass reference has incorrect type.");
3042 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3043 GV->setInitializer(Init);
3044 GV->setSection(Section);
3045 GV->setAlignment(4);
3046 CGM.AddUsedGlobal(GV);
3047 }
3048 else
3049 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003050 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003051 // method definition entries must be clear for next implementation.
3052 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003053}
3054
3055llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3056 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003057 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003058 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003059 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003060
John McCall1fb0caa2010-10-22 21:05:15 +00003061 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003062 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003063
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003064 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003065 // The isa for the metaclass is the root of the hierarchy.
3066 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3067 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3068 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003069 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003070 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003071 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003072 // The super class for the metaclass is emitted as the name of the
3073 // super class. The runtime fixes this up to point to the
3074 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003075 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003076 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003077 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003078 ObjCTypes.ClassPtrTy);
3079 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003080 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003081 }
3082 Values[ 2] = GetClassName(ID->getIdentifier());
3083 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003084 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3085 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3086 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003087 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003088 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003089 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003090 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003091 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003092 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003093 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003094 Values[ 9] = Protocols;
3095 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003096 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003097 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003098 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003099 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003100 Values);
3101
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003102 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003103 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003104
3105 // Check for a forward reference.
3106 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3107 if (GV) {
3108 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3109 "Forward metaclass reference has incorrect type.");
3110 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3111 GV->setInitializer(Init);
3112 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003113 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003114 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003115 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003116 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003117 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003118 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003119 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003120
3121 return GV;
3122}
3123
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003124llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003125 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003126
Mike Stumpf5408fe2009-05-16 07:57:57 +00003127 // FIXME: Should we look these up somewhere other than the module. Its a bit
3128 // silly since we only generate these while processing an implementation, so
3129 // exactly one pointer would work if know when we entered/exitted an
3130 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003131
3132 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003133 // Previously, metaclass with internal linkage may have been defined.
3134 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003135 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3136 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003137 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3138 "Forward metaclass reference has incorrect type.");
3139 return GV;
3140 } else {
3141 // Generate as an external reference to keep a consistent
3142 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003143 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003144 llvm::GlobalValue::ExternalLinkage,
3145 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003146 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003147 }
3148}
3149
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003150llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3151 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3152
3153 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3154 true)) {
3155 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3156 "Forward class metadata reference has incorrect type.");
3157 return GV;
3158 } else {
3159 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3160 llvm::GlobalValue::ExternalLinkage,
3161 0,
3162 Name);
3163 }
3164}
3165
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003166/*
3167 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003168 uint32_t size;
3169 const char *weak_ivar_layout;
3170 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003171 };
3172*/
3173llvm::Constant *
3174CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003175 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003176 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003177
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003178 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003179 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003180 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003181 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003182 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003183
3184 // Return null if no extension bits are used.
3185 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003186 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003187
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003188 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003189 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003190 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003191 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003192 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003193}
3194
3195/*
3196 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003197 char *ivar_name;
3198 char *ivar_type;
3199 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003200 };
3201
3202 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003203 int ivar_count;
3204 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003205 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003206*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003207llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003208 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003209 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003210
3211 // When emitting the root class GCC emits ivar entries for the
3212 // actual class structure. It is not clear if we need to follow this
3213 // behavior; for now lets try and get away with not doing it. If so,
3214 // the cleanest solution would be to make up an ObjCInterfaceDecl
3215 // for the class.
3216 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003217 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003218
Jordy Rosedb8264e2011-07-22 02:08:32 +00003219 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003220
Jordy Rosedb8264e2011-07-22 02:08:32 +00003221 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003222 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003223 // Ignore unnamed bit-fields.
3224 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003225 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003226 llvm::Constant *Ivar[] = {
3227 GetMethodVarName(IVD->getIdentifier()),
3228 GetMethodVarType(IVD),
3229 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedman38ad4f42012-11-06 01:51:20 +00003230 ComputeIvarBaseOffset(CGM, OID, IVD).getQuantity())
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003231 };
Owen Anderson08e25242009-07-27 22:29:56 +00003232 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003233 }
3234
3235 // Return null for empty list.
3236 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003237 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003238
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003239 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003240 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003241 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003242 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003243 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003244 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003245
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003246 llvm::GlobalVariable *GV;
3247 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003248 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003249 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003250 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003251 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003252 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003253 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003254 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003255 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003256}
3257
3258/*
3259 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003260 SEL method_name;
3261 char *method_types;
3262 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003263 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003264
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003265 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003266 struct objc_method_list *obsolete;
3267 int count;
3268 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003269 };
3270*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003271
3272/// GetMethodConstant - Return a struct objc_method constant for the
3273/// given method if it has been defined. The result is null if the
3274/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003275llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003276 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003277 if (!Fn)
3278 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003279
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003280 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003281 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003282 ObjCTypes.SelectorPtrTy),
3283 GetMethodVarType(MD),
3284 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3285 };
Owen Anderson08e25242009-07-27 22:29:56 +00003286 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003287}
3288
Chris Lattner5f9e2722011-07-23 10:55:15 +00003289llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003290 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003291 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003292 // Return null for empty list.
3293 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003294 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003295
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003296 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003297 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003298 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003299 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003300 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003301 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003302 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003303
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003304 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003305 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003306}
3307
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003308llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003309 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003310 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003311 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003312
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003313 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003314 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003315 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003316 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003317 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003318 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003319 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003320 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003321 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003322
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003323 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003324}
3325
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003326llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003327CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003328 llvm::Constant *Init,
3329 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003330 unsigned Align,
3331 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003332 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003333 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003334 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003335 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003336 if (Section)
3337 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003338 if (Align)
3339 GV->setAlignment(Align);
3340 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003341 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003342 return GV;
3343}
3344
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003345llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003346 // Abuse this interface function as a place to finalize.
3347 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003348 return NULL;
3349}
3350
Chris Lattner74391b42009-03-22 21:03:39 +00003351llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003352 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003353}
3354
Chris Lattner74391b42009-03-22 21:03:39 +00003355llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003356 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003357}
3358
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003359llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3360 bool copy) {
3361 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3362}
3363
David Chisnall8fac25d2010-12-26 22:13:16 +00003364llvm::Constant *CGObjCMac::GetGetStructFunction() {
3365 return ObjCTypes.getCopyStructFn();
3366}
3367llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003368 return ObjCTypes.getCopyStructFn();
3369}
3370
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003371llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
3372 return ObjCTypes.getCppAtomicObjectFunction();
3373}
3374
Chris Lattner74391b42009-03-22 21:03:39 +00003375llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003376 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003377}
3378
John McCallf1549f62010-07-06 01:34:17 +00003379void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3380 return EmitTryOrSynchronizedStmt(CGF, S);
3381}
3382
3383void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3384 const ObjCAtSynchronizedStmt &S) {
3385 return EmitTryOrSynchronizedStmt(CGF, S);
3386}
3387
John McCallcc505292010-07-21 06:59:36 +00003388namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003389 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003390 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003391 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003392 llvm::Value *CallTryExitVar;
3393 llvm::Value *ExceptionData;
3394 ObjCTypesHelper &ObjCTypes;
3395 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003396 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003397 llvm::Value *CallTryExitVar,
3398 llvm::Value *ExceptionData,
3399 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003400 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003401 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3402
John McCallad346f42011-07-12 20:27:29 +00003403 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003404 // Check whether we need to call objc_exception_try_exit.
3405 // In optimized code, this branch will always be folded.
3406 llvm::BasicBlock *FinallyCallExit =
3407 CGF.createBasicBlock("finally.call_exit");
3408 llvm::BasicBlock *FinallyNoCallExit =
3409 CGF.createBasicBlock("finally.no_call_exit");
3410 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3411 FinallyCallExit, FinallyNoCallExit);
3412
3413 CGF.EmitBlock(FinallyCallExit);
3414 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3415 ->setDoesNotThrow();
3416
3417 CGF.EmitBlock(FinallyNoCallExit);
3418
3419 if (isa<ObjCAtTryStmt>(S)) {
3420 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003421 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3422 // Save the current cleanup destination in case there's
3423 // control flow inside the finally statement.
3424 llvm::Value *CurCleanupDest =
3425 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3426
John McCallcc505292010-07-21 06:59:36 +00003427 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3428
John McCalld96a8e72010-08-11 00:16:14 +00003429 if (CGF.HaveInsertPoint()) {
3430 CGF.Builder.CreateStore(CurCleanupDest,
3431 CGF.getNormalCleanupDestSlot());
3432 } else {
3433 // Currently, the end of the cleanup must always exist.
3434 CGF.EnsureInsertPoint();
3435 }
3436 }
John McCallcc505292010-07-21 06:59:36 +00003437 } else {
3438 // Emit objc_sync_exit(expr); as finally's sole statement for
3439 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003440 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003441 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3442 ->setDoesNotThrow();
3443 }
3444 }
3445 };
John McCall87bb5822010-07-31 23:20:56 +00003446
3447 class FragileHazards {
3448 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003449 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003450 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3451
3452 llvm::InlineAsm *ReadHazard;
3453 llvm::InlineAsm *WriteHazard;
3454
3455 llvm::FunctionType *GetAsmFnType();
3456
3457 void collectLocals();
3458 void emitReadHazard(CGBuilderTy &Builder);
3459
3460 public:
3461 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003462
John McCall87bb5822010-07-31 23:20:56 +00003463 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003464 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003465 };
3466}
3467
3468/// Create the fragile-ABI read and write hazards based on the current
3469/// state of the function, which is presumed to be immediately prior
3470/// to a @try block. These hazards are used to maintain correct
3471/// semantics in the face of optimization and the fragile ABI's
3472/// cavalier use of setjmp/longjmp.
3473FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3474 collectLocals();
3475
3476 if (Locals.empty()) return;
3477
3478 // Collect all the blocks in the function.
3479 for (llvm::Function::iterator
3480 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3481 BlocksBeforeTry.insert(&*I);
3482
3483 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3484
3485 // Create a read hazard for the allocas. This inhibits dead-store
3486 // optimizations and forces the values to memory. This hazard is
3487 // inserted before any 'throwing' calls in the protected scope to
3488 // reflect the possibility that the variables might be read from the
3489 // catch block if the call throws.
3490 {
3491 std::string Constraint;
3492 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3493 if (I) Constraint += ',';
3494 Constraint += "*m";
3495 }
3496
3497 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3498 }
3499
3500 // Create a write hazard for the allocas. This inhibits folding
3501 // loads across the hazard. This hazard is inserted at the
3502 // beginning of the catch path to reflect the possibility that the
3503 // variables might have been written within the protected scope.
3504 {
3505 std::string Constraint;
3506 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3507 if (I) Constraint += ',';
3508 Constraint += "=*m";
3509 }
3510
3511 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3512 }
3513}
3514
3515/// Emit a write hazard at the current location.
3516void FragileHazards::emitWriteHazard() {
3517 if (Locals.empty()) return;
3518
Jay Foad4c7d9f12011-07-15 08:37:34 +00003519 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003520}
3521
John McCall87bb5822010-07-31 23:20:56 +00003522void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3523 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003524 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003525}
3526
3527/// Emit read hazards in all the protected blocks, i.e. all the blocks
3528/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003529void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003530 if (Locals.empty()) return;
3531
3532 CGBuilderTy Builder(CGF.getLLVMContext());
3533
3534 // Iterate through all blocks, skipping those prior to the try.
3535 for (llvm::Function::iterator
3536 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3537 llvm::BasicBlock &BB = *FI;
3538 if (BlocksBeforeTry.count(&BB)) continue;
3539
3540 // Walk through all the calls in the block.
3541 for (llvm::BasicBlock::iterator
3542 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3543 llvm::Instruction &I = *BI;
3544
3545 // Ignore instructions that aren't non-intrinsic calls.
3546 // These are the only calls that can possibly call longjmp.
3547 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3548 if (isa<llvm::IntrinsicInst>(I))
3549 continue;
3550
3551 // Ignore call sites marked nounwind. This may be questionable,
3552 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3553 llvm::CallSite CS(&I);
3554 if (CS.doesNotThrow()) continue;
3555
John McCall0b251722010-08-04 05:59:32 +00003556 // Insert a read hazard before the call. This will ensure that
3557 // any writes to the locals are performed before making the
3558 // call. If the call throws, then this is sufficient to
3559 // guarantee correctness as long as it doesn't also write to any
3560 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003561 Builder.SetInsertPoint(&BB, BI);
3562 emitReadHazard(Builder);
3563 }
3564 }
3565}
3566
3567static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3568 if (V) S.insert(V);
3569}
3570
3571void FragileHazards::collectLocals() {
3572 // Compute a set of allocas to ignore.
3573 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3574 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3575 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003576
3577 // Collect all the allocas currently in the function. This is
3578 // probably way too aggressive.
3579 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3580 for (llvm::BasicBlock::iterator
3581 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3582 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3583 Locals.push_back(&*I);
3584}
3585
3586llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003587 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003588 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3589 tys[i] = Locals[i]->getType();
3590 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003591}
3592
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003593/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003594
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003595 Objective-C setjmp-longjmp (sjlj) Exception Handling
3596 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003597
John McCallf1549f62010-07-06 01:34:17 +00003598 A catch buffer is a setjmp buffer plus:
3599 - a pointer to the exception that was caught
3600 - a pointer to the previous exception data buffer
3601 - two pointers of reserved storage
3602 Therefore catch buffers form a stack, with a pointer to the top
3603 of the stack kept in thread-local storage.
3604
3605 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3606 objc_exception_try_exit pops the given catch buffer, which is
3607 required to be the top of the EH stack.
3608 objc_exception_throw pops the top of the EH stack, writes the
3609 thrown exception into the appropriate field, and longjmps
3610 to the setjmp buffer. It crashes the process (with a printf
3611 and an abort()) if there are no catch buffers on the stack.
3612 objc_exception_extract just reads the exception pointer out of the
3613 catch buffer.
3614
3615 There's no reason an implementation couldn't use a light-weight
3616 setjmp here --- something like __builtin_setjmp, but API-compatible
3617 with the heavyweight setjmp. This will be more important if we ever
3618 want to implement correct ObjC/C++ exception interactions for the
3619 fragile ABI.
3620
3621 Note that for this use of setjmp/longjmp to be correct, we may need
3622 to mark some local variables volatile: if a non-volatile local
3623 variable is modified between the setjmp and the longjmp, it has
3624 indeterminate value. For the purposes of LLVM IR, it may be
3625 sufficient to make loads and stores within the @try (to variables
3626 declared outside the @try) volatile. This is necessary for
3627 optimized correctness, but is not currently being done; this is
3628 being tracked as rdar://problem/8160285
3629
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003630 The basic framework for a @try-catch-finally is as follows:
3631 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003632 objc_exception_data d;
3633 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003634 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003635
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003636 objc_exception_try_enter(&d);
3637 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003638 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003639 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003640 // exception path
3641 id _caught = objc_exception_extract(&d);
3642
3643 // enter new try scope for handlers
3644 if (!setjmp(d.jmp_buf)) {
3645 ... match exception and execute catch blocks ...
3646
3647 // fell off end, rethrow.
3648 _rethrow = _caught;
3649 ... jump-through-finally to finally_rethrow ...
3650 } else {
3651 // exception in catch block
3652 _rethrow = objc_exception_extract(&d);
3653 _call_try_exit = false;
3654 ... jump-through-finally to finally_rethrow ...
3655 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003656 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003657 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003658
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003659 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003660 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003661 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003662
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003663 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003664 ... dispatch to finally destination ...
3665
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003666 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003667 objc_exception_throw(_rethrow);
3668
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003669 finally_end:
3670 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003671
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003672 This framework differs slightly from the one gcc uses, in that gcc
3673 uses _rethrow to determine if objc_exception_try_exit should be called
3674 and if the object should be rethrown. This breaks in the face of
3675 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003676
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003677 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003679 - If there are no catch blocks, then we avoid emitting the second
3680 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003681
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003682 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3683 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003684
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685 - FIXME: If there is no @finally block we can do a few more
3686 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003687
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003688 Rethrows and Jumps-Through-Finally
3689 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003690
John McCallf1549f62010-07-06 01:34:17 +00003691 '@throw;' is supported by pushing the currently-caught exception
3692 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003693
John McCallf1549f62010-07-06 01:34:17 +00003694 Branches through the @finally block are handled with an ordinary
3695 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3696 exceptions are not compatible with C++ exceptions, and this is
3697 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003698
John McCallf1549f62010-07-06 01:34:17 +00003699 @synchronized(expr) { stmt; } is emitted as if it were:
3700 id synch_value = expr;
3701 objc_sync_enter(synch_value);
3702 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003703*/
3704
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003705void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3706 const Stmt &S) {
3707 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003708
3709 // A destination for the fall-through edges of the catch handlers to
3710 // jump to.
3711 CodeGenFunction::JumpDest FinallyEnd =
3712 CGF.getJumpDestInCurrentScope("finally.end");
3713
3714 // A destination for the rethrow edge of the catch handlers to jump
3715 // to.
3716 CodeGenFunction::JumpDest FinallyRethrow =
3717 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003718
Daniel Dunbar1c566672009-02-24 01:43:46 +00003719 // For @synchronized, call objc_sync_enter(sync.expr). The
3720 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003721 // @synchronized. We can't avoid a temp here because we need the
3722 // value to be preserved. If the backend ever does liveness
3723 // correctly after setjmp, this will be unnecessary.
3724 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003725 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003726 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003727 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3728 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003729 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3730 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003731
3732 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3733 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003734 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003735
John McCall0b251722010-08-04 05:59:32 +00003736 // Allocate memory for the setjmp buffer. This needs to be kept
3737 // live throughout the try and catch blocks.
3738 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3739 "exceptiondata.ptr");
3740
John McCall87bb5822010-07-31 23:20:56 +00003741 // Create the fragile hazards. Note that this will not capture any
3742 // of the allocas required for exception processing, but will
3743 // capture the current basic block (which extends all the way to the
3744 // setjmp call) as "before the @try".
3745 FragileHazards Hazards(CGF);
3746
John McCallf1549f62010-07-06 01:34:17 +00003747 // Create a flag indicating whether the cleanup needs to call
3748 // objc_exception_try_exit. This is true except when
3749 // - no catches match and we're branching through the cleanup
3750 // just to rethrow the exception, or
3751 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003752 // The setjmp-safety rule here is that we should always store to this
3753 // variable in a place that dominates the branch through the cleanup
3754 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003755 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003756 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003757
John McCall9e2213d2010-10-04 23:42:51 +00003758 // A slot containing the exception to rethrow. Only needed when we
3759 // have both a @catch and a @finally.
3760 llvm::Value *PropagatingExnVar = 0;
3761
John McCallf1549f62010-07-06 01:34:17 +00003762 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003763 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003764 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003765 CallTryExitVar,
3766 ExceptionData,
3767 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003768
3769 // Enter a try block:
3770 // - Call objc_exception_try_enter to push ExceptionData on top of
3771 // the EH stack.
3772 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3773 ->setDoesNotThrow();
3774
3775 // - Call setjmp on the exception data buffer.
3776 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3777 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3778 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003779 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003780 llvm::CallInst *SetJmpResult =
3781 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3782 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003783 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003784
3785 // If setjmp returned 0, enter the protected block; otherwise,
3786 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003787 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3788 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003789 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003790 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3791 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003792
John McCallf1549f62010-07-06 01:34:17 +00003793 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003794 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003795 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003796 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003797 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003798
3799 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003800
John McCallf1549f62010-07-06 01:34:17 +00003801 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003802 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003803
John McCall87bb5822010-07-31 23:20:56 +00003804 // Don't optimize loads of the in-scope locals across this point.
3805 Hazards.emitWriteHazard();
3806
John McCallf1549f62010-07-06 01:34:17 +00003807 // For a @synchronized (or a @try with no catches), just branch
3808 // through the cleanup to the rethrow block.
3809 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3810 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003811 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003812 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003813
3814 // Otherwise, we have to match against the caught exceptions.
3815 } else {
John McCall0b251722010-08-04 05:59:32 +00003816 // Retrieve the exception object. We may emit multiple blocks but
3817 // nothing can cross this so the value is already in SSA form.
3818 llvm::CallInst *Caught =
3819 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3820 ExceptionData, "caught");
3821 Caught->setDoesNotThrow();
3822
John McCallf1549f62010-07-06 01:34:17 +00003823 // Push the exception to rethrow onto the EH value stack for the
3824 // benefit of any @throws in the handlers.
3825 CGF.ObjCEHValueStack.push_back(Caught);
3826
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003827 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003828
John McCall0b251722010-08-04 05:59:32 +00003829 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003830
John McCall0b251722010-08-04 05:59:32 +00003831 llvm::BasicBlock *CatchBlock = 0;
3832 llvm::BasicBlock *CatchHandler = 0;
3833 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003834 // Save the currently-propagating exception before
3835 // objc_exception_try_enter clears the exception slot.
3836 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3837 "propagating_exception");
3838 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3839
John McCall0b251722010-08-04 05:59:32 +00003840 // Enter a new exception try block (in case a @catch block
3841 // throws an exception).
3842 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3843 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003844
John McCall0b251722010-08-04 05:59:32 +00003845 llvm::CallInst *SetJmpResult =
3846 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3847 "setjmp.result");
3848 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003849 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003850
John McCall0b251722010-08-04 05:59:32 +00003851 llvm::Value *Threw =
3852 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3853
3854 CatchBlock = CGF.createBasicBlock("catch");
3855 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3856 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3857
3858 CGF.EmitBlock(CatchBlock);
3859 }
3860
3861 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003862
Daniel Dunbar55e40722008-09-27 07:03:52 +00003863 // Handle catch list. As a special case we check if everything is
3864 // matched and avoid generating code for falling off the end if
3865 // so.
3866 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003867 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3868 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003869
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003870 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003871 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003872
Anders Carlsson80f25672008-09-09 17:59:25 +00003873 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003874 if (!CatchParam) {
3875 AllMatched = true;
3876 } else {
John McCall183700f2009-09-21 23:43:11 +00003877 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003878
John McCallf1549f62010-07-06 01:34:17 +00003879 // catch(id e) always matches under this ABI, since only
3880 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003881 // FIXME: For the time being we also match id<X>; this should
3882 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003883 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003884 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003885 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003886
John McCallf1549f62010-07-06 01:34:17 +00003887 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003888 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003889 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3890
Anders Carlssondde0a942008-09-11 09:15:33 +00003891 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003892 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003893 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003894
3895 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003896 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003897 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003898
Anders Carlssondde0a942008-09-11 09:15:33 +00003899 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003900
3901 // The scope of the catch variable ends right here.
3902 CatchVarCleanups.ForceCleanup();
3903
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003904 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003905 break;
3906 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003907
Steve Naroff14108da2009-07-10 23:34:53 +00003908 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003909 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003910
3911 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003912 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3913 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003914
3915 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003916 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003917
John McCallf1549f62010-07-06 01:34:17 +00003918 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003919 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3920 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003921 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003922
John McCallf1549f62010-07-06 01:34:17 +00003923 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3924 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003925
3926 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003927 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003928
Anders Carlsson80f25672008-09-09 17:59:25 +00003929 // Emit the @catch block.
3930 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003931
3932 // Collect any cleanups for the catch variable. The scope lasts until
3933 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003934 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003935
John McCallb6bbcc92010-10-15 04:57:14 +00003936 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003937 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003938
John McCallf1549f62010-07-06 01:34:17 +00003939 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003940 llvm::Value *Tmp =
3941 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003942 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003943 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003944
Anders Carlssondde0a942008-09-11 09:15:33 +00003945 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003946
3947 // We're done with the catch variable.
3948 CatchVarCleanups.ForceCleanup();
3949
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003950 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003951
Anders Carlsson80f25672008-09-09 17:59:25 +00003952 CGF.EmitBlock(NextCatchBlock);
3953 }
3954
John McCallf1549f62010-07-06 01:34:17 +00003955 CGF.ObjCEHValueStack.pop_back();
3956
John McCall0b251722010-08-04 05:59:32 +00003957 // If nothing wanted anything to do with the caught exception,
3958 // kill the extract call.
3959 if (Caught->use_empty())
3960 Caught->eraseFromParent();
3961
3962 if (!AllMatched)
3963 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3964
3965 if (HasFinally) {
3966 // Emit the exception handler for the @catch blocks.
3967 CGF.EmitBlock(CatchHandler);
3968
3969 // In theory we might now need a write hazard, but actually it's
3970 // unnecessary because there's no local-accessing code between
3971 // the try's write hazard and here.
3972 //Hazards.emitWriteHazard();
3973
John McCall9e2213d2010-10-04 23:42:51 +00003974 // Extract the new exception and save it to the
3975 // propagating-exception slot.
3976 assert(PropagatingExnVar);
3977 llvm::CallInst *NewCaught =
3978 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3979 ExceptionData, "caught");
3980 NewCaught->setDoesNotThrow();
3981 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3982
John McCall0b251722010-08-04 05:59:32 +00003983 // Don't pop the catch handler; the throw already did.
3984 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003985 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003986 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003987 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003988
John McCall87bb5822010-07-31 23:20:56 +00003989 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003990 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003991
John McCallf1549f62010-07-06 01:34:17 +00003992 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003993 CGF.Builder.restoreIP(TryFallthroughIP);
3994 if (CGF.HaveInsertPoint())
3995 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003996 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003997 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003998
John McCallf1549f62010-07-06 01:34:17 +00003999 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00004000 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004001 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004002 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004003 // If we have a propagating-exception variable, check it.
4004 llvm::Value *PropagatingExn;
4005 if (PropagatingExnVar) {
4006 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004007
John McCall9e2213d2010-10-04 23:42:51 +00004008 // Otherwise, just look in the buffer for the exception to throw.
4009 } else {
4010 llvm::CallInst *Caught =
4011 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4012 ExceptionData);
4013 Caught->setDoesNotThrow();
4014 PropagatingExn = Caught;
4015 }
4016
4017 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004018 ->setDoesNotThrow();
4019 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004020 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004021
John McCall87bb5822010-07-31 23:20:56 +00004022 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004023}
4024
4025void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00004026 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004027 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004028
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004029 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004030 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004031 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004032 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004033 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004034 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004035 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004036 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004037 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038
John McCallf1549f62010-07-06 01:34:17 +00004039 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4040 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004041 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004042
4043 // Clear the insertion point to indicate we are in unreachable code.
4044 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004045}
4046
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004047/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004048/// object: objc_read_weak (id *src)
4049///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004050llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004051 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004052 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004053 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4054 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4055 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004056 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004057 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004058 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004059 return read_weak;
4060}
4061
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004062/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4063/// objc_assign_weak (id src, id *dst)
4064///
4065void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004066 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004067 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004068 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004069 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004070 assert(Size <= 8 && "does not support size > 8");
4071 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004072 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004073 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4074 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004075 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4076 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004077 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004078 src, dst, "weakassign");
4079 return;
4080}
4081
Fariborz Jahanian58626502008-11-19 00:59:10 +00004082/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4083/// objc_assign_global (id src, id *dst)
4084///
4085void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004086 llvm::Value *src, llvm::Value *dst,
4087 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004088 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004089 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004090 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004091 assert(Size <= 8 && "does not support size > 8");
4092 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004093 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004094 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4095 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004096 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4097 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004098 if (!threadlocal)
4099 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4100 src, dst, "globalassign");
4101 else
4102 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4103 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004104 return;
4105}
4106
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004107/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004108/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004109///
4110void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004111 llvm::Value *src, llvm::Value *dst,
4112 llvm::Value *ivarOffset) {
4113 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004114 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004115 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004116 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004117 assert(Size <= 8 && "does not support size > 8");
4118 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004119 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004120 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4121 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004122 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4123 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004124 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4125 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004126 return;
4127}
4128
Fariborz Jahanian58626502008-11-19 00:59:10 +00004129/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4130/// objc_assign_strongCast (id src, id *dst)
4131///
4132void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004133 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004134 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004135 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004136 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004137 assert(Size <= 8 && "does not support size > 8");
4138 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004139 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004140 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4141 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004142 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4143 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004144 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004145 src, dst, "weakassign");
4146 return;
4147}
4148
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004149void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004150 llvm::Value *DestPtr,
4151 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004152 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004153 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4154 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004155 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004156 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004157 return;
4158}
4159
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004160/// EmitObjCValueForIvar - Code Gen for ivar reference.
4161///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004162LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4163 QualType ObjectTy,
4164 llvm::Value *BaseValue,
4165 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004166 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004167 const ObjCInterfaceDecl *ID =
4168 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004169 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4170 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004171}
4172
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004173llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004174 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004175 const ObjCIvarDecl *Ivar) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004176 CharUnits Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4177 return llvm::ConstantInt::get(ObjCTypes.LongTy, Offset.getQuantity());
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004178}
4179
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004180/* *** Private Interface *** */
4181
4182/// EmitImageInfo - Emit the image info marker used to encode some module
4183/// level information.
4184///
4185/// See: <rdr://4810609&4810587&4810587>
4186/// struct IMAGE_INFO {
4187/// unsigned version;
4188/// unsigned flags;
4189/// };
4190enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004191 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004192 eImageInfo_GarbageCollected = (1 << 1),
4193 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004194 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4195
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004196 // A flag indicating that the module has no instances of a @synthesize of a
4197 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004198 eImageInfo_CorrectedSynthesize = (1 << 4),
4199 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004200};
4201
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004202void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004203 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004204 const char *Section = (ObjCABI == 1) ?
4205 "__OBJC, __image_info,regular" :
4206 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004207
Bill Wendling3973acc2012-02-16 01:13:30 +00004208 // Generate module-level named metadata to convey this information to the
4209 // linker and code-gen.
4210 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004211
Bill Wendling3973acc2012-02-16 01:13:30 +00004212 // Add the ObjC ABI version to the module flags.
4213 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4214 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4215 version);
4216 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4217 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004218
David Blaikie4e4d0842012-03-11 07:00:24 +00004219 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004220 // Non-GC overrides those files which specify GC.
4221 Mod.addModuleFlag(llvm::Module::Override,
4222 "Objective-C Garbage Collection", (uint32_t)0);
4223 } else {
4224 // Add the ObjC garbage collection value.
4225 Mod.addModuleFlag(llvm::Module::Error,
4226 "Objective-C Garbage Collection",
4227 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004228
David Blaikie4e4d0842012-03-11 07:00:24 +00004229 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004230 // Add the ObjC GC Only value.
4231 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4232 eImageInfo_GCOnly);
4233
4234 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4235 llvm::Value *Ops[2] = {
4236 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4237 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4238 eImageInfo_GarbageCollected)
4239 };
4240 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4241 llvm::MDNode::get(VMContext, Ops));
4242 }
4243 }
Bill Wendling09822512012-04-24 11:04:57 +00004244
4245 // Indicate whether we're compiling this to run on a simulator.
4246 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4247 if (Triple.getOS() == llvm::Triple::IOS &&
4248 (Triple.getArch() == llvm::Triple::x86 ||
4249 Triple.getArch() == llvm::Triple::x86_64))
4250 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4251 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004252}
4253
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004254// struct objc_module {
4255// unsigned long version;
4256// unsigned long size;
4257// const char *name;
4258// Symtab symtab;
4259// };
4260
4261// FIXME: Get from somewhere
4262static const int ModuleVersion = 7;
4263
4264void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004265 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004266
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004267 llvm::Constant *Values[] = {
4268 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4269 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4270 // This used to be the filename, now it is unused. <rdr://4327263>
4271 GetClassName(&CGM.getContext().Idents.get("")),
4272 EmitModuleSymbols()
4273 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004274 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004275 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004276 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004277 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004278}
4279
4280llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004281 unsigned NumClasses = DefinedClasses.size();
4282 unsigned NumCategories = DefinedCategories.size();
4283
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004284 // Return null if no symbols were defined.
4285 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004286 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004287
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004288 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004289 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004290 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004291 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4292 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004293
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004294 // The runtime expects exactly the list of defined classes followed
4295 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004296 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004297 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004298 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004299 ObjCTypes.Int8PtrTy);
4300 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004301 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004302 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004303 ObjCTypes.Int8PtrTy);
4304
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004305 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004306 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004307 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004308 Symbols);
4309
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004310 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004311
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004312 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004313 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4314 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004315 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004316 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004317}
4318
John McCallf85e1932011-06-15 23:02:42 +00004319llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4320 IdentifierInfo *II) {
4321 LazySymbols.insert(II);
4322
4323 llvm::GlobalVariable *&Entry = ClassReferences[II];
4324
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004325 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004326 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004327 llvm::ConstantExpr::getBitCast(GetClassName(II),
4328 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004329 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004330 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4331 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4332 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004333 }
John McCallf85e1932011-06-15 23:02:42 +00004334
Benjamin Kramer578faa82011-09-27 21:06:10 +00004335 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004336}
4337
John McCallf85e1932011-06-15 23:02:42 +00004338llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4339 const ObjCInterfaceDecl *ID) {
4340 return EmitClassRefFromId(Builder, ID->getIdentifier());
4341}
4342
4343llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4344 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4345 return EmitClassRefFromId(Builder, II);
4346}
4347
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004348llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4349 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004350 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004351
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004352 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004354 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004355 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004356 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004357 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4358 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004359 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004360 }
4361
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004362 if (lvalue)
4363 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004364 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004365}
4366
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004367llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004368 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004369
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004370 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004371 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004372 llvm::ConstantDataArray::getString(VMContext,
4373 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004374 ((ObjCABI == 2) ?
4375 "__TEXT,__objc_classname,cstring_literals" :
4376 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004377 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004378
Owen Andersona1cf15f2009-07-14 23:10:40 +00004379 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004380}
4381
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004382llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4383 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4384 I = MethodDefinitions.find(MD);
4385 if (I != MethodDefinitions.end())
4386 return I->second;
4387
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004388 return NULL;
4389}
4390
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004391/// GetIvarLayoutName - Returns a unique constant for the given
4392/// ivar layout bitmap.
4393llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004394 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004395 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004396}
4397
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004398void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman38ad4f42012-11-06 01:51:20 +00004399 CharUnits BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004400 bool ForStrongLayout,
4401 bool &HasUnion) {
4402 const RecordDecl *RD = RT->getDecl();
4403 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004404 SmallVector<const FieldDecl*, 16> Fields;
4405 for (RecordDecl::field_iterator i = RD->field_begin(),
4406 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004407 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004408 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004410 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004412 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4413 ForStrongLayout, HasUnion);
4414}
4415
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004416void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004417 const llvm::StructLayout *Layout,
4418 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004419 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman38ad4f42012-11-06 01:51:20 +00004420 CharUnits BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004421 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004422 bool IsUnion = (RD && RD->isUnion());
4423 uint64_t MaxUnionIvarSize = 0;
4424 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00004425 const FieldDecl *MaxField = 0;
4426 const FieldDecl *MaxSkippedField = 0;
4427 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004428 CharUnits MaxFieldOffset = CharUnits::Zero();
4429 CharUnits MaxSkippedFieldOffset = CharUnits::Zero();
4430 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
4431 CharUnits FirstFieldDelta = CharUnits::Zero();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004433 if (RecFields.empty())
4434 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004435 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
4436 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004437 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004438 const ObjCIvarDecl *FirstField = cast<ObjCIvarDecl>(RecFields[0]);
4439 FirstFieldDelta = ComputeIvarBaseOffset(CGM, OI, FirstField);
John McCallf85e1932011-06-15 23:02:42 +00004440 }
4441
Chris Lattnerf1690852009-03-31 08:48:01 +00004442 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004443 const FieldDecl *Field = RecFields[i];
Eli Friedman38ad4f42012-11-06 01:51:20 +00004444 CharUnits FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004445 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004446 // Note that 'i' here is actually the field index inside RD of Field,
4447 // although this dependency is hidden.
4448 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedman38ad4f42012-11-06 01:51:20 +00004449 FieldOffset = CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)) -
4450 FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004451 } else
John McCallf85e1932011-06-15 23:02:42 +00004452 FieldOffset =
4453 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004454
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004455 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004456 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004457 LastFieldBitfieldOrUnnamed = Field;
4458 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004459 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004460 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004461
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004462 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004463 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004464 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004465 if (FQT->isUnionType())
4466 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004467
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004469 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004470 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004471 continue;
4472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Chris Lattnerf1690852009-03-31 08:48:01 +00004474 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004475 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004476 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004477 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004478 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004479 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004480 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4481 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004482 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004483 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004484 FQT = CArray->getElementType();
4485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004486
4487 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004488 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00004489 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004490 int OldIndex = IvarsInfo.size() - 1;
4491 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
Ted Kremenek6217b802009-07-29 21:53:49 +00004493 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004494 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004495 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004497 // Replicate layout information for each array element. Note that
4498 // one element is already done.
4499 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004500 for (int FirstIndex = IvarsInfo.size() - 1,
4501 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004502 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004503 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4504 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4505 IvarsInfo[i].ivar_size));
4506 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4507 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4508 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004509 }
4510 continue;
4511 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004512 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004513 // At this point, we are done with Record/Union and array there of.
4514 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00004515 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004516
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004517 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00004518 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4519 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004520 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004521 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004522 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004523 MaxUnionIvarSize = UnionIvarSize;
4524 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004525 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004526 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004527 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004528 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004529 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004530 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004532 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4533 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004534 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004535 // FIXME: Why the asymmetry? We divide by word size in bits on other
4536 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004537 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004538 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004539 MaxSkippedUnionIvarSize = UnionIvarSize;
4540 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004541 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004542 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004543 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004544 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004545 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004546 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004547 }
4548 }
4549 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004550
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004551 if (LastFieldBitfieldOrUnnamed) {
4552 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4553 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004554 uint64_t BitFieldSize
4555 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004556 GC_IVAR skivar;
4557 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
4558 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4559 + ((BitFieldSize % ByteSizeInBits) != 0);
4560 SkipIvars.push_back(skivar);
4561 } else {
4562 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4563 // Last field was unnamed. Must update skip info.
4564 unsigned FieldSize
4565 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4566 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4567 FieldSize / ByteSizeInBits));
4568 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004570
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004571 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004572 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004573 MaxUnionIvarSize));
4574 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004575 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004576 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004577}
4578
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004579/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4580/// the computations and returning the layout bitmap (for ivar or blocks) in
4581/// the given argument BitMap string container. Routine reads
4582/// two containers, IvarsInfo and SkipIvars which are assumed to be
4583/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004584llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Chris Lattner8b418682012-02-07 00:39:47 +00004585 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004586
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004587 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004588 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004589 CharUnits WordSize = CharUnits::fromQuantity(
4590 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy));
4591 unsigned WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4592 unsigned WordsToScan = IvarsInfo[0].ivar_size;
4593
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004594 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004595 CharUnits TailPrevGCObjC =
4596 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004597 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004598 // consecutive 'scanned' object pointers.
4599 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004600 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004601 // Skip over 'gc'able object pointer which lay over each other.
4602 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4603 continue;
4604 // Must skip over 1 or more words. We save current skip/scan values
4605 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004606 SKIP_SCAN SkScan;
4607 SkScan.skip = WordsToSkip;
4608 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004609 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004610
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004611 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004612 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4613 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004614 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004615 WordsToSkip = 0;
4616 WordsToScan = IvarsInfo[i].ivar_size;
4617 }
4618 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004619 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004620 SKIP_SCAN SkScan;
4621 SkScan.skip = WordsToSkip;
4622 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004623 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004624 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004625
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004626 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004627 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004628 // FIXME: Shouldn't be using CharUnits::One here; what are the units of
4629 // ivar_size?
4630 CharUnits LastByteSkipped =
4631 SkipIvars[LastIndex].ivar_bytepos +
4632 SkipIvars[LastIndex].ivar_size * CharUnits::One();
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004633 LastIndex = IvarsInfo.size()-1;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004634 CharUnits LastByteScanned =
4635 IvarsInfo[LastIndex].ivar_bytepos +
4636 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004637 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004638 if (LastByteSkipped > LastByteScanned) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004639 unsigned int TotalWords =
4640 (LastByteSkipped + (WordSize - CharUnits::One())) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004641 SKIP_SCAN SkScan;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004642 SkScan.skip = TotalWords - LastByteScanned / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004643 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004644 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004645 }
4646 }
4647 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4648 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004649 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004650 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004651 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4652 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4653 // 0xM0 followed by 0x0N detected.
4654 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4655 for (int j = i+1; j < SkipScan; j++)
4656 SkipScanIvars[j] = SkipScanIvars[j+1];
4657 --SkipScan;
4658 }
4659 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004660
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004661 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004662 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004663 unsigned char byte;
4664 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4665 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4666 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4667 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004668
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004669 // first skip big.
4670 for (unsigned int ix = 0; ix < skip_big; ix++)
4671 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004672
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004673 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004674 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004675 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004676 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004677 byte |= 0xf;
4678 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004679 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004680 byte |= scan_small;
4681 scan_small = 0;
4682 }
4683 BitMap += byte;
4684 }
4685 // next scan big
4686 for (unsigned int ix = 0; ix < scan_big; ix++)
4687 BitMap += (unsigned char)(0x0f);
4688 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004689 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004690 byte = scan_small;
4691 BitMap += byte;
4692 }
4693 }
4694 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004695 unsigned char zero = 0;
4696 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004697
4698 llvm::GlobalVariable * Entry =
4699 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004700 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004701 ((ObjCABI == 2) ?
4702 "__TEXT,__objc_classname,cstring_literals" :
4703 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004704 1, true);
4705 return getConstantGEP(VMContext, Entry, 0, 0);
4706}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004708/// BuildIvarLayout - Builds ivar layout bitmap for the class
4709/// implementation for the __strong or __weak case.
4710/// The layout map displays which words in ivar list must be skipped
4711/// and which must be scanned by GC (see below). String is built of bytes.
4712/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4713/// of words to skip and right nibble is count of words to scan. So, each
4714/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4715/// represented by a 0x00 byte which also ends the string.
4716/// 1. when ForStrongLayout is true, following ivars are scanned:
4717/// - id, Class
4718/// - object *
4719/// - __strong anything
4720///
4721/// 2. When ForStrongLayout is false, following ivars are scanned:
4722/// - __weak anything
4723///
4724llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4725 const ObjCImplementationDecl *OMD,
4726 bool ForStrongLayout) {
4727 bool hasUnion = false;
4728
Chris Lattner8b418682012-02-07 00:39:47 +00004729 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004730 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4731 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004732 return llvm::Constant::getNullValue(PtrTy);
4733
Jordy Rosedb8264e2011-07-22 02:08:32 +00004734 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4735 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004736 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004737 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004738 IVD; IVD = IVD->getNextIvar())
4739 RecFields.push_back(cast<FieldDecl>(IVD));
4740 }
4741 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004742 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004743 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004744
Jordy Rosedb8264e2011-07-22 02:08:32 +00004745 // FIXME: This is not ideal; we shouldn't have to do this copy.
4746 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004747 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004748
4749 if (RecFields.empty())
4750 return llvm::Constant::getNullValue(PtrTy);
4751
4752 SkipIvars.clear();
4753 IvarsInfo.clear();
4754
Eli Friedman38ad4f42012-11-06 01:51:20 +00004755 BuildAggrIvarLayout(OMD, 0, 0, RecFields, CharUnits::Zero(),
4756 ForStrongLayout, hasUnion);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004757 if (IvarsInfo.empty())
4758 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004759 // Sort on byte position in case we encounterred a union nested in
4760 // the ivar list.
4761 if (hasUnion && !IvarsInfo.empty())
4762 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4763 if (hasUnion && !SkipIvars.empty())
4764 std::sort(SkipIvars.begin(), SkipIvars.end());
4765
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004766 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004767 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004768
David Blaikie4e4d0842012-03-11 07:00:24 +00004769 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004771 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004772 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004773 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004774 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004775 if (!(s[i] & 0xf0))
4776 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4777 else
4778 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4779 printf("\n");
4780 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004781 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004782}
4783
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004784llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004785 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4786
Chris Lattner0b239712012-02-06 22:16:34 +00004787 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004788 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004789 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004790 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004791 ((ObjCABI == 2) ?
4792 "__TEXT,__objc_methname,cstring_literals" :
4793 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004794 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004795
Owen Andersona1cf15f2009-07-14 23:10:40 +00004796 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004797}
4798
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004799// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004800llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004801 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4802}
4803
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004804llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004805 std::string TypeStr;
4806 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4807
4808 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004809
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004810 if (!Entry)
4811 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004812 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004813 ((ObjCABI == 2) ?
4814 "__TEXT,__objc_methtype,cstring_literals" :
4815 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004816 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004817
Owen Andersona1cf15f2009-07-14 23:10:40 +00004818 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004819}
4820
Bob Wilsondc8dab62011-11-30 01:57:58 +00004821llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4822 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004823 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004824 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004825 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004826
4827 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4828
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004829 if (!Entry)
4830 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004831 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004832 ((ObjCABI == 2) ?
4833 "__TEXT,__objc_methtype,cstring_literals" :
4834 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004835 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004836
Owen Andersona1cf15f2009-07-14 23:10:40 +00004837 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004838}
4839
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004840// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004841llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004842 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004843
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004844 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004845 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004846 llvm::ConstantDataArray::getString(VMContext,
4847 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004848 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004849 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004850
Owen Andersona1cf15f2009-07-14 23:10:40 +00004851 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004852}
4853
4854// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004855// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004856llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004857CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4858 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004859 std::string TypeStr;
4860 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004861 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4862}
4863
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004864void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004865 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004866 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004867 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004868 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004869 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4870 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004871 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004872 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004873 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004874 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004875}
4876
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004877void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004878 EmitModuleInfo();
4879
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004880 // Emit the dummy bodies for any protocols which were referenced but
4881 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004883 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4884 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004885 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004886
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004887 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004888 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004889 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004890 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004891 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004892 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004893 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004894 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004895 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004896 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004897 }
4898
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004899 // Add assembler directives to add lazy undefined symbol references
4900 // for classes which are referenced but not defined. This is
4901 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004902 //
4903 // FIXME: It would be nice if we had an LLVM construct for this.
4904 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004905 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004906 Asm += CGM.getModule().getModuleInlineAsm();
4907 if (!Asm.empty() && Asm.back() != '\n')
4908 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004909
Daniel Dunbar33063492009-09-07 00:20:42 +00004910 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004911 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4912 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004913 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4914 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004915 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004916 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004917 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004918 }
4919
Bill Wendling13562a12012-02-07 09:06:01 +00004920 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004921 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4922 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4923 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004924
Daniel Dunbar33063492009-09-07 00:20:42 +00004925 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004926 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004927}
4928
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004929CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004930 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004931 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004932 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004933 ObjCABI = 2;
4934}
4935
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004936/* *** */
4937
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004938ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004939 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4940{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004941 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4942 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004943
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004944 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004945 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004946 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004947 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004948 Int8PtrTy = CGM.Int8PtrTy;
4949 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004950
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004951 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004952 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004953 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004954
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004955 // I'm not sure I like this. The implicit coordination is a bit
4956 // gross. We should solve this in a reasonable fashion because this
4957 // is a pretty common task (match some runtime data structure with
4958 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004959
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004960 // FIXME: This is leaked.
4961 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004962
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004963 // struct _objc_super {
4964 // id self;
4965 // Class cls;
4966 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004967 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004968 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004969 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004970 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004971 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004972 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004973 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004974 Ctx.getObjCClassType(), 0, 0, false,
4975 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004976 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004977
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004978 SuperCTy = Ctx.getTagDeclType(RD);
4979 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004980
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004981 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004982 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4983
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004984 // struct _prop_t {
4985 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004987 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004988 PropertyTy = llvm::StructType::create("struct._prop_t",
4989 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004990
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004991 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004992 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004993 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004994 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004995 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004996 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004997 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4998 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004999 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005000 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005001
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005002 // struct _objc_method {
5003 // SEL _cmd;
5004 // char *method_type;
5005 // char *_imp;
5006 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005007 MethodTy = llvm::StructType::create("struct._objc_method",
5008 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5009 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005010
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005011 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005012 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005013 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005014
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005015}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005016
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005017ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005018 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005019 // struct _objc_method_description {
5020 // SEL name;
5021 // char *types;
5022 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005023 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005024 llvm::StructType::create("struct._objc_method_description",
5025 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005026
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005027 // struct _objc_method_description_list {
5028 // int count;
5029 // struct _objc_method_description[1];
5030 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005031 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005032 llvm::StructType::create("struct._objc_method_description_list",
5033 IntTy,
5034 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005035
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005036 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005037 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005038 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005039
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005040 // Protocol description structures
5041
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005042 // struct _objc_protocol_extension {
5043 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5044 // struct _objc_method_description_list *optional_instance_methods;
5045 // struct _objc_method_description_list *optional_class_methods;
5046 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005047 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005048 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005049 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005050 llvm::StructType::create("struct._objc_protocol_extension",
5051 IntTy, MethodDescriptionListPtrTy,
5052 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005053 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005054
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005055 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005056 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005057
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005058 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005059
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005060 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005061 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005062
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005063 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005064 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005065 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005066 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005067 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005068 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005069
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005070 // struct _objc_protocol {
5071 // struct _objc_protocol_extension *isa;
5072 // char *protocol_name;
5073 // struct _objc_protocol **_objc_protocol_list;
5074 // struct _objc_method_description_list *instance_methods;
5075 // struct _objc_method_description_list *class_methods;
5076 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005077 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5078 llvm::PointerType::getUnqual(ProtocolListTy),
5079 MethodDescriptionListPtrTy,
5080 MethodDescriptionListPtrTy,
5081 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005082
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005083 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005084 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005085
Owen Anderson96e0fc72009-07-29 22:16:19 +00005086 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005087
5088 // Class description structures
5089
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005090 // struct _objc_ivar {
5091 // char *ivar_name;
5092 // char *ivar_type;
5093 // int ivar_offset;
5094 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005095 IvarTy = llvm::StructType::create("struct._objc_ivar",
5096 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005097
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005098 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005099 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005100 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005101 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005102
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005103 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005104 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005105 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005106 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005107
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005108 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005110 llvm::StructType::create("struct._objc_class_extension",
5111 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005112 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005113
Chris Lattnerc1c20112011-08-12 17:43:31 +00005114 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005115
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005116 // struct _objc_class {
5117 // Class isa;
5118 // Class super_class;
5119 // char *name;
5120 // long version;
5121 // long info;
5122 // long instance_size;
5123 // struct _objc_ivar_list *ivars;
5124 // struct _objc_method_list *methods;
5125 // struct _objc_cache *cache;
5126 // struct _objc_protocol_list *protocols;
5127 // char *ivar_layout;
5128 // struct _objc_class_ext *ext;
5129 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005130 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5131 llvm::PointerType::getUnqual(ClassTy),
5132 Int8PtrTy,
5133 LongTy,
5134 LongTy,
5135 LongTy,
5136 IvarListPtrTy,
5137 MethodListPtrTy,
5138 CachePtrTy,
5139 ProtocolListPtrTy,
5140 Int8PtrTy,
5141 ClassExtensionPtrTy,
5142 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005143
Owen Anderson96e0fc72009-07-29 22:16:19 +00005144 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005145
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005146 // struct _objc_category {
5147 // char *category_name;
5148 // char *class_name;
5149 // struct _objc_method_list *instance_method;
5150 // struct _objc_method_list *class_method;
5151 // uint32_t size; // sizeof(struct _objc_category)
5152 // struct _objc_property_list *instance_properties;// category's @property
5153 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005154 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005155 llvm::StructType::create("struct._objc_category",
5156 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5157 MethodListPtrTy, ProtocolListPtrTy,
5158 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005159
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005160 // Global metadata structures
5161
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005162 // struct _objc_symtab {
5163 // long sel_ref_cnt;
5164 // SEL *refs;
5165 // short cls_def_cnt;
5166 // short cat_def_cnt;
5167 // char *defs[cls_def_cnt + cat_def_cnt];
5168 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005169 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005170 llvm::StructType::create("struct._objc_symtab",
5171 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5172 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005173 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005174
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005175 // struct _objc_module {
5176 // long version;
5177 // long size; // sizeof(struct _objc_module)
5178 // char *name;
5179 // struct _objc_symtab* symtab;
5180 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005181 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005182 llvm::StructType::create("struct._objc_module",
5183 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005184
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005185
Mike Stumpf5408fe2009-05-16 07:57:57 +00005186 // FIXME: This is the size of the setjmp buffer and should be target
5187 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005188 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005189
Anders Carlsson124526b2008-09-09 10:10:21 +00005190 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005191 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192
5193 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005194 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005195 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5196 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005197
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005198}
5199
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005200ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005201 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005202 // struct _method_list_t {
5203 // uint32_t entsize; // sizeof(struct _objc_method)
5204 // uint32_t method_count;
5205 // struct _objc_method method_list[method_count];
5206 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005207 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005208 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5209 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005210 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005211 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005213 // struct _protocol_t {
5214 // id isa; // NULL
5215 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005216 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005217 // const struct method_list_t * const instance_methods;
5218 // const struct method_list_t * const class_methods;
5219 // const struct method_list_t *optionalInstanceMethods;
5220 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005221 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005222 // const uint32_t size; // sizeof(struct _protocol_t)
5223 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005224 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005225 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005226
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005227 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005228 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005229 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005231 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005232 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5233 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5234 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5235 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005236 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5237 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005238
5239 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005240 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005241
Fariborz Jahanianda320092009-01-29 19:24:30 +00005242 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005243 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005244 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005245 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005246 ProtocolListnfABITy->setBody(LongTy,
5247 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5248 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005249
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005250 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005251 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005253 // struct _ivar_t {
5254 // unsigned long int *offset; // pointer to ivar offset location
5255 // char *name;
5256 // char *type;
5257 // uint32_t alignment;
5258 // uint32_t size;
5259 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005260 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005261 llvm::StructType::create("struct._ivar_t",
5262 llvm::PointerType::getUnqual(LongTy),
5263 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005264
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005265 // struct _ivar_list_t {
5266 // uint32 entsize; // sizeof(struct _ivar_t)
5267 // uint32 count;
5268 // struct _iver_t list[count];
5269 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005270 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005271 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5272 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005273
Owen Anderson96e0fc72009-07-29 22:16:19 +00005274 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005275
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005276 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005277 // uint32_t const flags;
5278 // uint32_t const instanceStart;
5279 // uint32_t const instanceSize;
5280 // uint32_t const reserved; // only when building for 64bit targets
5281 // const uint8_t * const ivarLayout;
5282 // const char *const name;
5283 // const struct _method_list_t * const baseMethods;
5284 // const struct _objc_protocol_list *const baseProtocols;
5285 // const struct _ivar_list_t *const ivars;
5286 // const uint8_t * const weakIvarLayout;
5287 // const struct _prop_list_t * const properties;
5288 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005289
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005290 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005291 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5292 IntTy, IntTy, IntTy, Int8PtrTy,
5293 Int8PtrTy, MethodListnfABIPtrTy,
5294 ProtocolListnfABIPtrTy,
5295 IvarListnfABIPtrTy,
5296 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005298 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005299 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005300 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5301 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005302
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005303 // struct _class_t {
5304 // struct _class_t *isa;
5305 // struct _class_t * const superclass;
5306 // void *cache;
5307 // IMP *vtable;
5308 // struct class_ro_t *ro;
5309 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005310
Chris Lattnerc1c20112011-08-12 17:43:31 +00005311 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005312 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5313 llvm::PointerType::getUnqual(ClassnfABITy),
5314 CachePtrTy,
5315 llvm::PointerType::getUnqual(ImpnfABITy),
5316 llvm::PointerType::getUnqual(ClassRonfABITy),
5317 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005319 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005320 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005322 // struct _category_t {
5323 // const char * const name;
5324 // struct _class_t *const cls;
5325 // const struct _method_list_t * const instance_methods;
5326 // const struct _method_list_t * const class_methods;
5327 // const struct _protocol_list_t * const protocols;
5328 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005329 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005330 CategorynfABITy = llvm::StructType::create("struct._category_t",
5331 Int8PtrTy, ClassnfABIPtrTy,
5332 MethodListnfABIPtrTy,
5333 MethodListnfABIPtrTy,
5334 ProtocolListnfABIPtrTy,
5335 PropertyListPtrTy,
5336 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005337
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005338 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005339 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5340 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005341
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005342 // MessageRefTy - LLVM for:
5343 // struct _message_ref_t {
5344 // IMP messenger;
5345 // SEL name;
5346 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005348 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005349 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005350 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005351 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005352 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005353 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005354 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005355 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005356 Ctx.getObjCSelType(), 0, 0, false,
5357 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005358 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005359
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005360 MessageRefCTy = Ctx.getTagDeclType(RD);
5361 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5362 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005363
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005364 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005365 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005366
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005367 // SuperMessageRefTy - LLVM for:
5368 // struct _super_message_ref_t {
5369 // SUPER_IMP messenger;
5370 // SEL name;
5371 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005372 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005373 llvm::StructType::create("struct._super_message_ref_t",
5374 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005375
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005376 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005377 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005378
Daniel Dunbare588b992009-03-01 04:46:24 +00005379
5380 // struct objc_typeinfo {
5381 // const void** vtable; // objc_ehtype_vtable + 2
5382 // const char* name; // c++ typeinfo string
5383 // Class cls;
5384 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005385 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005386 llvm::StructType::create("struct._objc_typeinfo",
5387 llvm::PointerType::getUnqual(Int8PtrTy),
5388 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005389 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005390}
5391
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005392llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005393 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005394
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005395 return NULL;
5396}
5397
Bill Wendlingbb028552012-02-07 09:25:09 +00005398void CGObjCNonFragileABIMac::
5399AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5400 const char *SymbolName,
5401 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005402 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403
Daniel Dunbar463b8762009-05-15 21:48:48 +00005404 if (!NumClasses)
5405 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005406
Chris Lattner0b239712012-02-06 22:16:34 +00005407 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005408 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005409 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005410 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005411 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005412 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005413 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005414 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005415
Daniel Dunbar463b8762009-05-15 21:48:48 +00005416 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005417 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005418 llvm::GlobalValue::InternalLinkage,
5419 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005420 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005421 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005422 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005423 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005424}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005426void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5427 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005428
Daniel Dunbar463b8762009-05-15 21:48:48 +00005429 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005430 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005431 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005432 "\01L_OBJC_LABEL_CLASS_$",
5433 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005434
Bill Wendling13562a12012-02-07 09:06:01 +00005435 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005436 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5437 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5438 continue;
5439 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005440 }
5441
Bill Wendling13562a12012-02-07 09:06:01 +00005442 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005443 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5444 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5445 continue;
5446 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5447 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005448
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005450 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5451 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005452
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005453 // Build list of all implemented category addresses in array
5454 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005456 "\01L_OBJC_LABEL_CATEGORY_$",
5457 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005458 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005459 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5460 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005462 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005463}
5464
John McCall944c8432011-05-14 03:10:52 +00005465/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5466/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005468/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005469bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5470 // At various points we've experimented with using vtable-based
5471 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005472 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005473 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005474 return false;
John McCall944c8432011-05-14 03:10:52 +00005475 case CodeGenOptions::NonLegacy:
5476 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005477 case CodeGenOptions::Mixed:
5478 break;
5479 }
5480
5481 // If so, see whether this selector is in the white-list of things which must
5482 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005483 if (VTableDispatchMethods.empty()) {
5484 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5485 VTableDispatchMethods.insert(GetNullarySelector("class"));
5486 VTableDispatchMethods.insert(GetNullarySelector("self"));
5487 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5488 VTableDispatchMethods.insert(GetNullarySelector("length"));
5489 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005490
John McCall944c8432011-05-14 03:10:52 +00005491 // These are vtable-based if GC is disabled.
5492 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005493 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005494 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5495 VTableDispatchMethods.insert(GetNullarySelector("release"));
5496 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5497 }
5498
5499 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5500 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5501 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5502 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5503 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5504 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5505 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5506
5507 // These are vtable-based if GC is enabled.
5508 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005509 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005510 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5511 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5512
5513 // "countByEnumeratingWithState:objects:count"
5514 IdentifierInfo *KeyIdents[] = {
5515 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5516 &CGM.getContext().Idents.get("objects"),
5517 &CGM.getContext().Idents.get("count")
5518 };
5519 VTableDispatchMethods.insert(
5520 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5521 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005522 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005523
John McCall944c8432011-05-14 03:10:52 +00005524 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005525}
5526
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005527/// BuildClassRoTInitializer - generate meta-data for:
5528/// struct _class_ro_t {
5529/// uint32_t const flags;
5530/// uint32_t const instanceStart;
5531/// uint32_t const instanceSize;
5532/// uint32_t const reserved; // only when building for 64bit targets
5533/// const uint8_t * const ivarLayout;
5534/// const char *const name;
5535/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005536/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005537/// const struct _ivar_list_t *const ivars;
5538/// const uint8_t * const weakIvarLayout;
5539/// const struct _prop_list_t * const properties;
5540/// }
5541///
5542llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005543 unsigned flags,
5544 unsigned InstanceStart,
5545 unsigned InstanceSize,
5546 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005547 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005548 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005549
David Blaikie4e4d0842012-03-11 07:00:24 +00005550 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005551 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005552
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005553 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5554 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5555 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005556 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005557 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5558 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005559 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005560 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005561 // const struct _method_list_t * const baseMethods;
5562 std::vector<llvm::Constant*> Methods;
5563 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005564 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005565 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005566 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005567 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005568 // Class methods should always be defined.
5569 Methods.push_back(GetMethodConstant(*i));
5570 }
5571 } else {
5572 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005574 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005575 // Instance methods should always be defined.
5576 Methods.push_back(GetMethodConstant(*i));
5577 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005578 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005579 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005580 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005581
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005582 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5583 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005584
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005585 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5586 if (llvm::Constant *C = GetMethodConstant(MD))
5587 Methods.push_back(C);
5588 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5589 if (llvm::Constant *C = GetMethodConstant(MD))
5590 Methods.push_back(C);
5591 }
5592 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005593 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005594 Values[ 5] = EmitMethodList(MethodListName,
5595 "__DATA, __objc_const", Methods);
5596
Fariborz Jahanianda320092009-01-29 19:24:30 +00005597 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5598 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005599 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005600 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005601 OID->all_referenced_protocol_begin(),
5602 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005603
John McCall621915c2012-10-17 04:53:23 +00005604 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005605 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005606 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005607 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005608 } else {
5609 Values[ 7] = EmitIvarList(ID);
5610 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005611 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5612 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005613 }
Owen Anderson08e25242009-07-27 22:29:56 +00005614 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005615 Values);
5616 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005617 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5618 llvm::GlobalValue::InternalLinkage,
5619 Init,
John McCall621915c2012-10-17 04:53:23 +00005620 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005621 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5622 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005623 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005624 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005625 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005626 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005627
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005628}
5629
5630/// BuildClassMetaData - This routine defines that to-level meta-data
5631/// for the given ClassName for:
5632/// struct _class_t {
5633/// struct _class_t *isa;
5634/// struct _class_t * const superclass;
5635/// void *cache;
5636/// IMP *vtable;
5637/// struct class_ro_t *ro;
5638/// }
5639///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005640llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005641 std::string &ClassName,
5642 llvm::Constant *IsAGV,
5643 llvm::Constant *SuperClassGV,
5644 llvm::Constant *ClassRoGV,
5645 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005646 llvm::Constant *Values[] = {
5647 IsAGV,
5648 SuperClassGV,
5649 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5650 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5651 ClassRoGV // &CLASS_RO_GV
5652 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005653 if (!Values[1])
5654 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005655 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005656 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005657 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5658 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005659 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005660 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005661 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005662 if (HiddenVisibility)
5663 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005664 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005665}
5666
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005668CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005669 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005670}
5671
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005672void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005673 uint32_t &InstanceStart,
5674 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005675 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005676 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005677
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005678 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005679 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005680
5681 // If there are no fields, the start is the same as the end.
5682 if (!RL.getFieldCount())
5683 InstanceStart = InstanceSize;
5684 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005685 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005686}
5687
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005688void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5689 std::string ClassName = ID->getNameAsString();
5690 if (!ObjCEmptyCacheVar) {
5691 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692 CGM.getModule(),
5693 ObjCTypes.CacheTy,
5694 false,
5695 llvm::GlobalValue::ExternalLinkage,
5696 0,
5697 "_objc_empty_cache");
5698
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005699 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005700 CGM.getModule(),
5701 ObjCTypes.ImpnfABITy,
5702 false,
5703 llvm::GlobalValue::ExternalLinkage,
5704 0,
5705 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005706 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005708 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005709 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005711 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005712 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005713 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005714 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5715 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005717 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005718
John McCallb03527a2012-10-17 04:53:31 +00005719 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005720 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005721 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005722 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005723 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005724
5725 // FIXME: why is this flag set on the metaclass?
5726 // ObjC metaclasses have no fields and don't really get constructed.
5727 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005728 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005729 if (!ID->hasNonZeroConstructors())
5730 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5731 }
5732
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005733 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005734 // class is root
John McCall621915c2012-10-17 04:53:23 +00005735 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005736 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005737 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005738 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005739 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005740 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5741 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5742 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005743 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005744 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005745 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005746 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005747 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005748 ObjCMetaClassName +
5749 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005750 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005751 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005752 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005753 }
5754 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5755 InstanceStart,
5756 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005757 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005758 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005759 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5760 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005761 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005762
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005763 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005764 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005765 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005766 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005767
5768 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005769 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005770
John McCallb03527a2012-10-17 04:53:31 +00005771 // Set a flag to enable a runtime optimization when a class has
5772 // fields that require destruction but which don't require
5773 // anything except zero-initialization during construction. This
5774 // is most notably true of __strong and __weak types, but you can
5775 // also imagine there being C++ types with non-trivial default
5776 // constructors that merely set all fields to null.
5777 if (!ID->hasNonZeroConstructors())
5778 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5779 }
5780
Douglas Gregor68584ed2009-06-18 16:11:24 +00005781 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005782 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005783
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005784 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005785 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005786 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005787 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005788 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005789 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005790 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005791 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005792 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005793 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005794 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005795 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005796 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005797 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005798 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005799 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005800
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005801 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005802 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005803 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5804 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005805 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005806
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005807 // Determine if this class is also "non-lazy".
5808 if (ImplementationIsNonLazy(ID))
5809 DefinedNonLazyClasses.push_back(ClassMD);
5810
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005811 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005812 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005813 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005814 // Make sure method definition entries are all clear for next implementation.
5815 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005816}
5817
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005818/// GenerateProtocolRef - This routine is called to generate code for
5819/// a protocol reference expression; as in:
5820/// @code
5821/// @protocol(Proto1);
5822/// @endcode
5823/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5824/// which will hold address of the protocol meta-data.
5825///
5826llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005827 const ObjCProtocolDecl *PD) {
5828
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005829 // This routine is called for @protocol only. So, we must build definition
5830 // of protocol's meta-data (not a reference to it!)
5831 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832 llvm::Constant *Init =
5833 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005834 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005835
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005836 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005837 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005838
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005839 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5840 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005841 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005842 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005843 CGM.getModule(),
5844 Init->getType(), false,
5845 llvm::GlobalValue::WeakAnyLinkage,
5846 Init,
5847 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005848 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5849 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005850 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005851 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005852}
5853
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005854/// GenerateCategory - Build metadata for a category implementation.
5855/// struct _category_t {
5856/// const char * const name;
5857/// struct _class_t *const cls;
5858/// const struct _method_list_t * const instance_methods;
5859/// const struct _method_list_t * const class_methods;
5860/// const struct _protocol_list_t * const protocols;
5861/// const struct _prop_list_t * const properties;
5862/// }
5863///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005864void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005865 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005866 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005867 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5868 "_$_" + OCD->getNameAsString());
5869 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005870 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005871
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005872 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005873 Values[0] = GetClassName(OCD->getIdentifier());
5874 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005875 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005876 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005877 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5878
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005879 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005880 std::vector<llvm::Constant*> Methods;
5881 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005882 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005883 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005884
5885 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005886 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005887 // Instance methods should always be defined.
5888 Methods.push_back(GetMethodConstant(*i));
5889 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005890
5891 Values[2] = EmitMethodList(MethodListName,
5892 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005893 Methods);
5894
5895 MethodListName = Prefix;
5896 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5897 OCD->getNameAsString();
5898 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005899 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005900 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005901 // Class methods should always be defined.
5902 Methods.push_back(GetMethodConstant(*i));
5903 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005904
5905 Values[3] = EmitMethodList(MethodListName,
5906 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005907 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005908 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005909 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005910 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005911 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005912 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5913 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005914 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005915 + Interface->getName() + "_$_"
5916 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005917 Category->protocol_begin(),
5918 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005919 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5920 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005921 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005922 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5923 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005924 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005925
5926 llvm::Constant *Init =
5927 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005928 Values);
5929 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005930 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005931 false,
5932 llvm::GlobalValue::InternalLinkage,
5933 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005934 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005935 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005936 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005937 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005938 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005939 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005940
5941 // Determine if this category is also "non-lazy".
5942 if (ImplementationIsNonLazy(OCD))
5943 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005944 // method definition entries must be clear for next implementation.
5945 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005946}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005947
5948/// GetMethodConstant - Return a struct objc_method constant for the
5949/// given method if it has been defined. The result is null if the
5950/// method has not been defined. The return value has type MethodPtrTy.
5951llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005952 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005953 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005954 if (!Fn)
5955 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005956
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005957 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005958 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005959 ObjCTypes.SelectorPtrTy),
5960 GetMethodVarType(MD),
5961 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5962 };
Owen Anderson08e25242009-07-27 22:29:56 +00005963 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005964}
5965
5966/// EmitMethodList - Build meta-data for method declarations
5967/// struct _method_list_t {
5968/// uint32_t entsize; // sizeof(struct _objc_method)
5969/// uint32_t method_count;
5970/// struct _objc_method method_list[method_count];
5971/// }
5972///
Bill Wendlingbb028552012-02-07 09:25:09 +00005973llvm::Constant *
5974CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5975 const char *Section,
5976 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005977 // Return null for empty list.
5978 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005979 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005980
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005981 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005982 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005983 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005984 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005985 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005986 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005987 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005988 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005989 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005990 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005991
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005992 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005993 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005994 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005995 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005996 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005997 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005998 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005999}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006000
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006001/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6002/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00006003llvm::GlobalVariable *
6004CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6005 const ObjCIvarDecl *Ivar) {
6006 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006007 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006008 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006009 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006010 CGM.getModule().getGlobalVariable(Name);
6011 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006012 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006013 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006014 false,
6015 llvm::GlobalValue::ExternalLinkage,
6016 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006017 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006018 return IvarOffsetGV;
6019}
6020
Daniel Dunbare83be122010-04-02 21:14:02 +00006021llvm::Constant *
6022CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6023 const ObjCIvarDecl *Ivar,
Eli Friedman38ad4f42012-11-06 01:51:20 +00006024 CharUnits Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006025 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006026 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedman38ad4f42012-11-06 01:51:20 +00006027 Offset.getQuantity()));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006028 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006029 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006030
Mike Stumpf5408fe2009-05-16 07:57:57 +00006031 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6032 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006033 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6034 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006035 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006036 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006037 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006038 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006039 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006040 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006041}
6042
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006043/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006044/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006045/// IvarListnfABIPtrTy.
6046/// struct _ivar_t {
6047/// unsigned long int *offset; // pointer to ivar offset location
6048/// char *name;
6049/// char *type;
6050/// uint32_t alignment;
6051/// uint32_t size;
6052/// }
6053/// struct _ivar_list_t {
6054/// uint32 entsize; // sizeof(struct _ivar_t)
6055/// uint32 count;
6056/// struct _iver_t list[count];
6057/// }
6058///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006059
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006060llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006061 const ObjCImplementationDecl *ID) {
6062
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006063 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006064
Jordy Rosedb8264e2011-07-22 02:08:32 +00006065 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006066 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006067
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006068 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006069
Jordy Rosedb8264e2011-07-22 02:08:32 +00006070 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006071 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006072 // Ignore unnamed bit-fields.
6073 if (!IVD->getDeclName())
6074 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006075 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006076 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006077 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006078 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6079 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006080 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006081 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006082 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006083 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006084 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006085 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006086 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006087 // NOTE. Size of a bitfield does not match gcc's, because of the
6088 // way bitfields are treated special in each. But I am told that
6089 // 'size' for bitfield ivars is ignored by the runtime so it does
6090 // not matter. If it matters, there is enough info to get the
6091 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006092 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006093 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006094 }
6095 // Return null for empty list.
6096 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006097 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006098
6099 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006100 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006101 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6102 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006103 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006104 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006105 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006106 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006107 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6108 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006109 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006110 llvm::GlobalValue::InternalLinkage,
6111 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006112 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006113 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006114 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006115 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006116
Chris Lattnerad64e022009-07-17 23:57:13 +00006117 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006118 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006119}
6120
6121llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006122 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006123 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006124
Fariborz Jahanianda320092009-01-29 19:24:30 +00006125 if (!Entry) {
6126 // We use the initializer as a marker of whether this is a forward
6127 // reference or not. At module finalization we add the empty
6128 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006129 Entry =
6130 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6131 llvm::GlobalValue::ExternalLinkage,
6132 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006133 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006134 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006135 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006136
Fariborz Jahanianda320092009-01-29 19:24:30 +00006137 return Entry;
6138}
6139
6140/// GetOrEmitProtocol - Generate the protocol meta-data:
6141/// @code
6142/// struct _protocol_t {
6143/// id isa; // NULL
6144/// const char * const protocol_name;
6145/// const struct _protocol_list_t * protocol_list; // super protocols
6146/// const struct method_list_t * const instance_methods;
6147/// const struct method_list_t * const class_methods;
6148/// const struct method_list_t *optionalInstanceMethods;
6149/// const struct method_list_t *optionalClassMethods;
6150/// const struct _prop_list_t * properties;
6151/// const uint32_t size; // sizeof(struct _protocol_t)
6152/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006153/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006154/// }
6155/// @endcode
6156///
6157
6158llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006159 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006160 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006161
Fariborz Jahanianda320092009-01-29 19:24:30 +00006162 // Early exit if a defining object has already been generated.
6163 if (Entry && Entry->hasInitializer())
6164 return Entry;
6165
Douglas Gregor1d784b22012-01-01 19:51:50 +00006166 // Use the protocol definition, if there is one.
6167 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6168 PD = Def;
6169
Fariborz Jahanianda320092009-01-29 19:24:30 +00006170 // Construct method lists.
6171 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6172 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006173 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006174 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006175 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006176 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006177 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006178 if (!C)
6179 return GetOrEmitProtocolRef(PD);
6180
Fariborz Jahanianda320092009-01-29 19:24:30 +00006181 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6182 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006183 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006184 } else {
6185 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006186 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006187 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006188 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006189
6190 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006191 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006192 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006193 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006194 if (!C)
6195 return GetOrEmitProtocolRef(PD);
6196
Fariborz Jahanianda320092009-01-29 19:24:30 +00006197 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6198 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006199 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006200 } else {
6201 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006202 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006203 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006204 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006205
Bob Wilsondc8dab62011-11-30 01:57:58 +00006206 MethodTypesExt.insert(MethodTypesExt.end(),
6207 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6208
6209 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006210 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006211 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006212 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006213 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6214 PD->protocol_begin(),
6215 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006216
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006217 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006218 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006219 "__DATA, __objc_const",
6220 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006221 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006222 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006223 "__DATA, __objc_const",
6224 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006225 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006226 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006227 "__DATA, __objc_const",
6228 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006229 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006230 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006231 "__DATA, __objc_const",
6232 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006233 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006234 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006235 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006236 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006237 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006238 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006239 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6240 + PD->getName(),
6241 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006242 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006243 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006244
Fariborz Jahanianda320092009-01-29 19:24:30 +00006245 if (Entry) {
6246 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006247 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006248 Entry->setInitializer(Init);
6249 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006250 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006251 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6252 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6253 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006254 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006255 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006256 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006257
6258 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006259 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006260 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006261 CGM.AddUsedGlobal(Entry);
6262
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006263 // Use this protocol meta-data to build protocol list table in section
6264 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006265 llvm::GlobalVariable *PTGV =
6266 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6267 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6268 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006269 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006270 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006271 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006272 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006273 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006274 return Entry;
6275}
6276
6277/// EmitProtocolList - Generate protocol list meta-data:
6278/// @code
6279/// struct _protocol_list_t {
6280/// long protocol_count; // Note, this is 32/64 bit
6281/// struct _protocol_t[protocol_count];
6282/// }
6283/// @endcode
6284///
6285llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006286CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006287 ObjCProtocolDecl::protocol_iterator begin,
6288 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00006289 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006290
Fariborz Jahanianda320092009-01-29 19:24:30 +00006291 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006292 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006293 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006294
Daniel Dunbar948e2582009-02-15 07:36:20 +00006295 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006296 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006297 Name.toVector(TmpName);
6298 llvm::GlobalVariable *GV =
6299 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006300 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006301 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006302
Daniel Dunbar948e2582009-02-15 07:36:20 +00006303 for (; begin != end; ++begin)
6304 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6305
Fariborz Jahanianda320092009-01-29 19:24:30 +00006306 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006307 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006308 ObjCTypes.ProtocolnfABIPtrTy));
6309
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006310 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006311 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006312 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006313 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006314 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6315 ProtocolRefs.size()),
6316 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006317
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006318 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006319 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006320 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006321 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006322 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006323 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006324 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006325 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006326 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006327 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006328}
6329
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006330/// GetMethodDescriptionConstant - This routine build following meta-data:
6331/// struct _objc_method {
6332/// SEL _cmd;
6333/// char *method_type;
6334/// char *_imp;
6335/// }
6336
6337llvm::Constant *
6338CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006339 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006340 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006341 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6342 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006343 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006344 if (!Desc[1])
6345 return 0;
6346
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006347 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006348 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006349 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006350}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006351
6352/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6353/// This code gen. amounts to generating code for:
6354/// @code
6355/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6356/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006357///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006358LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006359 CodeGen::CodeGenFunction &CGF,
6360 QualType ObjectTy,
6361 llvm::Value *BaseValue,
6362 const ObjCIvarDecl *Ivar,
6363 unsigned CVRQualifiers) {
6364 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006365 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6366 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6367 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6368 llvm::MDNode::get(VMContext,
6369 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006370 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006371 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006372}
6373
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006374llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006375 CodeGen::CodeGenFunction &CGF,
6376 const ObjCInterfaceDecl *Interface,
6377 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006378 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006379}
6380
John McCallb1e81442011-05-13 23:16:18 +00006381static void appendSelectorForMessageRefTable(std::string &buffer,
6382 Selector selector) {
6383 if (selector.isUnarySelector()) {
6384 buffer += selector.getNameForSlot(0);
6385 return;
6386 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006387
John McCallb1e81442011-05-13 23:16:18 +00006388 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6389 buffer += selector.getNameForSlot(i);
6390 buffer += '_';
6391 }
6392}
6393
John McCall944c8432011-05-14 03:10:52 +00006394/// Emit a "v-table" message send. We emit a weak hidden-visibility
6395/// struct, initially containing the selector pointer and a pointer to
6396/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6397/// load and call the function pointer, passing the address of the
6398/// struct as the second parameter. The runtime determines whether
6399/// the selector is currently emitted using vtable dispatch; if so, it
6400/// substitutes a stub function which simply tail-calls through the
6401/// appropriate vtable slot, and if not, it substitues a stub function
6402/// which tail-calls objc_msgSend. Both stubs adjust the selector
6403/// argument to correctly point to the selector.
6404RValue
6405CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6406 ReturnValueSlot returnSlot,
6407 QualType resultType,
6408 Selector selector,
6409 llvm::Value *arg0,
6410 QualType arg0Type,
6411 bool isSuper,
6412 const CallArgList &formalArgs,
6413 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006414 // Compute the actual arguments.
6415 CallArgList args;
6416
John McCall944c8432011-05-14 03:10:52 +00006417 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006418 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006419 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6420 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006421
John McCall944c8432011-05-14 03:10:52 +00006422 // Second argument: a pointer to the message ref structure. Leave
6423 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006424 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6425
6426 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6427
John McCallde5d3c72012-02-17 03:33:10 +00006428 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006429
John McCallcba681a2011-05-14 21:12:11 +00006430 NullReturnState nullReturn;
6431
John McCall944c8432011-05-14 03:10:52 +00006432 // Find the function to call and the mangled name for the message
6433 // ref structure. Using a different mangled name wouldn't actually
6434 // be a problem; it would just be a waste.
6435 //
6436 // The runtime currently never uses vtable dispatch for anything
6437 // except normal, non-super message-sends.
6438 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006439 llvm::Constant *fn = 0;
6440 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006441 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006442 if (isSuper) {
6443 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6444 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006445 } else {
John McCallcba681a2011-05-14 21:12:11 +00006446 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006447 fn = ObjCTypes.getMessageSendStretFixupFn();
6448 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006449 }
John McCallb1e81442011-05-13 23:16:18 +00006450 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6451 fn = ObjCTypes.getMessageSendFpretFixupFn();
6452 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006453 } else {
John McCallb1e81442011-05-13 23:16:18 +00006454 if (isSuper) {
6455 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6456 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006457 } else {
John McCallb1e81442011-05-13 23:16:18 +00006458 fn = ObjCTypes.getMessageSendFixupFn();
6459 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006460 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006461 }
John McCallb1e81442011-05-13 23:16:18 +00006462 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6463 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006464
John McCallb1e81442011-05-13 23:16:18 +00006465 // Append the selector name, except use underscores anywhere we
6466 // would have used colons.
6467 appendSelectorForMessageRefTable(messageRefName, selector);
6468
6469 llvm::GlobalVariable *messageRef
6470 = CGM.getModule().getGlobalVariable(messageRefName);
6471 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006472 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006473 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006474 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006475 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6476 init->getType(),
6477 /*constant*/ false,
6478 llvm::GlobalValue::WeakAnyLinkage,
6479 init,
6480 messageRefName);
6481 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6482 messageRef->setAlignment(16);
6483 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6484 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006485
6486 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006487 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006488 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6489 e = method->param_end(); i != e; ++i) {
6490 const ParmVarDecl *ParamDecl = (*i);
6491 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6492 if (!nullReturn.NullBB)
6493 nullReturn.init(CGF, arg0);
6494 requiresnullCheck = true;
6495 break;
6496 }
6497 }
6498
John McCallb1e81442011-05-13 23:16:18 +00006499 llvm::Value *mref =
6500 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6501
John McCall944c8432011-05-14 03:10:52 +00006502 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006503 args[1].RV = RValue::get(mref);
6504
6505 // Load the function to call from the message ref table.
6506 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6507 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6508
John McCallde5d3c72012-02-17 03:33:10 +00006509 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006510
John McCallde5d3c72012-02-17 03:33:10 +00006511 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006512 return nullReturn.complete(CGF, result, resultType, formalArgs,
6513 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006514}
6515
6516/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006517CodeGen::RValue
6518CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006519 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006520 QualType ResultType,
6521 Selector Sel,
6522 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006523 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006524 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006525 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006526 return isVTableDispatchedSelector(Sel)
6527 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006528 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006529 false, CallArgs, Method)
6530 : EmitMessageSend(CGF, Return, ResultType,
6531 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006532 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006533 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006534}
6535
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006536llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006537CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006538 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6539
Daniel Dunbardfff2302009-03-02 05:18:14 +00006540 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006541 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006542 false, llvm::GlobalValue::ExternalLinkage,
6543 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006544 }
6545
6546 return GV;
6547}
6548
John McCallf85e1932011-06-15 23:02:42 +00006549llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6550 IdentifierInfo *II) {
6551 llvm::GlobalVariable *&Entry = ClassReferences[II];
6552
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006553 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006554 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006555 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006556 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006557 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6558 false, llvm::GlobalValue::InternalLinkage,
6559 ClassGV,
6560 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006561 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006562 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006563 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006564 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006565 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006566 }
John McCallf85e1932011-06-15 23:02:42 +00006567
Benjamin Kramer578faa82011-09-27 21:06:10 +00006568 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006569}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006570
John McCallf85e1932011-06-15 23:02:42 +00006571llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6572 const ObjCInterfaceDecl *ID) {
6573 return EmitClassRefFromId(Builder, ID->getIdentifier());
6574}
6575
6576llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6577 CGBuilderTy &Builder) {
6578 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6579 return EmitClassRefFromId(Builder, II);
6580}
6581
Daniel Dunbar11394522009-04-18 08:51:00 +00006582llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006583CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006584 const ObjCInterfaceDecl *ID) {
6585 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006586
Daniel Dunbar11394522009-04-18 08:51:00 +00006587 if (!Entry) {
6588 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6589 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006590 Entry =
6591 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006592 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006593 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006594 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006595 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006596 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006597 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006598 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006599 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006600 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006601
Benjamin Kramer578faa82011-09-27 21:06:10 +00006602 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006603}
6604
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006605/// EmitMetaClassRef - Return a Value * of the address of _class_t
6606/// meta-data
6607///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006608llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6609 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006610 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6611 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006612 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006613
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006614 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006615 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006616 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006617 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006618 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006619 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006620 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006621 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006622 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006623 ObjCTypes.ClassnfABIPtrTy));
6624
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006625 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006626 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006627
Benjamin Kramer578faa82011-09-27 21:06:10 +00006628 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006629}
6630
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006631/// GetClass - Return a reference to the class for the given interface
6632/// decl.
6633llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6634 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006635 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006636 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6637 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6638 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6639 }
6640
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006641 return EmitClassRef(Builder, ID);
6642}
6643
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006644/// Generates a message send where the super is the receiver. This is
6645/// a message send to self with special delivery semantics indicating
6646/// which class's method should be called.
6647CodeGen::RValue
6648CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006649 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006650 QualType ResultType,
6651 Selector Sel,
6652 const ObjCInterfaceDecl *Class,
6653 bool isCategoryImpl,
6654 llvm::Value *Receiver,
6655 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006656 const CodeGen::CallArgList &CallArgs,
6657 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006658 // ...
6659 // Create and init a super structure; this is a (receiver, class)
6660 // pair we will pass to objc_msgSendSuper.
6661 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006662 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006663
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006664 llvm::Value *ReceiverAsObject =
6665 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6666 CGF.Builder.CreateStore(ReceiverAsObject,
6667 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006668
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006669 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006670 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006671 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006672 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006673 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006674 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006675
Mike Stumpf5408fe2009-05-16 07:57:57 +00006676 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6677 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006678 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006679 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6680 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6681 CGF.Builder.CreateStore(Target,
6682 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006683
John McCall944c8432011-05-14 03:10:52 +00006684 return (isVTableDispatchedSelector(Sel))
6685 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006686 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006687 true, CallArgs, Method)
6688 : EmitMessageSend(CGF, Return, ResultType,
6689 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006690 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006691 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006692}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006693
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006694llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006695 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006696 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006697
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006698 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006699 llvm::Constant *Casted =
6700 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6701 ObjCTypes.SelectorPtrTy);
6702 Entry =
6703 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6704 llvm::GlobalValue::InternalLinkage,
6705 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006706 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006707 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006708 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006709
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006710 if (lval)
6711 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006712 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6713
6714 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6715 llvm::MDNode::get(VMContext,
6716 ArrayRef<llvm::Value*>()));
6717 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006718}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006719/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006720/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006721///
6722void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006723 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006724 llvm::Value *dst,
6725 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006726 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006727 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006728 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006729 assert(Size <= 8 && "does not support size > 8");
6730 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6731 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006732 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6733 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006734 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6735 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006736 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6737 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006738 return;
6739}
6740
6741/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6742/// objc_assign_strongCast (id src, id *dst)
6743///
6744void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006745 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006746 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006747 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006748 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006749 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006750 assert(Size <= 8 && "does not support size > 8");
6751 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006752 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006753 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6754 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006755 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6756 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006757 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006758 src, dst, "weakassign");
6759 return;
6760}
6761
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006762void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006763 CodeGen::CodeGenFunction &CGF,
6764 llvm::Value *DestPtr,
6765 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006766 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006767 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6768 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006769 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006770 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006771 return;
6772}
6773
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006774/// EmitObjCWeakRead - Code gen for loading value of a __weak
6775/// object: objc_read_weak (id *src)
6776///
6777llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006778 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006779 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006780 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006781 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6782 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006783 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006784 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006785 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006786 return read_weak;
6787}
6788
6789/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6790/// objc_assign_weak (id src, id *dst)
6791///
6792void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006793 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006794 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006795 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006796 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006797 assert(Size <= 8 && "does not support size > 8");
6798 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6799 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006800 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6801 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006802 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6803 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006804 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006805 src, dst, "weakassign");
6806 return;
6807}
6808
6809/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6810/// objc_assign_global (id src, id *dst)
6811///
6812void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006813 llvm::Value *src, llvm::Value *dst,
6814 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006815 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006816 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006817 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006818 assert(Size <= 8 && "does not support size > 8");
6819 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6820 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006821 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6822 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006823 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6824 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006825 if (!threadlocal)
6826 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6827 src, dst, "globalassign");
6828 else
6829 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6830 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006831 return;
6832}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006833
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006834void
John McCallf1549f62010-07-06 01:34:17 +00006835CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6836 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006837 EmitAtSynchronizedStmt(CGF, S,
6838 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6839 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006840}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006841
John McCall5a180392010-07-24 00:37:23 +00006842llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006843CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006844 // There's a particular fixed type info for 'id'.
6845 if (T->isObjCIdType() ||
6846 T->isObjCQualifiedIdType()) {
6847 llvm::Constant *IDEHType =
6848 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6849 if (!IDEHType)
6850 IDEHType =
6851 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6852 false,
6853 llvm::GlobalValue::ExternalLinkage,
6854 0, "OBJC_EHTYPE_id");
6855 return IDEHType;
6856 }
6857
6858 // All other types should be Objective-C interface pointer types.
6859 const ObjCObjectPointerType *PT =
6860 T->getAs<ObjCObjectPointerType>();
6861 assert(PT && "Invalid @catch type.");
6862 const ObjCInterfaceType *IT = PT->getInterfaceType();
6863 assert(IT && "Invalid @catch type.");
6864 return GetInterfaceEHType(IT->getDecl(), false);
6865}
6866
John McCallf1549f62010-07-06 01:34:17 +00006867void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6868 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006869 EmitTryCatchStmt(CGF, S,
6870 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6871 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6872 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006873}
6874
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006875/// EmitThrowStmt - Generate code for a throw statement.
6876void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6877 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006878 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006879 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006880 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006881 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006882 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006883 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006884 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006885 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006886 }
6887
John McCall7ec404c2010-10-16 08:21:07 +00006888 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006889 CGF.Builder.ClearInsertionPoint();
6890}
Daniel Dunbare588b992009-03-01 04:46:24 +00006891
John McCall5a180392010-07-24 00:37:23 +00006892llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006893CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006894 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006895 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006896
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006897 // If we don't need a definition, return the entry if found or check
6898 // if we use an external reference.
6899 if (!ForDefinition) {
6900 if (Entry)
6901 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006902
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006903 // If this type (or a super class) has the __objc_exception__
6904 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006905 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006906 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006907 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006908 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006909 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006910 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006911 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006912 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006913
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006914 // Otherwise we need to either make a new entry or fill in the
6915 // initializer.
6916 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006917 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006918 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006919 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006920 CGM.getModule().getGlobalVariable(VTableName);
6921 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006922 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6923 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006924 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006925 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006926
Chris Lattner8b418682012-02-07 00:39:47 +00006927 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006928
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006929 llvm::Constant *Values[] = {
6930 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6931 GetClassName(ID->getIdentifier()),
6932 GetClassGlobal(ClassName)
6933 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006934 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006935 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006936
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006937 if (Entry) {
6938 Entry->setInitializer(Init);
6939 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006940 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006941 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006942 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006943 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006944 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006945 }
6946
David Blaikie4e4d0842012-03-11 07:00:24 +00006947 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006948 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006949 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006950 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006951
6952 if (ForDefinition) {
6953 Entry->setSection("__DATA,__objc_const");
6954 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6955 } else {
6956 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6957 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006958
6959 return Entry;
6960}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006961
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006962/* *** */
6963
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006964CodeGen::CGObjCRuntime *
6965CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006966 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6967 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006968 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006969
6970 case ObjCRuntime::MacOSX:
6971 case ObjCRuntime::iOS:
6972 return new CGObjCNonFragileABIMac(CGM);
6973
David Chisnall11d3f4c2012-07-03 20:49:52 +00006974 case ObjCRuntime::GNUstep:
6975 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006976 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006977 llvm_unreachable("these runtimes are not Mac runtimes");
6978 }
6979 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006980}