blob: acf4f2a0219872bc79afc1b1469f4729ff1d2106 [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:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000739 unsigned ivar_bytepos;
740 unsigned ivar_size;
741 GC_IVAR(unsigned bytepos = 0, 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,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936 unsigned int BytePos, bool ForStrongLayout,
937 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,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000942 unsigned int 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,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001327 unsigned long int 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.
1896 IvarsInfo.push_back(GC_IVAR(0, 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
1918 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1919
1920 // __block variables are passed by their descriptor address.
1921 if (ci->isByRef()) {
1922 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001923 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001924 }
1925
1926 assert(!type->isArrayType() && "array variable should not be caught");
1927 if (const RecordType *record = type->getAs<RecordType>()) {
1928 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001929 continue;
1930 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001931
John McCall6b5a61b2011-02-07 10:33:21 +00001932 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1933 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1934
1935 if (GCAttr == Qualifiers::Strong)
1936 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1937 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001938 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001939 SkipIvars.push_back(GC_IVAR(fieldOffset,
1940 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001941 }
1942
1943 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001944 return nullPtr;
1945
1946 // Sort on byte position; captures might not be allocated in order,
1947 // and unions can do funny things.
1948 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1949 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001950
1951 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001952 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001954 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001955 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001956 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001957 if (!(s[i] & 0xf0))
1958 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1959 else
1960 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1961 printf("\n");
1962 }
1963
1964 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001965}
1966
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001967/// getBlockCaptureLifetime - This routine returns life time of the captured
1968/// block variable for the purpose of block layout meta-data generation. FQT is
1969/// the type of the variable captured in the block.
1970Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT) {
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001971 if (CGM.getLangOpts().ObjCAutoRefCount)
1972 return FQT.getObjCLifetime();
1973
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001974 // MRR.
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001975 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001976 return Qualifiers::OCL_ExplicitNone;
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001977
1978 return Qualifiers::OCL_None;
1979}
1980
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001981void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1982 Qualifiers::ObjCLifetime LifeTime,
1983 unsigned FieldOffset,
1984 unsigned FieldSize) {
1985 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
1986 unsigned FieldSizeInBytes = FieldSize/ByteSizeInBits;
1987
1988 // __block variables are passed by their descriptor address.
1989 if (IsByref)
1990 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
1991 FieldSizeInBytes));
1992 else if (LifeTime == Qualifiers::OCL_Strong)
1993 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
1994 FieldSizeInBytes));
1995 else if (LifeTime == Qualifiers::OCL_Weak)
1996 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
1997 FieldSizeInBytes));
1998 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
1999 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
2000 FieldSizeInBytes));
2001 else
2002 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2003 FieldOffset,
2004 FieldSizeInBytes));
2005}
2006
2007void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2008 const RecordDecl *RD,
2009 ArrayRef<const FieldDecl*> RecFields,
2010 unsigned int BytePos, bool &HasUnion) {
2011 bool IsUnion = (RD && RD->isUnion());
2012 uint64_t MaxUnionSize = 0;
2013 const FieldDecl *MaxField = 0;
2014 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
2015 uint64_t MaxFieldOffset = 0;
2016 uint64_t LastBitfieldOrUnnamedOffset = 0;
2017
2018 if (RecFields.empty())
2019 return;
2020 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2021
2022 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2023 const FieldDecl *Field = RecFields[i];
2024 uint64_t FieldOffset;
2025 // Note that 'i' here is actually the field index inside RD of Field,
2026 // although this dependency is hidden.
2027 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2028 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits);
2029
2030 // Skip over unnamed or bitfields
2031 if (!Field->getIdentifier() || Field->isBitField()) {
2032 LastFieldBitfieldOrUnnamed = Field;
2033 LastBitfieldOrUnnamedOffset = FieldOffset;
2034 continue;
2035 }
2036
2037 LastFieldBitfieldOrUnnamed = 0;
2038 QualType FQT = Field->getType();
2039 if (FQT->isRecordType() || FQT->isUnionType()) {
2040 if (FQT->isUnionType())
2041 HasUnion = true;
2042
2043 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2044 BytePos + FieldOffset, HasUnion);
2045 continue;
2046 }
2047
2048 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2049 const ConstantArrayType *CArray =
2050 dyn_cast_or_null<ConstantArrayType>(Array);
2051 uint64_t ElCount = CArray->getSize().getZExtValue();
2052 assert(CArray && "only array with known element size is supported");
2053 FQT = CArray->getElementType();
2054 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2055 const ConstantArrayType *CArray =
2056 dyn_cast_or_null<ConstantArrayType>(Array);
2057 ElCount *= CArray->getSize().getZExtValue();
2058 FQT = CArray->getElementType();
2059 }
2060
2061 assert(!FQT->isUnionType() &&
2062 "layout for array of unions not supported");
2063 if (FQT->isRecordType() && ElCount) {
2064 int OldIndex = RunSkipBlockVars.size() - 1;
2065 const RecordType *RT = FQT->getAs<RecordType>();
2066 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2067 HasUnion);
2068
2069 // Replicate layout information for each array element. Note that
2070 // one element is already done.
2071 uint64_t ElIx = 1;
2072 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
2073 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
2074 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2075 RunSkipBlockVars.push_back(
2076 RUN_SKIP(RunSkipBlockVars[i].opcode,
2077 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2078 RunSkipBlockVars[i].block_var_size));
2079 }
2080 continue;
2081 }
2082 }
2083 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
2084 if (IsUnion) {
2085 uint64_t UnionIvarSize = FieldSize;
2086 if (UnionIvarSize > MaxUnionSize) {
2087 MaxUnionSize = UnionIvarSize;
2088 MaxField = Field;
2089 MaxFieldOffset = FieldOffset;
2090 }
2091 } else {
2092 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002093 getBlockCaptureLifetime(FQT),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002094 BytePos + FieldOffset,
2095 FieldSize);
2096 }
2097 }
2098
2099 if (LastFieldBitfieldOrUnnamed) {
2100 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2101 // Last field was a bitfield. Must update the info.
2102 uint64_t BitFieldSize
2103 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
2104 unsigned Size = (BitFieldSize / ByteSizeInBits) +
2105 ((BitFieldSize % ByteSizeInBits) != 0);
2106 Size += LastBitfieldOrUnnamedOffset;
2107 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002108 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002109 BytePos + LastBitfieldOrUnnamedOffset,
2110 Size*ByteSizeInBits);
2111 } else {
2112 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2113 // Last field was unnamed. Must update skip info.
2114 unsigned FieldSize
2115 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
2116 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002117 getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002118 BytePos + LastBitfieldOrUnnamedOffset,
2119 FieldSize);
2120 }
2121 }
2122
2123 if (MaxField)
2124 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002125 getBlockCaptureLifetime(MaxField->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002126 BytePos + MaxFieldOffset,
2127 MaxUnionSize);
2128}
2129
2130void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
2131 unsigned int BytePos,
2132 bool &HasUnion) {
2133 const RecordDecl *RD = RT->getDecl();
2134 SmallVector<const FieldDecl*, 16> Fields;
2135 for (RecordDecl::field_iterator i = RD->field_begin(),
2136 e = RD->field_end(); i != e; ++i)
2137 Fields.push_back(*i);
2138 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2139 const llvm::StructLayout *RecLayout =
2140 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2141
2142 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion);
2143}
2144
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002145/// InlineLayoutInstruction - This routine produce an inline instruction for the
2146/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2147/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2148/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2149/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2150/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2151/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2152/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2153/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2154uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2155 SmallVectorImpl<unsigned char> &Layout) {
2156 uint64_t Result = 0;
2157 if (Layout.size() <= 3) {
2158 unsigned size = Layout.size();
2159 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2160 unsigned char inst;
2161 enum BLOCK_LAYOUT_OPCODE opcode ;
2162 switch (size) {
2163 case 3:
2164 inst = Layout[0];
2165 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2166 if (opcode == BLOCK_LAYOUT_STRONG)
2167 strong_word_count = (inst & 0xF)+1;
2168 else
2169 return 0;
2170 inst = Layout[1];
2171 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2172 if (opcode == BLOCK_LAYOUT_BYREF)
2173 byref_word_count = (inst & 0xF)+1;
2174 else
2175 return 0;
2176 inst = Layout[2];
2177 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2178 if (opcode == BLOCK_LAYOUT_WEAK)
2179 weak_word_count = (inst & 0xF)+1;
2180 else
2181 return 0;
2182 break;
2183
2184 case 2:
2185 inst = Layout[0];
2186 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2187 if (opcode == BLOCK_LAYOUT_STRONG) {
2188 strong_word_count = (inst & 0xF)+1;
2189 inst = Layout[1];
2190 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2191 if (opcode == BLOCK_LAYOUT_BYREF)
2192 byref_word_count = (inst & 0xF)+1;
2193 else if (opcode == BLOCK_LAYOUT_WEAK)
2194 weak_word_count = (inst & 0xF)+1;
2195 else
2196 return 0;
2197 }
2198 else if (opcode == BLOCK_LAYOUT_BYREF) {
2199 byref_word_count = (inst & 0xF)+1;
2200 inst = Layout[1];
2201 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2202 if (opcode == BLOCK_LAYOUT_WEAK)
2203 weak_word_count = (inst & 0xF)+1;
2204 else
2205 return 0;
2206 }
2207 else
2208 return 0;
2209 break;
2210
2211 case 1:
2212 inst = Layout[0];
2213 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2214 if (opcode == BLOCK_LAYOUT_STRONG)
2215 strong_word_count = (inst & 0xF)+1;
2216 else if (opcode == BLOCK_LAYOUT_BYREF)
2217 byref_word_count = (inst & 0xF)+1;
2218 else if (opcode == BLOCK_LAYOUT_WEAK)
2219 weak_word_count = (inst & 0xF)+1;
2220 else
2221 return 0;
2222 break;
2223
2224 default:
2225 return 0;
2226 }
2227
2228 // Cannot inline when any of the word counts is 15. Because this is one less
2229 // than the actual work count (so 15 means 16 actual word counts),
2230 // and we can only display 0 thru 15 word counts.
2231 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2232 return 0;
2233
2234 unsigned count =
2235 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2236
2237 if (size == count) {
2238 if (strong_word_count)
2239 Result = strong_word_count;
2240 Result <<= 4;
2241 if (byref_word_count)
2242 Result += byref_word_count;
2243 Result <<= 4;
2244 if (weak_word_count)
2245 Result += weak_word_count;
2246 }
2247 }
2248 return Result;
2249}
2250
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002251llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2252 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002253 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2254
2255 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2256
2257 RunSkipBlockVars.clear();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002258 bool hasUnion = false;
2259
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002260 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2261 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2262 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2263
2264 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2265
2266 // Calculate the basic layout of the block structure.
2267 const llvm::StructLayout *layout =
2268 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2269
2270 // Ignore the optional 'this' capture: C++ objects are not assumed
2271 // to be GC'ed.
2272
2273 // Walk the captured variables.
2274 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2275 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2276 const VarDecl *variable = ci->getVariable();
2277 QualType type = variable->getType();
2278
2279 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2280
2281 // Ignore constant captures.
2282 if (capture.isConstant()) continue;
2283
2284 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
2285
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002286 assert(!type->isArrayType() && "array variable should not be caught");
2287 if (const RecordType *record = type->getAs<RecordType>()) {
2288 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002289 continue;
2290 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002291 unsigned fieldSize = ci->isByRef() ? WordSizeInBits
2292 : CGM.getContext().getTypeSize(type);
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002293 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002294 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002295 }
2296
2297 if (RunSkipBlockVars.empty())
2298 return nullPtr;
2299
2300 // Sort on byte position; captures might not be allocated in order,
2301 // and unions can do funny things.
2302 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002303 SmallVector<unsigned char, 16> Layout;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002304
2305 unsigned size = RunSkipBlockVars.size();
2306 unsigned int shift = (WordSizeInBytes == 8) ? 3 : 2;
2307 unsigned int mask = (WordSizeInBytes == 8) ? 0x7 : 0x3;
2308 for (unsigned i = 0; i < size; i++) {
2309 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2310 unsigned start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2311 unsigned end_byte_pos = start_byte_pos;
2312 unsigned j = i+1;
2313 while (j < size) {
2314 if (opcode == RunSkipBlockVars[j].opcode) {
2315 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2316 i++;
2317 }
2318 else
2319 break;
2320 }
2321 unsigned size_in_bytes =
2322 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002323 if (j < size) {
2324 unsigned gap =
2325 RunSkipBlockVars[j].block_var_bytepos -
2326 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2327 size_in_bytes += gap;
2328 }
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002329 unsigned residue_in_bytes = 0;
2330 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2331 residue_in_bytes = size_in_bytes & mask;
2332 size_in_bytes -= residue_in_bytes;
2333 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2334 }
2335
2336 unsigned size_in_words = size_in_bytes >> shift;
2337 while (size_in_words >= 16) {
2338 // Note that value in imm. is one less that the actual
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002339 // value. So, 0xf means 16 words follow!
2340 unsigned char inst = (opcode << 4) | 0xf;
2341 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002342 size_in_words -= 16;
2343 }
2344 if (size_in_words > 0) {
2345 // Note that value in imm. is one less that the actual
2346 // value. So, we subtract 1 away!
2347 unsigned char inst = (opcode << 4) | (size_in_words-1);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002348 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002349 }
2350 if (residue_in_bytes > 0) {
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002351 unsigned char inst =
2352 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes-1);
2353 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002354 }
2355 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002356
2357 int e = Layout.size()-1;
2358 while (e >= 0) {
2359 unsigned char inst = Layout[e--];
2360 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2361 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2362 Layout.pop_back();
2363 else
2364 break;
2365 }
2366
2367 uint64_t Result = InlineLayoutInstruction(Layout);
2368 if (Result != 0) {
2369 // Block variable layout instruction has been inlined.
2370 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2371 printf("\n Inline instruction for block variable layout: ");
Matt Beaumont-Gay00a85412012-11-01 20:26:42 +00002372 printf("0x0%llx\n", (unsigned long long)Result);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002373 }
2374 if (WordSizeInBytes == 8) {
2375 const llvm::APInt Instruction(64, Result);
2376 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2377 }
2378 else {
2379 const llvm::APInt Instruction(32, Result);
2380 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2381 }
2382 }
2383
2384 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2385 Layout.push_back(inst);
2386 std::string BitMap;
2387 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2388 BitMap += Layout[i];
2389
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002390 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2391 printf("\n block variable layout: ");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002392 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2393 unsigned char inst = BitMap[i];
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002394 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2395 unsigned delta = 1;
2396 switch (opcode) {
2397 case BLOCK_LAYOUT_OPERATOR:
2398 printf("BL_OPERATOR:");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002399 delta = 0;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002400 break;
2401 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2402 printf("BL_NON_OBJECT_BYTES:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002403 break;
2404 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2405 printf("BL_NON_OBJECT_WORD:");
2406 break;
2407 case BLOCK_LAYOUT_STRONG:
2408 printf("BL_STRONG:");
2409 break;
2410 case BLOCK_LAYOUT_BYREF:
2411 printf("BL_BYREF:");
2412 break;
2413 case BLOCK_LAYOUT_WEAK:
2414 printf("BL_WEAK:");
2415 break;
2416 case BLOCK_LAYOUT_UNRETAINED:
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002417 printf("BL_UNRETAINED:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002418 break;
2419 }
2420 // Actual value of word count is one more that what is in the imm.
2421 // field of the instruction
2422 printf("%d", (inst & 0xf) + delta);
2423 if (i < e-1)
2424 printf(", ");
2425 else
2426 printf("\n");
2427 }
2428 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002429
2430 llvm::GlobalVariable * Entry =
2431 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2432 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2433 "__TEXT,__objc_classname,cstring_literals", 1, true);
2434 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002435}
2436
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002437llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002438 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002439 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002440 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002441 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2442
Owen Anderson3c4972d2009-07-29 18:54:39 +00002443 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002444 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002445}
2446
Fariborz Jahanianda320092009-01-29 19:24:30 +00002447void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002448 // FIXME: We shouldn't need this, the protocol decl should contain enough
2449 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002450 DefinedProtocols.insert(PD->getIdentifier());
2451
2452 // If we have generated a forward reference to this protocol, emit
2453 // it now. Otherwise do nothing, the protocol objects are lazily
2454 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002455 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002456 GetOrEmitProtocol(PD);
2457}
2458
Fariborz Jahanianda320092009-01-29 19:24:30 +00002459llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002460 if (DefinedProtocols.count(PD->getIdentifier()))
2461 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002462
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002463 return GetOrEmitProtocolRef(PD);
2464}
2465
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002466/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002467// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2468struct _objc_protocol {
2469struct _objc_protocol_extension *isa;
2470char *protocol_name;
2471struct _objc_protocol_list *protocol_list;
2472struct _objc__method_prototype_list *instance_methods;
2473struct _objc__method_prototype_list *class_methods
2474};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002475
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002476See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002477*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002478llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002479 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002480
2481 // Early exit if a defining object has already been generated.
2482 if (Entry && Entry->hasInitializer())
2483 return Entry;
2484
Douglas Gregor1d784b22012-01-01 19:51:50 +00002485 // Use the protocol definition, if there is one.
2486 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2487 PD = Def;
2488
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002489 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002490 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002491 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2492
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002493 // Construct method lists.
2494 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2495 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002496 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002498 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002499 ObjCMethodDecl *MD = *i;
2500 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002501 if (!C)
2502 return GetOrEmitProtocolRef(PD);
2503
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002504 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2505 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002506 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002507 } else {
2508 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002509 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002510 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002511 }
2512
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002514 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002515 ObjCMethodDecl *MD = *i;
2516 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002517 if (!C)
2518 return GetOrEmitProtocolRef(PD);
2519
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002520 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2521 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002522 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002523 } else {
2524 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002525 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002526 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002527 }
2528
Bob Wilsondc8dab62011-11-30 01:57:58 +00002529 MethodTypesExt.insert(MethodTypesExt.end(),
2530 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2531
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002532 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002533 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2534 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002535 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002536 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00002537 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002538 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002539 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002540 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002541 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002542 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002543 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002544 ClassMethods)
2545 };
Owen Anderson08e25242009-07-27 22:29:56 +00002546 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002547 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002548
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002549 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002550 // Already created, fix the linkage and update the initializer.
2551 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002552 Entry->setInitializer(Init);
2553 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002554 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002555 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002556 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002557 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002558 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002559 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002560 // FIXME: Is this necessary? Why only for protocol?
2561 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002562
2563 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002564 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002565 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002566
2567 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002568}
2569
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002570llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002571 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2572
2573 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002574 // We use the initializer as a marker of whether this is a forward
2575 // reference or not. At module finalization we add the empty
2576 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002577 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002578 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002579 llvm::GlobalValue::ExternalLinkage,
2580 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002581 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002582 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002583 // FIXME: Is this necessary? Why only for protocol?
2584 Entry->setAlignment(4);
2585 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002586
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002587 return Entry;
2588}
2589
2590/*
2591 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002592 uint32_t size;
2593 struct objc_method_description_list *optional_instance_methods;
2594 struct objc_method_description_list *optional_class_methods;
2595 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002596 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002597 };
2598*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002599llvm::Constant *
2600CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002601 ArrayRef<llvm::Constant*> OptInstanceMethods,
2602 ArrayRef<llvm::Constant*> OptClassMethods,
2603 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002604 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002605 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002606 llvm::Constant *Values[] = {
2607 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002608 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002609 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002610 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002611 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002612 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002613 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002614 OptClassMethods),
2615 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002616 ObjCTypes),
2617 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2618 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002619 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002620
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002621 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002622 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002623 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002624 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002625
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002626 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002627 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002628
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002629 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002630 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002631 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002632 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633}
2634
2635/*
2636 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002637 struct objc_protocol_list *next;
2638 long count;
2639 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002640 };
2641*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002642llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002643CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002644 ObjCProtocolDecl::protocol_iterator begin,
2645 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002646 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002647
Daniel Dunbardbc93372008-08-21 21:57:41 +00002648 for (; begin != end; ++begin)
2649 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002650
2651 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002652 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002653 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002654
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002655 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002656 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002657
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002658 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002659 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002660 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002661 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002662 ProtocolRefs.size() - 1);
2663 Values[2] =
2664 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2665 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002666 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002667
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002668 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002669 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002670 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002671 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002672 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002673}
2674
Bill Wendlingbb028552012-02-07 09:25:09 +00002675void CGObjCCommonMac::
2676PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002677 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002678 const Decl *Container,
2679 const ObjCProtocolDecl *PROTO,
2680 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002681 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2682 E = PROTO->protocol_end(); P != E; ++P)
2683 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2684 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2685 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002686 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002687 if (!PropertySet.insert(PD->getIdentifier()))
2688 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002689 llvm::Constant *Prop[] = {
2690 GetPropertyName(PD->getIdentifier()),
2691 GetPropertyTypeString(PD, Container)
2692 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002693 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2694 }
2695}
2696
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002697/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002698 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002699 const char * const name;
2700 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002701 };
2702
2703 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002704 uint32_t entsize; // sizeof (struct _objc_property)
2705 uint32_t prop_count;
2706 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002707 };
2708*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002709llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002710 const Decl *Container,
2711 const ObjCContainerDecl *OCD,
2712 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002713 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002714 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002715 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2716 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002717 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002718 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002719 llvm::Constant *Prop[] = {
2720 GetPropertyName(PD->getIdentifier()),
2721 GetPropertyTypeString(PD, Container)
2722 };
Owen Anderson08e25242009-07-27 22:29:56 +00002723 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002724 Prop));
2725 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002726 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002727 for (ObjCInterfaceDecl::all_protocol_iterator
2728 P = OID->all_referenced_protocol_begin(),
2729 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002730 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2731 ObjCTypes);
2732 }
2733 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2734 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2735 E = CD->protocol_end(); P != E; ++P)
2736 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2737 ObjCTypes);
2738 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002739
2740 // Return null for empty list.
2741 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002742 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002743
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002744 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002745 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002746 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002747 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2748 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002749 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002750 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002751 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002752 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002753
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002754 llvm::GlobalVariable *GV =
2755 CreateMetadataVar(Name, Init,
2756 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002757 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002758 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002759 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002760 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002761}
2762
Bill Wendlingbb028552012-02-07 09:25:09 +00002763llvm::Constant *
2764CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2765 ArrayRef<llvm::Constant*> MethodTypes,
2766 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002767 // Return null for empty list.
2768 if (MethodTypes.empty())
2769 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2770
2771 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2772 MethodTypes.size());
2773 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2774
2775 llvm::GlobalVariable *GV =
2776 CreateMetadataVar(Name, Init,
2777 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2778 (ObjCABI == 2) ? 8 : 4,
2779 true);
2780 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2781}
2782
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002783/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002784 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002785 int count;
2786 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002787 };
2788*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002789llvm::Constant *
2790CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002791 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002792 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002793 ObjCTypes.SelectorPtrTy),
2794 GetMethodVarType(MD)
2795 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002796 if (!Desc[1])
2797 return 0;
2798
Owen Anderson08e25242009-07-27 22:29:56 +00002799 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002800 Desc);
2801}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002802
Bill Wendlingbb028552012-02-07 09:25:09 +00002803llvm::Constant *
2804CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2805 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002806 // Return null for empty list.
2807 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002808 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002809
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002810 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002811 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002812 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002813 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002814 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002815 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002816
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002817 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002818 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002819 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002820}
2821
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002822/*
2823 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002824 char *category_name;
2825 char *class_name;
2826 struct _objc_method_list *instance_methods;
2827 struct _objc_method_list *class_methods;
2828 struct _objc_protocol_list *protocols;
2829 uint32_t size; // <rdar://4585769>
2830 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002831 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002832*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002833void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002834 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002835
Mike Stumpf5408fe2009-05-16 07:57:57 +00002836 // FIXME: This is poor design, the OCD should have a pointer to the category
2837 // decl. Additionally, note that Category can be null for the @implementation
2838 // w/o an @interface case. Sema should just create one for us as it does for
2839 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002840 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002841 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002842 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002843
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002844 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002845 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2846 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002847
Bill Wendling3964e622012-02-09 22:16:49 +00002848 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002849 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002850 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002851 // Instance methods should always be defined.
2852 InstanceMethods.push_back(GetMethodConstant(*i));
2853 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002854 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002855 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002856 // Class methods should always be defined.
2857 ClassMethods.push_back(GetMethodConstant(*i));
2858 }
2859
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002860 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002861 Values[0] = GetClassName(OCD->getIdentifier());
2862 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002863 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002865 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002866 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002867 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002868 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002869 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002870 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002871 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002872 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002873 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002874 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002875 Category->protocol_begin(),
2876 Category->protocol_end());
2877 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002878 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002879 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002880 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002881
2882 // If there is no category @interface then there can be no properties.
2883 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002884 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002885 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002886 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002887 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002888 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002889
Owen Anderson08e25242009-07-27 22:29:56 +00002890 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002891 Values);
2892
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002893 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002894 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002895 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002896 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002897 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002898 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002899 // method definition entries must be clear for next implementation.
2900 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002901}
2902
John McCall621915c2012-10-17 04:53:23 +00002903enum FragileClassFlags {
2904 FragileABI_Class_Factory = 0x00001,
2905 FragileABI_Class_Meta = 0x00002,
2906 FragileABI_Class_HasCXXStructors = 0x02000,
2907 FragileABI_Class_Hidden = 0x20000
2908};
2909
2910enum NonFragileClassFlags {
2911 /// Is a meta-class.
2912 NonFragileABI_Class_Meta = 0x00001,
2913
2914 /// Is a root class.
2915 NonFragileABI_Class_Root = 0x00002,
2916
2917 /// Has a C++ constructor and destructor.
2918 NonFragileABI_Class_HasCXXStructors = 0x00004,
2919
2920 /// Has hidden visibility.
2921 NonFragileABI_Class_Hidden = 0x00010,
2922
2923 /// Has the exception attribute.
2924 NonFragileABI_Class_Exception = 0x00020,
2925
2926 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2927 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2928
2929 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00002930 NonFragileABI_Class_CompiledByARC = 0x00080,
2931
2932 /// Class has non-trivial destructors, but zero-initialization is okay.
2933 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002934};
2935
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002936/*
2937 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002938 Class isa;
2939 Class super_class;
2940 const char *name;
2941 long version;
2942 long info;
2943 long instance_size;
2944 struct _objc_ivar_list *ivars;
2945 struct _objc_method_list *methods;
2946 struct _objc_cache *cache;
2947 struct _objc_protocol_list *protocols;
2948 // Objective-C 1.0 extensions (<rdr://4585769>)
2949 const char *ivar_layout;
2950 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002951 };
2952
2953 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002954*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002955void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002956 DefinedSymbols.insert(ID->getIdentifier());
2957
Chris Lattner8ec03f52008-11-24 03:54:41 +00002958 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002959 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002960 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002961 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002962 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002963 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002964 Interface->all_referenced_protocol_begin(),
2965 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00002966 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00002967 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00002968 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002969 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002970 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002971
2972 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002973 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00002974 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002975
Bill Wendling3964e622012-02-09 22:16:49 +00002976 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002977 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002978 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002979 // Instance methods should always be defined.
2980 InstanceMethods.push_back(GetMethodConstant(*i));
2981 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002982 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002983 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002984 // Class methods should always be defined.
2985 ClassMethods.push_back(GetMethodConstant(*i));
2986 }
2987
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002988 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002989 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002990 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002991
2992 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2993 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2994
2995 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2996 if (llvm::Constant *C = GetMethodConstant(MD))
2997 InstanceMethods.push_back(C);
2998 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2999 if (llvm::Constant *C = GetMethodConstant(MD))
3000 InstanceMethods.push_back(C);
3001 }
3002 }
3003
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003004 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003005 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003006 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003007 // Record a reference to the super class.
3008 LazySymbols.insert(Super->getIdentifier());
3009
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003010 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003011 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003012 ObjCTypes.ClassPtrTy);
3013 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003014 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003015 }
3016 Values[ 2] = GetClassName(ID->getIdentifier());
3017 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003018 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3019 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3020 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003021 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003022 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003023 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003024 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003025 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003026 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003027 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003028 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003029 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003030 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003031 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003032 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003033 std::string Name("\01L_OBJC_CLASS_");
3034 Name += ClassName;
3035 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3036 // Check for a forward reference.
3037 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3038 if (GV) {
3039 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3040 "Forward metaclass reference has incorrect type.");
3041 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3042 GV->setInitializer(Init);
3043 GV->setSection(Section);
3044 GV->setAlignment(4);
3045 CGM.AddUsedGlobal(GV);
3046 }
3047 else
3048 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003049 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003050 // method definition entries must be clear for next implementation.
3051 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003052}
3053
3054llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3055 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003056 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003057 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003058 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003059
John McCall1fb0caa2010-10-22 21:05:15 +00003060 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003061 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003062
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003063 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003064 // The isa for the metaclass is the root of the hierarchy.
3065 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3066 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3067 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003068 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003069 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003070 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003071 // The super class for the metaclass is emitted as the name of the
3072 // super class. The runtime fixes this up to point to the
3073 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003074 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003075 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003076 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003077 ObjCTypes.ClassPtrTy);
3078 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003079 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003080 }
3081 Values[ 2] = GetClassName(ID->getIdentifier());
3082 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003083 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3084 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3085 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003086 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003087 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003088 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003089 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003090 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003091 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003092 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003093 Values[ 9] = Protocols;
3094 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003095 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003096 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003097 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003098 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003099 Values);
3100
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003101 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003102 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003103
3104 // Check for a forward reference.
3105 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3106 if (GV) {
3107 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3108 "Forward metaclass reference has incorrect type.");
3109 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3110 GV->setInitializer(Init);
3111 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003112 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003113 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003114 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003115 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003116 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003117 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003118 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003119
3120 return GV;
3121}
3122
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003123llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003124 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003125
Mike Stumpf5408fe2009-05-16 07:57:57 +00003126 // FIXME: Should we look these up somewhere other than the module. Its a bit
3127 // silly since we only generate these while processing an implementation, so
3128 // exactly one pointer would work if know when we entered/exitted an
3129 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003130
3131 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003132 // Previously, metaclass with internal linkage may have been defined.
3133 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003134 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3135 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003136 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3137 "Forward metaclass reference has incorrect type.");
3138 return GV;
3139 } else {
3140 // Generate as an external reference to keep a consistent
3141 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003142 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003143 llvm::GlobalValue::ExternalLinkage,
3144 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003145 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003146 }
3147}
3148
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003149llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3150 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3151
3152 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3153 true)) {
3154 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3155 "Forward class metadata reference has incorrect type.");
3156 return GV;
3157 } else {
3158 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3159 llvm::GlobalValue::ExternalLinkage,
3160 0,
3161 Name);
3162 }
3163}
3164
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003165/*
3166 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003167 uint32_t size;
3168 const char *weak_ivar_layout;
3169 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003170 };
3171*/
3172llvm::Constant *
3173CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003174 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003175 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003176
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003177 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003178 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003179 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003180 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003181 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003182
3183 // Return null if no extension bits are used.
3184 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003185 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003186
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003187 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003188 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003189 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003190 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003191 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003192}
3193
3194/*
3195 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003196 char *ivar_name;
3197 char *ivar_type;
3198 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003199 };
3200
3201 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003202 int ivar_count;
3203 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003204 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003205*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003206llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003207 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003208 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003209
3210 // When emitting the root class GCC emits ivar entries for the
3211 // actual class structure. It is not clear if we need to follow this
3212 // behavior; for now lets try and get away with not doing it. If so,
3213 // the cleanest solution would be to make up an ObjCInterfaceDecl
3214 // for the class.
3215 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003216 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003217
Jordy Rosedb8264e2011-07-22 02:08:32 +00003218 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003219
Jordy Rosedb8264e2011-07-22 02:08:32 +00003220 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003221 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003222 // Ignore unnamed bit-fields.
3223 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003224 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003225 llvm::Constant *Ivar[] = {
3226 GetMethodVarName(IVD->getIdentifier()),
3227 GetMethodVarType(IVD),
3228 llvm::ConstantInt::get(ObjCTypes.IntTy,
3229 ComputeIvarBaseOffset(CGM, OID, IVD))
3230 };
Owen Anderson08e25242009-07-27 22:29:56 +00003231 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003232 }
3233
3234 // Return null for empty list.
3235 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003236 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003237
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003238 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003239 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003240 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003241 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003242 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003243 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003244
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003245 llvm::GlobalVariable *GV;
3246 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003247 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003248 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003249 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003250 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003251 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003252 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003253 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003254 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003255}
3256
3257/*
3258 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003259 SEL method_name;
3260 char *method_types;
3261 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003262 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003263
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003264 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003265 struct objc_method_list *obsolete;
3266 int count;
3267 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003268 };
3269*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003270
3271/// GetMethodConstant - Return a struct objc_method constant for the
3272/// given method if it has been defined. The result is null if the
3273/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003274llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003275 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003276 if (!Fn)
3277 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003278
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003279 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003280 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003281 ObjCTypes.SelectorPtrTy),
3282 GetMethodVarType(MD),
3283 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3284 };
Owen Anderson08e25242009-07-27 22:29:56 +00003285 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003286}
3287
Chris Lattner5f9e2722011-07-23 10:55:15 +00003288llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003289 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003290 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003291 // Return null for empty list.
3292 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003293 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003294
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003295 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003296 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003297 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003298 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003299 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003300 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003301 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003302
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003303 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003304 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003305}
3306
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003307llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003308 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003309 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003310 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003311
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003312 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003313 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003314 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003315 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003316 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003317 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003318 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003319 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003320 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003321
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003322 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003323}
3324
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003325llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003326CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003327 llvm::Constant *Init,
3328 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003329 unsigned Align,
3330 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003331 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003332 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003333 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003334 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003335 if (Section)
3336 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003337 if (Align)
3338 GV->setAlignment(Align);
3339 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003340 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003341 return GV;
3342}
3343
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003344llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003345 // Abuse this interface function as a place to finalize.
3346 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003347 return NULL;
3348}
3349
Chris Lattner74391b42009-03-22 21:03:39 +00003350llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003351 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003352}
3353
Chris Lattner74391b42009-03-22 21:03:39 +00003354llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003355 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003356}
3357
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003358llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3359 bool copy) {
3360 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3361}
3362
David Chisnall8fac25d2010-12-26 22:13:16 +00003363llvm::Constant *CGObjCMac::GetGetStructFunction() {
3364 return ObjCTypes.getCopyStructFn();
3365}
3366llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003367 return ObjCTypes.getCopyStructFn();
3368}
3369
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003370llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
3371 return ObjCTypes.getCppAtomicObjectFunction();
3372}
3373
Chris Lattner74391b42009-03-22 21:03:39 +00003374llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003375 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003376}
3377
John McCallf1549f62010-07-06 01:34:17 +00003378void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3379 return EmitTryOrSynchronizedStmt(CGF, S);
3380}
3381
3382void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3383 const ObjCAtSynchronizedStmt &S) {
3384 return EmitTryOrSynchronizedStmt(CGF, S);
3385}
3386
John McCallcc505292010-07-21 06:59:36 +00003387namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003388 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003389 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003390 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003391 llvm::Value *CallTryExitVar;
3392 llvm::Value *ExceptionData;
3393 ObjCTypesHelper &ObjCTypes;
3394 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003395 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003396 llvm::Value *CallTryExitVar,
3397 llvm::Value *ExceptionData,
3398 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003399 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003400 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3401
John McCallad346f42011-07-12 20:27:29 +00003402 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003403 // Check whether we need to call objc_exception_try_exit.
3404 // In optimized code, this branch will always be folded.
3405 llvm::BasicBlock *FinallyCallExit =
3406 CGF.createBasicBlock("finally.call_exit");
3407 llvm::BasicBlock *FinallyNoCallExit =
3408 CGF.createBasicBlock("finally.no_call_exit");
3409 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3410 FinallyCallExit, FinallyNoCallExit);
3411
3412 CGF.EmitBlock(FinallyCallExit);
3413 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3414 ->setDoesNotThrow();
3415
3416 CGF.EmitBlock(FinallyNoCallExit);
3417
3418 if (isa<ObjCAtTryStmt>(S)) {
3419 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003420 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3421 // Save the current cleanup destination in case there's
3422 // control flow inside the finally statement.
3423 llvm::Value *CurCleanupDest =
3424 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3425
John McCallcc505292010-07-21 06:59:36 +00003426 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3427
John McCalld96a8e72010-08-11 00:16:14 +00003428 if (CGF.HaveInsertPoint()) {
3429 CGF.Builder.CreateStore(CurCleanupDest,
3430 CGF.getNormalCleanupDestSlot());
3431 } else {
3432 // Currently, the end of the cleanup must always exist.
3433 CGF.EnsureInsertPoint();
3434 }
3435 }
John McCallcc505292010-07-21 06:59:36 +00003436 } else {
3437 // Emit objc_sync_exit(expr); as finally's sole statement for
3438 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003439 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003440 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3441 ->setDoesNotThrow();
3442 }
3443 }
3444 };
John McCall87bb5822010-07-31 23:20:56 +00003445
3446 class FragileHazards {
3447 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003448 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003449 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3450
3451 llvm::InlineAsm *ReadHazard;
3452 llvm::InlineAsm *WriteHazard;
3453
3454 llvm::FunctionType *GetAsmFnType();
3455
3456 void collectLocals();
3457 void emitReadHazard(CGBuilderTy &Builder);
3458
3459 public:
3460 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003461
John McCall87bb5822010-07-31 23:20:56 +00003462 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003463 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003464 };
3465}
3466
3467/// Create the fragile-ABI read and write hazards based on the current
3468/// state of the function, which is presumed to be immediately prior
3469/// to a @try block. These hazards are used to maintain correct
3470/// semantics in the face of optimization and the fragile ABI's
3471/// cavalier use of setjmp/longjmp.
3472FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3473 collectLocals();
3474
3475 if (Locals.empty()) return;
3476
3477 // Collect all the blocks in the function.
3478 for (llvm::Function::iterator
3479 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3480 BlocksBeforeTry.insert(&*I);
3481
3482 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3483
3484 // Create a read hazard for the allocas. This inhibits dead-store
3485 // optimizations and forces the values to memory. This hazard is
3486 // inserted before any 'throwing' calls in the protected scope to
3487 // reflect the possibility that the variables might be read from the
3488 // catch block if the call throws.
3489 {
3490 std::string Constraint;
3491 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3492 if (I) Constraint += ',';
3493 Constraint += "*m";
3494 }
3495
3496 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3497 }
3498
3499 // Create a write hazard for the allocas. This inhibits folding
3500 // loads across the hazard. This hazard is inserted at the
3501 // beginning of the catch path to reflect the possibility that the
3502 // variables might have been written within the protected scope.
3503 {
3504 std::string Constraint;
3505 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3506 if (I) Constraint += ',';
3507 Constraint += "=*m";
3508 }
3509
3510 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3511 }
3512}
3513
3514/// Emit a write hazard at the current location.
3515void FragileHazards::emitWriteHazard() {
3516 if (Locals.empty()) return;
3517
Jay Foad4c7d9f12011-07-15 08:37:34 +00003518 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003519}
3520
John McCall87bb5822010-07-31 23:20:56 +00003521void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3522 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003523 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003524}
3525
3526/// Emit read hazards in all the protected blocks, i.e. all the blocks
3527/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003528void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003529 if (Locals.empty()) return;
3530
3531 CGBuilderTy Builder(CGF.getLLVMContext());
3532
3533 // Iterate through all blocks, skipping those prior to the try.
3534 for (llvm::Function::iterator
3535 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3536 llvm::BasicBlock &BB = *FI;
3537 if (BlocksBeforeTry.count(&BB)) continue;
3538
3539 // Walk through all the calls in the block.
3540 for (llvm::BasicBlock::iterator
3541 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3542 llvm::Instruction &I = *BI;
3543
3544 // Ignore instructions that aren't non-intrinsic calls.
3545 // These are the only calls that can possibly call longjmp.
3546 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3547 if (isa<llvm::IntrinsicInst>(I))
3548 continue;
3549
3550 // Ignore call sites marked nounwind. This may be questionable,
3551 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3552 llvm::CallSite CS(&I);
3553 if (CS.doesNotThrow()) continue;
3554
John McCall0b251722010-08-04 05:59:32 +00003555 // Insert a read hazard before the call. This will ensure that
3556 // any writes to the locals are performed before making the
3557 // call. If the call throws, then this is sufficient to
3558 // guarantee correctness as long as it doesn't also write to any
3559 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003560 Builder.SetInsertPoint(&BB, BI);
3561 emitReadHazard(Builder);
3562 }
3563 }
3564}
3565
3566static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3567 if (V) S.insert(V);
3568}
3569
3570void FragileHazards::collectLocals() {
3571 // Compute a set of allocas to ignore.
3572 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3573 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3574 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003575
3576 // Collect all the allocas currently in the function. This is
3577 // probably way too aggressive.
3578 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3579 for (llvm::BasicBlock::iterator
3580 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3581 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3582 Locals.push_back(&*I);
3583}
3584
3585llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003586 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003587 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3588 tys[i] = Locals[i]->getType();
3589 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003590}
3591
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003592/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003593
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003594 Objective-C setjmp-longjmp (sjlj) Exception Handling
3595 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003596
John McCallf1549f62010-07-06 01:34:17 +00003597 A catch buffer is a setjmp buffer plus:
3598 - a pointer to the exception that was caught
3599 - a pointer to the previous exception data buffer
3600 - two pointers of reserved storage
3601 Therefore catch buffers form a stack, with a pointer to the top
3602 of the stack kept in thread-local storage.
3603
3604 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3605 objc_exception_try_exit pops the given catch buffer, which is
3606 required to be the top of the EH stack.
3607 objc_exception_throw pops the top of the EH stack, writes the
3608 thrown exception into the appropriate field, and longjmps
3609 to the setjmp buffer. It crashes the process (with a printf
3610 and an abort()) if there are no catch buffers on the stack.
3611 objc_exception_extract just reads the exception pointer out of the
3612 catch buffer.
3613
3614 There's no reason an implementation couldn't use a light-weight
3615 setjmp here --- something like __builtin_setjmp, but API-compatible
3616 with the heavyweight setjmp. This will be more important if we ever
3617 want to implement correct ObjC/C++ exception interactions for the
3618 fragile ABI.
3619
3620 Note that for this use of setjmp/longjmp to be correct, we may need
3621 to mark some local variables volatile: if a non-volatile local
3622 variable is modified between the setjmp and the longjmp, it has
3623 indeterminate value. For the purposes of LLVM IR, it may be
3624 sufficient to make loads and stores within the @try (to variables
3625 declared outside the @try) volatile. This is necessary for
3626 optimized correctness, but is not currently being done; this is
3627 being tracked as rdar://problem/8160285
3628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003629 The basic framework for a @try-catch-finally is as follows:
3630 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003631 objc_exception_data d;
3632 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003633 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003634
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003635 objc_exception_try_enter(&d);
3636 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003637 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003638 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003639 // exception path
3640 id _caught = objc_exception_extract(&d);
3641
3642 // enter new try scope for handlers
3643 if (!setjmp(d.jmp_buf)) {
3644 ... match exception and execute catch blocks ...
3645
3646 // fell off end, rethrow.
3647 _rethrow = _caught;
3648 ... jump-through-finally to finally_rethrow ...
3649 } else {
3650 // exception in catch block
3651 _rethrow = objc_exception_extract(&d);
3652 _call_try_exit = false;
3653 ... jump-through-finally to finally_rethrow ...
3654 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003655 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003656 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003657
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003658 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003659 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003660 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003661
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003662 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003663 ... dispatch to finally destination ...
3664
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003665 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003666 objc_exception_throw(_rethrow);
3667
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003668 finally_end:
3669 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003670
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003671 This framework differs slightly from the one gcc uses, in that gcc
3672 uses _rethrow to determine if objc_exception_try_exit should be called
3673 and if the object should be rethrown. This breaks in the face of
3674 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003675
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003677
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003678 - If there are no catch blocks, then we avoid emitting the second
3679 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003680
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3682 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003683
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003684 - FIXME: If there is no @finally block we can do a few more
3685 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003686
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687 Rethrows and Jumps-Through-Finally
3688 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003689
John McCallf1549f62010-07-06 01:34:17 +00003690 '@throw;' is supported by pushing the currently-caught exception
3691 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003692
John McCallf1549f62010-07-06 01:34:17 +00003693 Branches through the @finally block are handled with an ordinary
3694 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3695 exceptions are not compatible with C++ exceptions, and this is
3696 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003697
John McCallf1549f62010-07-06 01:34:17 +00003698 @synchronized(expr) { stmt; } is emitted as if it were:
3699 id synch_value = expr;
3700 objc_sync_enter(synch_value);
3701 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003702*/
3703
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003704void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3705 const Stmt &S) {
3706 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003707
3708 // A destination for the fall-through edges of the catch handlers to
3709 // jump to.
3710 CodeGenFunction::JumpDest FinallyEnd =
3711 CGF.getJumpDestInCurrentScope("finally.end");
3712
3713 // A destination for the rethrow edge of the catch handlers to jump
3714 // to.
3715 CodeGenFunction::JumpDest FinallyRethrow =
3716 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003717
Daniel Dunbar1c566672009-02-24 01:43:46 +00003718 // For @synchronized, call objc_sync_enter(sync.expr). The
3719 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003720 // @synchronized. We can't avoid a temp here because we need the
3721 // value to be preserved. If the backend ever does liveness
3722 // correctly after setjmp, this will be unnecessary.
3723 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003724 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003725 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003726 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3727 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003728 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3729 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003730
3731 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3732 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003733 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003734
John McCall0b251722010-08-04 05:59:32 +00003735 // Allocate memory for the setjmp buffer. This needs to be kept
3736 // live throughout the try and catch blocks.
3737 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3738 "exceptiondata.ptr");
3739
John McCall87bb5822010-07-31 23:20:56 +00003740 // Create the fragile hazards. Note that this will not capture any
3741 // of the allocas required for exception processing, but will
3742 // capture the current basic block (which extends all the way to the
3743 // setjmp call) as "before the @try".
3744 FragileHazards Hazards(CGF);
3745
John McCallf1549f62010-07-06 01:34:17 +00003746 // Create a flag indicating whether the cleanup needs to call
3747 // objc_exception_try_exit. This is true except when
3748 // - no catches match and we're branching through the cleanup
3749 // just to rethrow the exception, or
3750 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003751 // The setjmp-safety rule here is that we should always store to this
3752 // variable in a place that dominates the branch through the cleanup
3753 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003754 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003755 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003756
John McCall9e2213d2010-10-04 23:42:51 +00003757 // A slot containing the exception to rethrow. Only needed when we
3758 // have both a @catch and a @finally.
3759 llvm::Value *PropagatingExnVar = 0;
3760
John McCallf1549f62010-07-06 01:34:17 +00003761 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003762 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003763 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003764 CallTryExitVar,
3765 ExceptionData,
3766 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003767
3768 // Enter a try block:
3769 // - Call objc_exception_try_enter to push ExceptionData on top of
3770 // the EH stack.
3771 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3772 ->setDoesNotThrow();
3773
3774 // - Call setjmp on the exception data buffer.
3775 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3776 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3777 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003778 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003779 llvm::CallInst *SetJmpResult =
3780 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3781 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003782 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003783
3784 // If setjmp returned 0, enter the protected block; otherwise,
3785 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003786 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3787 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003788 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003789 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3790 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003791
John McCallf1549f62010-07-06 01:34:17 +00003792 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003793 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003794 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003795 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003796 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003797
3798 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003799
John McCallf1549f62010-07-06 01:34:17 +00003800 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003801 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003802
John McCall87bb5822010-07-31 23:20:56 +00003803 // Don't optimize loads of the in-scope locals across this point.
3804 Hazards.emitWriteHazard();
3805
John McCallf1549f62010-07-06 01:34:17 +00003806 // For a @synchronized (or a @try with no catches), just branch
3807 // through the cleanup to the rethrow block.
3808 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3809 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003810 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003811 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003812
3813 // Otherwise, we have to match against the caught exceptions.
3814 } else {
John McCall0b251722010-08-04 05:59:32 +00003815 // Retrieve the exception object. We may emit multiple blocks but
3816 // nothing can cross this so the value is already in SSA form.
3817 llvm::CallInst *Caught =
3818 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3819 ExceptionData, "caught");
3820 Caught->setDoesNotThrow();
3821
John McCallf1549f62010-07-06 01:34:17 +00003822 // Push the exception to rethrow onto the EH value stack for the
3823 // benefit of any @throws in the handlers.
3824 CGF.ObjCEHValueStack.push_back(Caught);
3825
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003826 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003827
John McCall0b251722010-08-04 05:59:32 +00003828 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003829
John McCall0b251722010-08-04 05:59:32 +00003830 llvm::BasicBlock *CatchBlock = 0;
3831 llvm::BasicBlock *CatchHandler = 0;
3832 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003833 // Save the currently-propagating exception before
3834 // objc_exception_try_enter clears the exception slot.
3835 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3836 "propagating_exception");
3837 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3838
John McCall0b251722010-08-04 05:59:32 +00003839 // Enter a new exception try block (in case a @catch block
3840 // throws an exception).
3841 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3842 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003843
John McCall0b251722010-08-04 05:59:32 +00003844 llvm::CallInst *SetJmpResult =
3845 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3846 "setjmp.result");
3847 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003848 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003849
John McCall0b251722010-08-04 05:59:32 +00003850 llvm::Value *Threw =
3851 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3852
3853 CatchBlock = CGF.createBasicBlock("catch");
3854 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3855 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3856
3857 CGF.EmitBlock(CatchBlock);
3858 }
3859
3860 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003861
Daniel Dunbar55e40722008-09-27 07:03:52 +00003862 // Handle catch list. As a special case we check if everything is
3863 // matched and avoid generating code for falling off the end if
3864 // so.
3865 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003866 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3867 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003868
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003869 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003870 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003871
Anders Carlsson80f25672008-09-09 17:59:25 +00003872 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003873 if (!CatchParam) {
3874 AllMatched = true;
3875 } else {
John McCall183700f2009-09-21 23:43:11 +00003876 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003877
John McCallf1549f62010-07-06 01:34:17 +00003878 // catch(id e) always matches under this ABI, since only
3879 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003880 // FIXME: For the time being we also match id<X>; this should
3881 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003882 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003883 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003884 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003885
John McCallf1549f62010-07-06 01:34:17 +00003886 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003887 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003888 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3889
Anders Carlssondde0a942008-09-11 09:15:33 +00003890 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003891 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003892 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003893
3894 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003895 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003896 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003897
Anders Carlssondde0a942008-09-11 09:15:33 +00003898 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003899
3900 // The scope of the catch variable ends right here.
3901 CatchVarCleanups.ForceCleanup();
3902
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003903 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003904 break;
3905 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003906
Steve Naroff14108da2009-07-10 23:34:53 +00003907 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003908 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003909
3910 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003911 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3912 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003913
3914 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003915 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003916
John McCallf1549f62010-07-06 01:34:17 +00003917 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003918 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3919 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003920 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003921
John McCallf1549f62010-07-06 01:34:17 +00003922 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3923 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003924
3925 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003926 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003927
Anders Carlsson80f25672008-09-09 17:59:25 +00003928 // Emit the @catch block.
3929 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003930
3931 // Collect any cleanups for the catch variable. The scope lasts until
3932 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003933 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003934
John McCallb6bbcc92010-10-15 04:57:14 +00003935 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003936 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003937
John McCallf1549f62010-07-06 01:34:17 +00003938 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003939 llvm::Value *Tmp =
3940 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003941 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003942 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003943
Anders Carlssondde0a942008-09-11 09:15:33 +00003944 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003945
3946 // We're done with the catch variable.
3947 CatchVarCleanups.ForceCleanup();
3948
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003949 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003950
Anders Carlsson80f25672008-09-09 17:59:25 +00003951 CGF.EmitBlock(NextCatchBlock);
3952 }
3953
John McCallf1549f62010-07-06 01:34:17 +00003954 CGF.ObjCEHValueStack.pop_back();
3955
John McCall0b251722010-08-04 05:59:32 +00003956 // If nothing wanted anything to do with the caught exception,
3957 // kill the extract call.
3958 if (Caught->use_empty())
3959 Caught->eraseFromParent();
3960
3961 if (!AllMatched)
3962 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3963
3964 if (HasFinally) {
3965 // Emit the exception handler for the @catch blocks.
3966 CGF.EmitBlock(CatchHandler);
3967
3968 // In theory we might now need a write hazard, but actually it's
3969 // unnecessary because there's no local-accessing code between
3970 // the try's write hazard and here.
3971 //Hazards.emitWriteHazard();
3972
John McCall9e2213d2010-10-04 23:42:51 +00003973 // Extract the new exception and save it to the
3974 // propagating-exception slot.
3975 assert(PropagatingExnVar);
3976 llvm::CallInst *NewCaught =
3977 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3978 ExceptionData, "caught");
3979 NewCaught->setDoesNotThrow();
3980 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3981
John McCall0b251722010-08-04 05:59:32 +00003982 // Don't pop the catch handler; the throw already did.
3983 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003984 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003985 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003986 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003987
John McCall87bb5822010-07-31 23:20:56 +00003988 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003989 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003990
John McCallf1549f62010-07-06 01:34:17 +00003991 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003992 CGF.Builder.restoreIP(TryFallthroughIP);
3993 if (CGF.HaveInsertPoint())
3994 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003995 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003996 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003997
John McCallf1549f62010-07-06 01:34:17 +00003998 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003999 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004000 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004001 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004002 // If we have a propagating-exception variable, check it.
4003 llvm::Value *PropagatingExn;
4004 if (PropagatingExnVar) {
4005 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004006
John McCall9e2213d2010-10-04 23:42:51 +00004007 // Otherwise, just look in the buffer for the exception to throw.
4008 } else {
4009 llvm::CallInst *Caught =
4010 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4011 ExceptionData);
4012 Caught->setDoesNotThrow();
4013 PropagatingExn = Caught;
4014 }
4015
4016 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004017 ->setDoesNotThrow();
4018 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004019 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004020
John McCall87bb5822010-07-31 23:20:56 +00004021 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004022}
4023
4024void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00004025 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004026 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004027
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004028 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004029 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004030 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004031 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004032 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004033 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004034 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004035 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004036 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004037
John McCallf1549f62010-07-06 01:34:17 +00004038 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4039 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004040 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004041
4042 // Clear the insertion point to indicate we are in unreachable code.
4043 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004044}
4045
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004046/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004047/// object: objc_read_weak (id *src)
4048///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004049llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004050 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004051 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4053 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4054 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004055 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004056 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004057 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004058 return read_weak;
4059}
4060
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004061/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4062/// objc_assign_weak (id src, id *dst)
4063///
4064void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004065 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004066 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004067 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004068 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004069 assert(Size <= 8 && "does not support size > 8");
4070 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004071 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004072 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4073 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004074 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4075 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004076 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004077 src, dst, "weakassign");
4078 return;
4079}
4080
Fariborz Jahanian58626502008-11-19 00:59:10 +00004081/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4082/// objc_assign_global (id src, id *dst)
4083///
4084void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004085 llvm::Value *src, llvm::Value *dst,
4086 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004087 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004088 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004089 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004090 assert(Size <= 8 && "does not support size > 8");
4091 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004092 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004093 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4094 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004095 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4096 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004097 if (!threadlocal)
4098 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4099 src, dst, "globalassign");
4100 else
4101 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4102 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004103 return;
4104}
4105
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004106/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004107/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004108///
4109void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004110 llvm::Value *src, llvm::Value *dst,
4111 llvm::Value *ivarOffset) {
4112 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004113 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004114 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004115 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004116 assert(Size <= 8 && "does not support size > 8");
4117 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004118 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004119 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4120 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004121 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4122 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004123 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4124 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004125 return;
4126}
4127
Fariborz Jahanian58626502008-11-19 00:59:10 +00004128/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4129/// objc_assign_strongCast (id src, id *dst)
4130///
4131void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004132 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004133 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004134 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004135 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004136 assert(Size <= 8 && "does not support size > 8");
4137 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004138 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004139 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4140 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004141 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4142 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004143 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004144 src, dst, "weakassign");
4145 return;
4146}
4147
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004148void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004149 llvm::Value *DestPtr,
4150 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004151 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004152 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4153 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004154 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004155 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004156 return;
4157}
4158
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004159/// EmitObjCValueForIvar - Code Gen for ivar reference.
4160///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004161LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4162 QualType ObjectTy,
4163 llvm::Value *BaseValue,
4164 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004165 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004166 const ObjCInterfaceDecl *ID =
4167 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004168 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4169 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004170}
4171
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004172llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004173 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004174 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004175 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004176 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004177 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4178 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004179}
4180
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004181/* *** Private Interface *** */
4182
4183/// EmitImageInfo - Emit the image info marker used to encode some module
4184/// level information.
4185///
4186/// See: <rdr://4810609&4810587&4810587>
4187/// struct IMAGE_INFO {
4188/// unsigned version;
4189/// unsigned flags;
4190/// };
4191enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004192 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004193 eImageInfo_GarbageCollected = (1 << 1),
4194 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004195 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4196
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004197 // A flag indicating that the module has no instances of a @synthesize of a
4198 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004199 eImageInfo_CorrectedSynthesize = (1 << 4),
4200 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004201};
4202
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004203void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004204 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004205 const char *Section = (ObjCABI == 1) ?
4206 "__OBJC, __image_info,regular" :
4207 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004208
Bill Wendling3973acc2012-02-16 01:13:30 +00004209 // Generate module-level named metadata to convey this information to the
4210 // linker and code-gen.
4211 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212
Bill Wendling3973acc2012-02-16 01:13:30 +00004213 // Add the ObjC ABI version to the module flags.
4214 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4215 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4216 version);
4217 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4218 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004219
David Blaikie4e4d0842012-03-11 07:00:24 +00004220 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004221 // Non-GC overrides those files which specify GC.
4222 Mod.addModuleFlag(llvm::Module::Override,
4223 "Objective-C Garbage Collection", (uint32_t)0);
4224 } else {
4225 // Add the ObjC garbage collection value.
4226 Mod.addModuleFlag(llvm::Module::Error,
4227 "Objective-C Garbage Collection",
4228 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004229
David Blaikie4e4d0842012-03-11 07:00:24 +00004230 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004231 // Add the ObjC GC Only value.
4232 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4233 eImageInfo_GCOnly);
4234
4235 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4236 llvm::Value *Ops[2] = {
4237 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4238 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4239 eImageInfo_GarbageCollected)
4240 };
4241 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4242 llvm::MDNode::get(VMContext, Ops));
4243 }
4244 }
Bill Wendling09822512012-04-24 11:04:57 +00004245
4246 // Indicate whether we're compiling this to run on a simulator.
4247 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4248 if (Triple.getOS() == llvm::Triple::IOS &&
4249 (Triple.getArch() == llvm::Triple::x86 ||
4250 Triple.getArch() == llvm::Triple::x86_64))
4251 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4252 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004253}
4254
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004255// struct objc_module {
4256// unsigned long version;
4257// unsigned long size;
4258// const char *name;
4259// Symtab symtab;
4260// };
4261
4262// FIXME: Get from somewhere
4263static const int ModuleVersion = 7;
4264
4265void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004266 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004267
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004268 llvm::Constant *Values[] = {
4269 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4270 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4271 // This used to be the filename, now it is unused. <rdr://4327263>
4272 GetClassName(&CGM.getContext().Idents.get("")),
4273 EmitModuleSymbols()
4274 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004275 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004276 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004277 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004278 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004279}
4280
4281llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004282 unsigned NumClasses = DefinedClasses.size();
4283 unsigned NumCategories = DefinedCategories.size();
4284
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004285 // Return null if no symbols were defined.
4286 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004287 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004288
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004289 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004290 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004291 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004292 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4293 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004294
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004295 // The runtime expects exactly the list of defined classes followed
4296 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004297 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004298 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004299 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004300 ObjCTypes.Int8PtrTy);
4301 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004302 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004303 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004304 ObjCTypes.Int8PtrTy);
4305
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004306 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004307 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004308 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004309 Symbols);
4310
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004311 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004312
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004313 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004314 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4315 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004316 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004317 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004318}
4319
John McCallf85e1932011-06-15 23:02:42 +00004320llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4321 IdentifierInfo *II) {
4322 LazySymbols.insert(II);
4323
4324 llvm::GlobalVariable *&Entry = ClassReferences[II];
4325
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004326 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004327 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004328 llvm::ConstantExpr::getBitCast(GetClassName(II),
4329 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004330 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004331 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4332 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4333 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004334 }
John McCallf85e1932011-06-15 23:02:42 +00004335
Benjamin Kramer578faa82011-09-27 21:06:10 +00004336 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004337}
4338
John McCallf85e1932011-06-15 23:02:42 +00004339llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4340 const ObjCInterfaceDecl *ID) {
4341 return EmitClassRefFromId(Builder, ID->getIdentifier());
4342}
4343
4344llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4345 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4346 return EmitClassRefFromId(Builder, II);
4347}
4348
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004349llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4350 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004351 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004352
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004353 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004354 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004355 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004356 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004358 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4359 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004360 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004361 }
4362
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004363 if (lvalue)
4364 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004365 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004366}
4367
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004368llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004369 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004370
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004371 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004372 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004373 llvm::ConstantDataArray::getString(VMContext,
4374 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004375 ((ObjCABI == 2) ?
4376 "__TEXT,__objc_classname,cstring_literals" :
4377 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004378 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004379
Owen Andersona1cf15f2009-07-14 23:10:40 +00004380 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004381}
4382
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004383llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4384 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4385 I = MethodDefinitions.find(MD);
4386 if (I != MethodDefinitions.end())
4387 return I->second;
4388
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004389 return NULL;
4390}
4391
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004392/// GetIvarLayoutName - Returns a unique constant for the given
4393/// ivar layout bitmap.
4394llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004395 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004396 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004397}
4398
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004399void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004400 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004401 bool ForStrongLayout,
4402 bool &HasUnion) {
4403 const RecordDecl *RD = RT->getDecl();
4404 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004405 SmallVector<const FieldDecl*, 16> Fields;
4406 for (RecordDecl::field_iterator i = RD->field_begin(),
4407 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004408 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004409 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004410 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004411 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004413 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4414 ForStrongLayout, HasUnion);
4415}
4416
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004417void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004418 const llvm::StructLayout *Layout,
4419 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004420 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004421 unsigned int BytePos, bool ForStrongLayout,
4422 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004423 bool IsUnion = (RD && RD->isUnion());
4424 uint64_t MaxUnionIvarSize = 0;
4425 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00004426 const FieldDecl *MaxField = 0;
4427 const FieldDecl *MaxSkippedField = 0;
4428 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004429 uint64_t MaxFieldOffset = 0;
4430 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004431 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00004432 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004433
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004434 if (RecFields.empty())
4435 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004436 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
4437 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004438 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004439 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00004440 FirstFieldDelta =
4441 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
4442 }
4443
Chris Lattnerf1690852009-03-31 08:48:01 +00004444 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004445 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00004446 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004447 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004448 // Note that 'i' here is actually the field index inside RD of Field,
4449 // although this dependency is hidden.
4450 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00004451 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004452 } else
John McCallf85e1932011-06-15 23:02:42 +00004453 FieldOffset =
4454 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004455
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004456 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004457 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004458 LastFieldBitfieldOrUnnamed = Field;
4459 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004460 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004461 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004462
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004463 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004464 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004465 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004466 if (FQT->isUnionType())
4467 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004468
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004469 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004470 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004471 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004472 continue;
4473 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474
Chris Lattnerf1690852009-03-31 08:48:01 +00004475 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004476 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004477 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004478 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004479 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004480 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004481 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4482 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004483 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004484 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004485 FQT = CArray->getElementType();
4486 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004487
4488 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004489 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00004490 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004491 int OldIndex = IvarsInfo.size() - 1;
4492 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004493
Ted Kremenek6217b802009-07-29 21:53:49 +00004494 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004495 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004496 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004497
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004498 // Replicate layout information for each array element. Note that
4499 // one element is already done.
4500 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004501 for (int FirstIndex = IvarsInfo.size() - 1,
4502 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004503 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004504 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4505 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4506 IvarsInfo[i].ivar_size));
4507 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4508 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4509 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004510 }
4511 continue;
4512 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004513 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004514 // At this point, we are done with Record/Union and array there of.
4515 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00004516 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004517
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004518 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00004519 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4520 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004521 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004522 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004523 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004524 MaxUnionIvarSize = UnionIvarSize;
4525 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004526 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004527 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004528 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004529 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004530 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004531 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004532 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004533 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4534 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004535 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004536 // FIXME: Why the asymmetry? We divide by word size in bits on other
4537 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004538 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004539 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004540 MaxSkippedUnionIvarSize = UnionIvarSize;
4541 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004542 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004543 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004544 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004545 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004546 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004547 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004548 }
4549 }
4550 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004551
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004552 if (LastFieldBitfieldOrUnnamed) {
4553 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4554 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004555 uint64_t BitFieldSize
4556 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004557 GC_IVAR skivar;
4558 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
4559 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4560 + ((BitFieldSize % ByteSizeInBits) != 0);
4561 SkipIvars.push_back(skivar);
4562 } else {
4563 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4564 // Last field was unnamed. Must update skip info.
4565 unsigned FieldSize
4566 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4567 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4568 FieldSize / ByteSizeInBits));
4569 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004570 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004571
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004572 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004574 MaxUnionIvarSize));
4575 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004576 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004577 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004578}
4579
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004580/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4581/// the computations and returning the layout bitmap (for ivar or blocks) in
4582/// the given argument BitMap string container. Routine reads
4583/// two containers, IvarsInfo and SkipIvars which are assumed to be
4584/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004585llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004586 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004587 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004588
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004589 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004590 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004591 unsigned int WordSize =
Micah Villmow25a6a842012-10-08 16:25:52 +00004592 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004593 if (IvarsInfo[0].ivar_bytepos == 0) {
4594 WordsToSkip = 0;
4595 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004596 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004597 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4598 WordsToScan = IvarsInfo[0].ivar_size;
4599 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004600 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004601 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004602 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004603 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004604 // consecutive 'scanned' object pointers.
4605 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004606 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004607 // Skip over 'gc'able object pointer which lay over each other.
4608 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4609 continue;
4610 // Must skip over 1 or more words. We save current skip/scan values
4611 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004612 SKIP_SCAN SkScan;
4613 SkScan.skip = WordsToSkip;
4614 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004615 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004616
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004617 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004618 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4619 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004620 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004621 WordsToSkip = 0;
4622 WordsToScan = IvarsInfo[i].ivar_size;
4623 }
4624 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004625 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004626 SKIP_SCAN SkScan;
4627 SkScan.skip = WordsToSkip;
4628 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004629 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004630 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004631
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004632 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004633 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004634 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004635 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004636 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004637 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004638 IvarsInfo[LastIndex].ivar_bytepos +
4639 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004640 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004641 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004642 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004643 SKIP_SCAN SkScan;
4644 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4645 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004646 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004647 }
4648 }
4649 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4650 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004651 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004652 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004653 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4654 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4655 // 0xM0 followed by 0x0N detected.
4656 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4657 for (int j = i+1; j < SkipScan; j++)
4658 SkipScanIvars[j] = SkipScanIvars[j+1];
4659 --SkipScan;
4660 }
4661 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004662
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004663 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004664 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004665 unsigned char byte;
4666 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4667 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4668 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4669 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004670
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004671 // first skip big.
4672 for (unsigned int ix = 0; ix < skip_big; ix++)
4673 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004674
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004675 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004676 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004677 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004678 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004679 byte |= 0xf;
4680 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004681 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004682 byte |= scan_small;
4683 scan_small = 0;
4684 }
4685 BitMap += byte;
4686 }
4687 // next scan big
4688 for (unsigned int ix = 0; ix < scan_big; ix++)
4689 BitMap += (unsigned char)(0x0f);
4690 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004691 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004692 byte = scan_small;
4693 BitMap += byte;
4694 }
4695 }
4696 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004697 unsigned char zero = 0;
4698 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004699
4700 llvm::GlobalVariable * Entry =
4701 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004702 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004703 ((ObjCABI == 2) ?
4704 "__TEXT,__objc_classname,cstring_literals" :
4705 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004706 1, true);
4707 return getConstantGEP(VMContext, Entry, 0, 0);
4708}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004709
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004710/// BuildIvarLayout - Builds ivar layout bitmap for the class
4711/// implementation for the __strong or __weak case.
4712/// The layout map displays which words in ivar list must be skipped
4713/// and which must be scanned by GC (see below). String is built of bytes.
4714/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4715/// of words to skip and right nibble is count of words to scan. So, each
4716/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4717/// represented by a 0x00 byte which also ends the string.
4718/// 1. when ForStrongLayout is true, following ivars are scanned:
4719/// - id, Class
4720/// - object *
4721/// - __strong anything
4722///
4723/// 2. When ForStrongLayout is false, following ivars are scanned:
4724/// - __weak anything
4725///
4726llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4727 const ObjCImplementationDecl *OMD,
4728 bool ForStrongLayout) {
4729 bool hasUnion = false;
4730
Chris Lattner8b418682012-02-07 00:39:47 +00004731 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004732 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4733 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004734 return llvm::Constant::getNullValue(PtrTy);
4735
Jordy Rosedb8264e2011-07-22 02:08:32 +00004736 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4737 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004738 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004739 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004740 IVD; IVD = IVD->getNextIvar())
4741 RecFields.push_back(cast<FieldDecl>(IVD));
4742 }
4743 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004744 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004745 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004746
Jordy Rosedb8264e2011-07-22 02:08:32 +00004747 // FIXME: This is not ideal; we shouldn't have to do this copy.
4748 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004749 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004750
4751 if (RecFields.empty())
4752 return llvm::Constant::getNullValue(PtrTy);
4753
4754 SkipIvars.clear();
4755 IvarsInfo.clear();
4756
4757 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4758 if (IvarsInfo.empty())
4759 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004760 // Sort on byte position in case we encounterred a union nested in
4761 // the ivar list.
4762 if (hasUnion && !IvarsInfo.empty())
4763 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4764 if (hasUnion && !SkipIvars.empty())
4765 std::sort(SkipIvars.begin(), SkipIvars.end());
4766
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004767 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004768 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004769
David Blaikie4e4d0842012-03-11 07:00:24 +00004770 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004771 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004772 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004773 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004774 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004775 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004776 if (!(s[i] & 0xf0))
4777 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4778 else
4779 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4780 printf("\n");
4781 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004782 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004783}
4784
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004785llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004786 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4787
Chris Lattner0b239712012-02-06 22:16:34 +00004788 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004789 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004790 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004791 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004792 ((ObjCABI == 2) ?
4793 "__TEXT,__objc_methname,cstring_literals" :
4794 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004795 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004796
Owen Andersona1cf15f2009-07-14 23:10:40 +00004797 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004798}
4799
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004800// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004801llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004802 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4803}
4804
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004805llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004806 std::string TypeStr;
4807 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4808
4809 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004810
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004811 if (!Entry)
4812 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004813 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004814 ((ObjCABI == 2) ?
4815 "__TEXT,__objc_methtype,cstring_literals" :
4816 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004817 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004818
Owen Andersona1cf15f2009-07-14 23:10:40 +00004819 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004820}
4821
Bob Wilsondc8dab62011-11-30 01:57:58 +00004822llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4823 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004824 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004825 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004826 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004827
4828 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4829
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004830 if (!Entry)
4831 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004832 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004833 ((ObjCABI == 2) ?
4834 "__TEXT,__objc_methtype,cstring_literals" :
4835 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004836 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004837
Owen Andersona1cf15f2009-07-14 23:10:40 +00004838 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004839}
4840
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004841// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004842llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004843 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004845 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004846 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004847 llvm::ConstantDataArray::getString(VMContext,
4848 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004849 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004850 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004851
Owen Andersona1cf15f2009-07-14 23:10:40 +00004852 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004853}
4854
4855// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004856// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004857llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004858CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4859 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004860 std::string TypeStr;
4861 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004862 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4863}
4864
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004865void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004866 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004867 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004868 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004869 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004870 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4871 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004873 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004874 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004875 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004876}
4877
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004878void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004879 EmitModuleInfo();
4880
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004881 // Emit the dummy bodies for any protocols which were referenced but
4882 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004883 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004884 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4885 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004886 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004887
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004888 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004889 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004890 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004891 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004892 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004893 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004894 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004895 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004896 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004897 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004898 }
4899
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004900 // Add assembler directives to add lazy undefined symbol references
4901 // for classes which are referenced but not defined. This is
4902 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004903 //
4904 // FIXME: It would be nice if we had an LLVM construct for this.
4905 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004906 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004907 Asm += CGM.getModule().getModuleInlineAsm();
4908 if (!Asm.empty() && Asm.back() != '\n')
4909 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004910
Daniel Dunbar33063492009-09-07 00:20:42 +00004911 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004912 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4913 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004914 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4915 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004916 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004917 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004918 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004919 }
4920
Bill Wendling13562a12012-02-07 09:06:01 +00004921 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004922 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4923 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4924 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004925
Daniel Dunbar33063492009-09-07 00:20:42 +00004926 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004927 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004928}
4929
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004930CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004931 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004932 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004933 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004934 ObjCABI = 2;
4935}
4936
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004937/* *** */
4938
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004939ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004940 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4941{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004942 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4943 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004944
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004945 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004946 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004947 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004948 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004949 Int8PtrTy = CGM.Int8PtrTy;
4950 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004951
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004952 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004953 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004954 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004955
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004956 // I'm not sure I like this. The implicit coordination is a bit
4957 // gross. We should solve this in a reasonable fashion because this
4958 // is a pretty common task (match some runtime data structure with
4959 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004961 // FIXME: This is leaked.
4962 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004963
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004964 // struct _objc_super {
4965 // id self;
4966 // Class cls;
4967 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004968 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004969 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004970 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004971 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004972 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004973 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004974 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004975 Ctx.getObjCClassType(), 0, 0, false,
4976 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004977 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004978
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004979 SuperCTy = Ctx.getTagDeclType(RD);
4980 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004981
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004982 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004983 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4984
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004985 // struct _prop_t {
4986 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004987 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004988 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004989 PropertyTy = llvm::StructType::create("struct._prop_t",
4990 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004991
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004992 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004993 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004994 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004995 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004996 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004997 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004998 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4999 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005000 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005001 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005002
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005003 // struct _objc_method {
5004 // SEL _cmd;
5005 // char *method_type;
5006 // char *_imp;
5007 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005008 MethodTy = llvm::StructType::create("struct._objc_method",
5009 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5010 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005011
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005012 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005013 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005014 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005015
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005016}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005017
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005018ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005019 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005020 // struct _objc_method_description {
5021 // SEL name;
5022 // char *types;
5023 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005024 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005025 llvm::StructType::create("struct._objc_method_description",
5026 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005027
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005028 // struct _objc_method_description_list {
5029 // int count;
5030 // struct _objc_method_description[1];
5031 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005032 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005033 llvm::StructType::create("struct._objc_method_description_list",
5034 IntTy,
5035 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005037 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005038 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005039 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005040
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005041 // Protocol description structures
5042
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005043 // struct _objc_protocol_extension {
5044 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5045 // struct _objc_method_description_list *optional_instance_methods;
5046 // struct _objc_method_description_list *optional_class_methods;
5047 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005048 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005049 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005050 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005051 llvm::StructType::create("struct._objc_protocol_extension",
5052 IntTy, MethodDescriptionListPtrTy,
5053 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005054 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005056 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005057 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005058
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005059 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005060
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005061 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005062 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005063
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005064 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005065 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005066 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005067 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005068 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005069 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005070
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005071 // struct _objc_protocol {
5072 // struct _objc_protocol_extension *isa;
5073 // char *protocol_name;
5074 // struct _objc_protocol **_objc_protocol_list;
5075 // struct _objc_method_description_list *instance_methods;
5076 // struct _objc_method_description_list *class_methods;
5077 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005078 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5079 llvm::PointerType::getUnqual(ProtocolListTy),
5080 MethodDescriptionListPtrTy,
5081 MethodDescriptionListPtrTy,
5082 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005083
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005084 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005085 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005086
Owen Anderson96e0fc72009-07-29 22:16:19 +00005087 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005088
5089 // Class description structures
5090
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005091 // struct _objc_ivar {
5092 // char *ivar_name;
5093 // char *ivar_type;
5094 // int ivar_offset;
5095 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005096 IvarTy = llvm::StructType::create("struct._objc_ivar",
5097 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005098
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005099 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005100 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005101 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005102 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005103
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005104 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005105 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005106 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005107 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005108
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005109 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005110 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005111 llvm::StructType::create("struct._objc_class_extension",
5112 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005113 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005114
Chris Lattnerc1c20112011-08-12 17:43:31 +00005115 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005116
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005117 // struct _objc_class {
5118 // Class isa;
5119 // Class super_class;
5120 // char *name;
5121 // long version;
5122 // long info;
5123 // long instance_size;
5124 // struct _objc_ivar_list *ivars;
5125 // struct _objc_method_list *methods;
5126 // struct _objc_cache *cache;
5127 // struct _objc_protocol_list *protocols;
5128 // char *ivar_layout;
5129 // struct _objc_class_ext *ext;
5130 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005131 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5132 llvm::PointerType::getUnqual(ClassTy),
5133 Int8PtrTy,
5134 LongTy,
5135 LongTy,
5136 LongTy,
5137 IvarListPtrTy,
5138 MethodListPtrTy,
5139 CachePtrTy,
5140 ProtocolListPtrTy,
5141 Int8PtrTy,
5142 ClassExtensionPtrTy,
5143 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144
Owen Anderson96e0fc72009-07-29 22:16:19 +00005145 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005146
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005147 // struct _objc_category {
5148 // char *category_name;
5149 // char *class_name;
5150 // struct _objc_method_list *instance_method;
5151 // struct _objc_method_list *class_method;
5152 // uint32_t size; // sizeof(struct _objc_category)
5153 // struct _objc_property_list *instance_properties;// category's @property
5154 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005155 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005156 llvm::StructType::create("struct._objc_category",
5157 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5158 MethodListPtrTy, ProtocolListPtrTy,
5159 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005160
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005161 // Global metadata structures
5162
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005163 // struct _objc_symtab {
5164 // long sel_ref_cnt;
5165 // SEL *refs;
5166 // short cls_def_cnt;
5167 // short cat_def_cnt;
5168 // char *defs[cls_def_cnt + cat_def_cnt];
5169 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005170 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005171 llvm::StructType::create("struct._objc_symtab",
5172 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5173 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005174 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005175
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005176 // struct _objc_module {
5177 // long version;
5178 // long size; // sizeof(struct _objc_module)
5179 // char *name;
5180 // struct _objc_symtab* symtab;
5181 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005182 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005183 llvm::StructType::create("struct._objc_module",
5184 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005185
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186
Mike Stumpf5408fe2009-05-16 07:57:57 +00005187 // FIXME: This is the size of the setjmp buffer and should be target
5188 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005189 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005190
Anders Carlsson124526b2008-09-09 10:10:21 +00005191 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005192 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005193
5194 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005195 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005196 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5197 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005198
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005199}
5200
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005201ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005202 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005203 // struct _method_list_t {
5204 // uint32_t entsize; // sizeof(struct _objc_method)
5205 // uint32_t method_count;
5206 // struct _objc_method method_list[method_count];
5207 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005208 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005209 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5210 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005211 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005212 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005213
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005214 // struct _protocol_t {
5215 // id isa; // NULL
5216 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005217 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005218 // const struct method_list_t * const instance_methods;
5219 // const struct method_list_t * const class_methods;
5220 // const struct method_list_t *optionalInstanceMethods;
5221 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005222 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005223 // const uint32_t size; // sizeof(struct _protocol_t)
5224 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005225 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005226 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005227
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005228 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005229 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005230 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005231
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005232 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005233 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5234 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5235 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5236 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005237 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5238 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005239
5240 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005241 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242
Fariborz Jahanianda320092009-01-29 19:24:30 +00005243 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005244 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005245 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005246 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005247 ProtocolListnfABITy->setBody(LongTy,
5248 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5249 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005250
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005251 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005252 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005254 // struct _ivar_t {
5255 // unsigned long int *offset; // pointer to ivar offset location
5256 // char *name;
5257 // char *type;
5258 // uint32_t alignment;
5259 // uint32_t size;
5260 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005261 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005262 llvm::StructType::create("struct._ivar_t",
5263 llvm::PointerType::getUnqual(LongTy),
5264 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005265
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005266 // struct _ivar_list_t {
5267 // uint32 entsize; // sizeof(struct _ivar_t)
5268 // uint32 count;
5269 // struct _iver_t list[count];
5270 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005271 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005272 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5273 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005274
Owen Anderson96e0fc72009-07-29 22:16:19 +00005275 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005276
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005277 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005278 // uint32_t const flags;
5279 // uint32_t const instanceStart;
5280 // uint32_t const instanceSize;
5281 // uint32_t const reserved; // only when building for 64bit targets
5282 // const uint8_t * const ivarLayout;
5283 // const char *const name;
5284 // const struct _method_list_t * const baseMethods;
5285 // const struct _objc_protocol_list *const baseProtocols;
5286 // const struct _ivar_list_t *const ivars;
5287 // const uint8_t * const weakIvarLayout;
5288 // const struct _prop_list_t * const properties;
5289 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005290
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005291 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005292 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5293 IntTy, IntTy, IntTy, Int8PtrTy,
5294 Int8PtrTy, MethodListnfABIPtrTy,
5295 ProtocolListnfABIPtrTy,
5296 IvarListnfABIPtrTy,
5297 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005298
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005299 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005300 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005301 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5302 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005304 // struct _class_t {
5305 // struct _class_t *isa;
5306 // struct _class_t * const superclass;
5307 // void *cache;
5308 // IMP *vtable;
5309 // struct class_ro_t *ro;
5310 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311
Chris Lattnerc1c20112011-08-12 17:43:31 +00005312 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005313 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5314 llvm::PointerType::getUnqual(ClassnfABITy),
5315 CachePtrTy,
5316 llvm::PointerType::getUnqual(ImpnfABITy),
5317 llvm::PointerType::getUnqual(ClassRonfABITy),
5318 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005319
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005320 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005321 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005322
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005323 // struct _category_t {
5324 // const char * const name;
5325 // struct _class_t *const cls;
5326 // const struct _method_list_t * const instance_methods;
5327 // const struct _method_list_t * const class_methods;
5328 // const struct _protocol_list_t * const protocols;
5329 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005330 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005331 CategorynfABITy = llvm::StructType::create("struct._category_t",
5332 Int8PtrTy, ClassnfABIPtrTy,
5333 MethodListnfABIPtrTy,
5334 MethodListnfABIPtrTy,
5335 ProtocolListnfABIPtrTy,
5336 PropertyListPtrTy,
5337 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005338
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005339 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005340 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5341 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005343 // MessageRefTy - LLVM for:
5344 // struct _message_ref_t {
5345 // IMP messenger;
5346 // SEL name;
5347 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005348
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005349 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005350 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005351 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005352 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005353 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005354 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005355 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005356 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005357 Ctx.getObjCSelType(), 0, 0, false,
5358 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005359 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005361 MessageRefCTy = Ctx.getTagDeclType(RD);
5362 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5363 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005364
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005365 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005366 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005368 // SuperMessageRefTy - LLVM for:
5369 // struct _super_message_ref_t {
5370 // SUPER_IMP messenger;
5371 // SEL name;
5372 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005373 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005374 llvm::StructType::create("struct._super_message_ref_t",
5375 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005376
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005377 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005378 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005379
Daniel Dunbare588b992009-03-01 04:46:24 +00005380
5381 // struct objc_typeinfo {
5382 // const void** vtable; // objc_ehtype_vtable + 2
5383 // const char* name; // c++ typeinfo string
5384 // Class cls;
5385 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005386 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005387 llvm::StructType::create("struct._objc_typeinfo",
5388 llvm::PointerType::getUnqual(Int8PtrTy),
5389 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005390 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005391}
5392
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005393llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005394 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005395
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005396 return NULL;
5397}
5398
Bill Wendlingbb028552012-02-07 09:25:09 +00005399void CGObjCNonFragileABIMac::
5400AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5401 const char *SymbolName,
5402 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005403 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404
Daniel Dunbar463b8762009-05-15 21:48:48 +00005405 if (!NumClasses)
5406 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005407
Chris Lattner0b239712012-02-06 22:16:34 +00005408 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005409 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005410 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005411 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005412 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005413 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005414 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005415 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005416
Daniel Dunbar463b8762009-05-15 21:48:48 +00005417 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005418 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005419 llvm::GlobalValue::InternalLinkage,
5420 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005421 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005422 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005423 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005424 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005425}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005426
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005427void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5428 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005429
Daniel Dunbar463b8762009-05-15 21:48:48 +00005430 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005431 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005432 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005433 "\01L_OBJC_LABEL_CLASS_$",
5434 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005435
Bill Wendling13562a12012-02-07 09:06:01 +00005436 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005437 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5438 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5439 continue;
5440 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005441 }
5442
Bill Wendling13562a12012-02-07 09:06:01 +00005443 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005444 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5445 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5446 continue;
5447 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5448 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005449
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005450 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005451 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5452 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005453
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005454 // Build list of all implemented category addresses in array
5455 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005456 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005457 "\01L_OBJC_LABEL_CATEGORY_$",
5458 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005459 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005460 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5461 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005463 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005464}
5465
John McCall944c8432011-05-14 03:10:52 +00005466/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5467/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005468/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005469/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005470bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5471 // At various points we've experimented with using vtable-based
5472 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005473 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005474 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005475 return false;
John McCall944c8432011-05-14 03:10:52 +00005476 case CodeGenOptions::NonLegacy:
5477 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005478 case CodeGenOptions::Mixed:
5479 break;
5480 }
5481
5482 // If so, see whether this selector is in the white-list of things which must
5483 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005484 if (VTableDispatchMethods.empty()) {
5485 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5486 VTableDispatchMethods.insert(GetNullarySelector("class"));
5487 VTableDispatchMethods.insert(GetNullarySelector("self"));
5488 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5489 VTableDispatchMethods.insert(GetNullarySelector("length"));
5490 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005491
John McCall944c8432011-05-14 03:10:52 +00005492 // These are vtable-based if GC is disabled.
5493 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005494 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005495 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5496 VTableDispatchMethods.insert(GetNullarySelector("release"));
5497 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5498 }
5499
5500 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5501 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5502 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5503 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5504 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5505 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5506 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5507
5508 // These are vtable-based if GC is enabled.
5509 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005510 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005511 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5512 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5513
5514 // "countByEnumeratingWithState:objects:count"
5515 IdentifierInfo *KeyIdents[] = {
5516 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5517 &CGM.getContext().Idents.get("objects"),
5518 &CGM.getContext().Idents.get("count")
5519 };
5520 VTableDispatchMethods.insert(
5521 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5522 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005523 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005524
John McCall944c8432011-05-14 03:10:52 +00005525 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005526}
5527
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005528/// BuildClassRoTInitializer - generate meta-data for:
5529/// struct _class_ro_t {
5530/// uint32_t const flags;
5531/// uint32_t const instanceStart;
5532/// uint32_t const instanceSize;
5533/// uint32_t const reserved; // only when building for 64bit targets
5534/// const uint8_t * const ivarLayout;
5535/// const char *const name;
5536/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005537/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005538/// const struct _ivar_list_t *const ivars;
5539/// const uint8_t * const weakIvarLayout;
5540/// const struct _prop_list_t * const properties;
5541/// }
5542///
5543llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005544 unsigned flags,
5545 unsigned InstanceStart,
5546 unsigned InstanceSize,
5547 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005548 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005549 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005550
David Blaikie4e4d0842012-03-11 07:00:24 +00005551 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005552 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005553
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005554 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5555 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5556 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005557 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005558 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5559 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005561 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005562 // const struct _method_list_t * const baseMethods;
5563 std::vector<llvm::Constant*> Methods;
5564 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005565 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005566 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005567 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005568 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005569 // Class methods should always be defined.
5570 Methods.push_back(GetMethodConstant(*i));
5571 }
5572 } else {
5573 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005574 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005575 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005576 // Instance methods should always be defined.
5577 Methods.push_back(GetMethodConstant(*i));
5578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005579 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005580 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005581 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005582
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005583 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5584 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005585
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005586 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5587 if (llvm::Constant *C = GetMethodConstant(MD))
5588 Methods.push_back(C);
5589 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5590 if (llvm::Constant *C = GetMethodConstant(MD))
5591 Methods.push_back(C);
5592 }
5593 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005594 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005595 Values[ 5] = EmitMethodList(MethodListName,
5596 "__DATA, __objc_const", Methods);
5597
Fariborz Jahanianda320092009-01-29 19:24:30 +00005598 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5599 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005601 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005602 OID->all_referenced_protocol_begin(),
5603 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005604
John McCall621915c2012-10-17 04:53:23 +00005605 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005606 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005607 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005608 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005609 } else {
5610 Values[ 7] = EmitIvarList(ID);
5611 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005612 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5613 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005614 }
Owen Anderson08e25242009-07-27 22:29:56 +00005615 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005616 Values);
5617 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005618 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5619 llvm::GlobalValue::InternalLinkage,
5620 Init,
John McCall621915c2012-10-17 04:53:23 +00005621 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005622 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5623 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005624 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005625 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005626 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005627 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005628
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005629}
5630
5631/// BuildClassMetaData - This routine defines that to-level meta-data
5632/// for the given ClassName for:
5633/// struct _class_t {
5634/// struct _class_t *isa;
5635/// struct _class_t * const superclass;
5636/// void *cache;
5637/// IMP *vtable;
5638/// struct class_ro_t *ro;
5639/// }
5640///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005641llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005642 std::string &ClassName,
5643 llvm::Constant *IsAGV,
5644 llvm::Constant *SuperClassGV,
5645 llvm::Constant *ClassRoGV,
5646 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005647 llvm::Constant *Values[] = {
5648 IsAGV,
5649 SuperClassGV,
5650 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5651 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5652 ClassRoGV // &CLASS_RO_GV
5653 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005654 if (!Values[1])
5655 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005656 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005657 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005658 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5659 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005660 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005661 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005662 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005663 if (HiddenVisibility)
5664 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005665 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005666}
5667
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005668bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005669CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005670 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005671}
5672
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005673void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005674 uint32_t &InstanceStart,
5675 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005676 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005677 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005678
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005679 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005680 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005681
5682 // If there are no fields, the start is the same as the end.
5683 if (!RL.getFieldCount())
5684 InstanceStart = InstanceSize;
5685 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005686 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005687}
5688
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005689void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5690 std::string ClassName = ID->getNameAsString();
5691 if (!ObjCEmptyCacheVar) {
5692 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005693 CGM.getModule(),
5694 ObjCTypes.CacheTy,
5695 false,
5696 llvm::GlobalValue::ExternalLinkage,
5697 0,
5698 "_objc_empty_cache");
5699
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005700 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701 CGM.getModule(),
5702 ObjCTypes.ImpnfABITy,
5703 false,
5704 llvm::GlobalValue::ExternalLinkage,
5705 0,
5706 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005707 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005708 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005709 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005710 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005711 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005712 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005713 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005714 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005715 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5716 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005717
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005718 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719
John McCallb03527a2012-10-17 04:53:31 +00005720 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005721 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005722 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005723 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005724 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005725
5726 // FIXME: why is this flag set on the metaclass?
5727 // ObjC metaclasses have no fields and don't really get constructed.
5728 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005729 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005730 if (!ID->hasNonZeroConstructors())
5731 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5732 }
5733
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005734 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005735 // class is root
John McCall621915c2012-10-17 04:53:23 +00005736 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005737 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005738 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005739 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005740 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005741 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5742 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5743 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005744 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005745 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005746 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005747 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005748 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005749 ObjCMetaClassName +
5750 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005751 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005752 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005753 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005754 }
5755 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5756 InstanceStart,
5757 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005758 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005760 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5761 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005762 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005763
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005764 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005765 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005766 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005767 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005768
5769 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005770 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005771
John McCallb03527a2012-10-17 04:53:31 +00005772 // Set a flag to enable a runtime optimization when a class has
5773 // fields that require destruction but which don't require
5774 // anything except zero-initialization during construction. This
5775 // is most notably true of __strong and __weak types, but you can
5776 // also imagine there being C++ types with non-trivial default
5777 // constructors that merely set all fields to null.
5778 if (!ID->hasNonZeroConstructors())
5779 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5780 }
5781
Douglas Gregor68584ed2009-06-18 16:11:24 +00005782 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005783 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005784
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005785 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005786 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005787 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005788 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005789 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005790 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005791 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005792 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005793 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005794 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005795 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005796 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005797 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005798 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005799 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005800 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005801
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005802 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005803 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005804 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5805 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005806 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005807
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005808 // Determine if this class is also "non-lazy".
5809 if (ImplementationIsNonLazy(ID))
5810 DefinedNonLazyClasses.push_back(ClassMD);
5811
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005812 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005813 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005814 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005815 // Make sure method definition entries are all clear for next implementation.
5816 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005817}
5818
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005819/// GenerateProtocolRef - This routine is called to generate code for
5820/// a protocol reference expression; as in:
5821/// @code
5822/// @protocol(Proto1);
5823/// @endcode
5824/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5825/// which will hold address of the protocol meta-data.
5826///
5827llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005828 const ObjCProtocolDecl *PD) {
5829
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005830 // This routine is called for @protocol only. So, we must build definition
5831 // of protocol's meta-data (not a reference to it!)
5832 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005833 llvm::Constant *Init =
5834 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005835 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005836
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005837 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005838 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005839
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005840 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5841 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005842 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005843 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844 CGM.getModule(),
5845 Init->getType(), false,
5846 llvm::GlobalValue::WeakAnyLinkage,
5847 Init,
5848 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005849 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5850 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005851 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005852 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005853}
5854
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005855/// GenerateCategory - Build metadata for a category implementation.
5856/// struct _category_t {
5857/// const char * const name;
5858/// struct _class_t *const cls;
5859/// const struct _method_list_t * const instance_methods;
5860/// const struct _method_list_t * const class_methods;
5861/// const struct _protocol_list_t * const protocols;
5862/// const struct _prop_list_t * const properties;
5863/// }
5864///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005865void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005866 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005867 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005868 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5869 "_$_" + OCD->getNameAsString());
5870 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005871 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005872
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005873 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005874 Values[0] = GetClassName(OCD->getIdentifier());
5875 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005876 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005877 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005878 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5879
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005880 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005881 std::vector<llvm::Constant*> Methods;
5882 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005883 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005884 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005885
5886 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005887 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005888 // Instance methods should always be defined.
5889 Methods.push_back(GetMethodConstant(*i));
5890 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005891
5892 Values[2] = EmitMethodList(MethodListName,
5893 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005894 Methods);
5895
5896 MethodListName = Prefix;
5897 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5898 OCD->getNameAsString();
5899 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005900 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005901 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005902 // Class methods should always be defined.
5903 Methods.push_back(GetMethodConstant(*i));
5904 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905
5906 Values[3] = EmitMethodList(MethodListName,
5907 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005908 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005909 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005910 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005911 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005912 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005913 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5914 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005915 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005916 + Interface->getName() + "_$_"
5917 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005918 Category->protocol_begin(),
5919 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005920 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5921 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005922 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005923 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5924 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005925 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005926
5927 llvm::Constant *Init =
5928 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005929 Values);
5930 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005931 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005932 false,
5933 llvm::GlobalValue::InternalLinkage,
5934 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005935 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005936 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005937 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005938 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005939 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005940 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005941
5942 // Determine if this category is also "non-lazy".
5943 if (ImplementationIsNonLazy(OCD))
5944 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005945 // method definition entries must be clear for next implementation.
5946 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005947}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005948
5949/// GetMethodConstant - Return a struct objc_method constant for the
5950/// given method if it has been defined. The result is null if the
5951/// method has not been defined. The return value has type MethodPtrTy.
5952llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005953 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005954 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005955 if (!Fn)
5956 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005957
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005958 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005959 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005960 ObjCTypes.SelectorPtrTy),
5961 GetMethodVarType(MD),
5962 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5963 };
Owen Anderson08e25242009-07-27 22:29:56 +00005964 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005965}
5966
5967/// EmitMethodList - Build meta-data for method declarations
5968/// struct _method_list_t {
5969/// uint32_t entsize; // sizeof(struct _objc_method)
5970/// uint32_t method_count;
5971/// struct _objc_method method_list[method_count];
5972/// }
5973///
Bill Wendlingbb028552012-02-07 09:25:09 +00005974llvm::Constant *
5975CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5976 const char *Section,
5977 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005978 // Return null for empty list.
5979 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005980 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005981
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005982 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005983 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005984 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005985 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005986 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005987 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005988 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005989 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005990 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005991 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005992
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005993 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005994 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005995 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005996 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005997 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005998 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005999 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006000}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006001
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006002/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6003/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00006004llvm::GlobalVariable *
6005CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6006 const ObjCIvarDecl *Ivar) {
6007 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006008 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006009 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006010 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006011 CGM.getModule().getGlobalVariable(Name);
6012 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006013 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006014 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006015 false,
6016 llvm::GlobalValue::ExternalLinkage,
6017 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006018 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006019 return IvarOffsetGV;
6020}
6021
Daniel Dunbare83be122010-04-02 21:14:02 +00006022llvm::Constant *
6023CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6024 const ObjCIvarDecl *Ivar,
6025 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006026 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006027 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00006028 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006029 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006030 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006031
Mike Stumpf5408fe2009-05-16 07:57:57 +00006032 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6033 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006034 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6035 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006036 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006037 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006038 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006039 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006040 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006041 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006042}
6043
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006044/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006045/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006046/// IvarListnfABIPtrTy.
6047/// struct _ivar_t {
6048/// unsigned long int *offset; // pointer to ivar offset location
6049/// char *name;
6050/// char *type;
6051/// uint32_t alignment;
6052/// uint32_t size;
6053/// }
6054/// struct _ivar_list_t {
6055/// uint32 entsize; // sizeof(struct _ivar_t)
6056/// uint32 count;
6057/// struct _iver_t list[count];
6058/// }
6059///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006060
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006061llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006062 const ObjCImplementationDecl *ID) {
6063
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006064 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006065
Jordy Rosedb8264e2011-07-22 02:08:32 +00006066 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006067 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006068
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006069 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006070
Jordy Rosedb8264e2011-07-22 02:08:32 +00006071 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006072 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006073 // Ignore unnamed bit-fields.
6074 if (!IVD->getDeclName())
6075 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006076 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006077 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006078 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006079 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6080 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006081 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006082 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006083 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006084 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006085 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006086 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006087 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006088 // NOTE. Size of a bitfield does not match gcc's, because of the
6089 // way bitfields are treated special in each. But I am told that
6090 // 'size' for bitfield ivars is ignored by the runtime so it does
6091 // not matter. If it matters, there is enough info to get the
6092 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006093 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006094 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006095 }
6096 // Return null for empty list.
6097 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006098 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006099
6100 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006101 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006102 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6103 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006104 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006105 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006106 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006107 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006108 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6109 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006110 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006111 llvm::GlobalValue::InternalLinkage,
6112 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006113 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006114 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006115 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006116 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006117
Chris Lattnerad64e022009-07-17 23:57:13 +00006118 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006119 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006120}
6121
6122llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006123 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006124 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006125
Fariborz Jahanianda320092009-01-29 19:24:30 +00006126 if (!Entry) {
6127 // We use the initializer as a marker of whether this is a forward
6128 // reference or not. At module finalization we add the empty
6129 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006130 Entry =
6131 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6132 llvm::GlobalValue::ExternalLinkage,
6133 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006134 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006135 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006137
Fariborz Jahanianda320092009-01-29 19:24:30 +00006138 return Entry;
6139}
6140
6141/// GetOrEmitProtocol - Generate the protocol meta-data:
6142/// @code
6143/// struct _protocol_t {
6144/// id isa; // NULL
6145/// const char * const protocol_name;
6146/// const struct _protocol_list_t * protocol_list; // super protocols
6147/// const struct method_list_t * const instance_methods;
6148/// const struct method_list_t * const class_methods;
6149/// const struct method_list_t *optionalInstanceMethods;
6150/// const struct method_list_t *optionalClassMethods;
6151/// const struct _prop_list_t * properties;
6152/// const uint32_t size; // sizeof(struct _protocol_t)
6153/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006154/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006155/// }
6156/// @endcode
6157///
6158
6159llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006160 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006161 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006162
Fariborz Jahanianda320092009-01-29 19:24:30 +00006163 // Early exit if a defining object has already been generated.
6164 if (Entry && Entry->hasInitializer())
6165 return Entry;
6166
Douglas Gregor1d784b22012-01-01 19:51:50 +00006167 // Use the protocol definition, if there is one.
6168 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6169 PD = Def;
6170
Fariborz Jahanianda320092009-01-29 19:24:30 +00006171 // Construct method lists.
6172 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6173 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006174 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006175 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006176 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006177 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006178 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006179 if (!C)
6180 return GetOrEmitProtocolRef(PD);
6181
Fariborz Jahanianda320092009-01-29 19:24:30 +00006182 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6183 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006184 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006185 } else {
6186 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006187 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006188 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006189 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006190
6191 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006192 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006193 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006194 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006195 if (!C)
6196 return GetOrEmitProtocolRef(PD);
6197
Fariborz Jahanianda320092009-01-29 19:24:30 +00006198 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6199 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006200 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006201 } else {
6202 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006203 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006204 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006205 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006206
Bob Wilsondc8dab62011-11-30 01:57:58 +00006207 MethodTypesExt.insert(MethodTypesExt.end(),
6208 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6209
6210 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006211 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006212 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006213 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006214 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6215 PD->protocol_begin(),
6216 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006217
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006218 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006219 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006220 "__DATA, __objc_const",
6221 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006222 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006223 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006224 "__DATA, __objc_const",
6225 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006226 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006227 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006228 "__DATA, __objc_const",
6229 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006230 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006231 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006232 "__DATA, __objc_const",
6233 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006234 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006235 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006236 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006237 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006238 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006239 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006240 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6241 + PD->getName(),
6242 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006243 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006244 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006245
Fariborz Jahanianda320092009-01-29 19:24:30 +00006246 if (Entry) {
6247 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006248 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006249 Entry->setInitializer(Init);
6250 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006251 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006252 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6253 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6254 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006255 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006256 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006257 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006258
6259 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006260 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006261 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006262 CGM.AddUsedGlobal(Entry);
6263
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006264 // Use this protocol meta-data to build protocol list table in section
6265 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006266 llvm::GlobalVariable *PTGV =
6267 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6268 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6269 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006270 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006271 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006272 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006273 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006274 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006275 return Entry;
6276}
6277
6278/// EmitProtocolList - Generate protocol list meta-data:
6279/// @code
6280/// struct _protocol_list_t {
6281/// long protocol_count; // Note, this is 32/64 bit
6282/// struct _protocol_t[protocol_count];
6283/// }
6284/// @endcode
6285///
6286llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006287CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006288 ObjCProtocolDecl::protocol_iterator begin,
6289 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00006290 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006291
Fariborz Jahanianda320092009-01-29 19:24:30 +00006292 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006293 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006294 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006295
Daniel Dunbar948e2582009-02-15 07:36:20 +00006296 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006297 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006298 Name.toVector(TmpName);
6299 llvm::GlobalVariable *GV =
6300 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006301 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006302 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006303
Daniel Dunbar948e2582009-02-15 07:36:20 +00006304 for (; begin != end; ++begin)
6305 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6306
Fariborz Jahanianda320092009-01-29 19:24:30 +00006307 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006308 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006309 ObjCTypes.ProtocolnfABIPtrTy));
6310
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006311 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006312 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006313 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006314 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006315 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6316 ProtocolRefs.size()),
6317 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006318
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006319 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006320 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006321 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006322 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006323 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006324 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006325 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006326 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006327 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006328 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006329}
6330
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006331/// GetMethodDescriptionConstant - This routine build following meta-data:
6332/// struct _objc_method {
6333/// SEL _cmd;
6334/// char *method_type;
6335/// char *_imp;
6336/// }
6337
6338llvm::Constant *
6339CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006340 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006341 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006342 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6343 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006344 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006345 if (!Desc[1])
6346 return 0;
6347
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006348 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006349 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006350 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006351}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006352
6353/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6354/// This code gen. amounts to generating code for:
6355/// @code
6356/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6357/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006358///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006359LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006360 CodeGen::CodeGenFunction &CGF,
6361 QualType ObjectTy,
6362 llvm::Value *BaseValue,
6363 const ObjCIvarDecl *Ivar,
6364 unsigned CVRQualifiers) {
6365 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006366 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6367 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6368 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6369 llvm::MDNode::get(VMContext,
6370 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006371 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006372 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006373}
6374
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006375llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006376 CodeGen::CodeGenFunction &CGF,
6377 const ObjCInterfaceDecl *Interface,
6378 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006379 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006380}
6381
John McCallb1e81442011-05-13 23:16:18 +00006382static void appendSelectorForMessageRefTable(std::string &buffer,
6383 Selector selector) {
6384 if (selector.isUnarySelector()) {
6385 buffer += selector.getNameForSlot(0);
6386 return;
6387 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006388
John McCallb1e81442011-05-13 23:16:18 +00006389 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6390 buffer += selector.getNameForSlot(i);
6391 buffer += '_';
6392 }
6393}
6394
John McCall944c8432011-05-14 03:10:52 +00006395/// Emit a "v-table" message send. We emit a weak hidden-visibility
6396/// struct, initially containing the selector pointer and a pointer to
6397/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6398/// load and call the function pointer, passing the address of the
6399/// struct as the second parameter. The runtime determines whether
6400/// the selector is currently emitted using vtable dispatch; if so, it
6401/// substitutes a stub function which simply tail-calls through the
6402/// appropriate vtable slot, and if not, it substitues a stub function
6403/// which tail-calls objc_msgSend. Both stubs adjust the selector
6404/// argument to correctly point to the selector.
6405RValue
6406CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6407 ReturnValueSlot returnSlot,
6408 QualType resultType,
6409 Selector selector,
6410 llvm::Value *arg0,
6411 QualType arg0Type,
6412 bool isSuper,
6413 const CallArgList &formalArgs,
6414 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006415 // Compute the actual arguments.
6416 CallArgList args;
6417
John McCall944c8432011-05-14 03:10:52 +00006418 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006419 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006420 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6421 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006422
John McCall944c8432011-05-14 03:10:52 +00006423 // Second argument: a pointer to the message ref structure. Leave
6424 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006425 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6426
6427 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6428
John McCallde5d3c72012-02-17 03:33:10 +00006429 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006430
John McCallcba681a2011-05-14 21:12:11 +00006431 NullReturnState nullReturn;
6432
John McCall944c8432011-05-14 03:10:52 +00006433 // Find the function to call and the mangled name for the message
6434 // ref structure. Using a different mangled name wouldn't actually
6435 // be a problem; it would just be a waste.
6436 //
6437 // The runtime currently never uses vtable dispatch for anything
6438 // except normal, non-super message-sends.
6439 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006440 llvm::Constant *fn = 0;
6441 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006442 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006443 if (isSuper) {
6444 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6445 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006446 } else {
John McCallcba681a2011-05-14 21:12:11 +00006447 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006448 fn = ObjCTypes.getMessageSendStretFixupFn();
6449 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006450 }
John McCallb1e81442011-05-13 23:16:18 +00006451 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6452 fn = ObjCTypes.getMessageSendFpretFixupFn();
6453 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006454 } else {
John McCallb1e81442011-05-13 23:16:18 +00006455 if (isSuper) {
6456 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6457 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006458 } else {
John McCallb1e81442011-05-13 23:16:18 +00006459 fn = ObjCTypes.getMessageSendFixupFn();
6460 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006461 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006462 }
John McCallb1e81442011-05-13 23:16:18 +00006463 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6464 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006465
John McCallb1e81442011-05-13 23:16:18 +00006466 // Append the selector name, except use underscores anywhere we
6467 // would have used colons.
6468 appendSelectorForMessageRefTable(messageRefName, selector);
6469
6470 llvm::GlobalVariable *messageRef
6471 = CGM.getModule().getGlobalVariable(messageRefName);
6472 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006473 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006474 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006475 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006476 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6477 init->getType(),
6478 /*constant*/ false,
6479 llvm::GlobalValue::WeakAnyLinkage,
6480 init,
6481 messageRefName);
6482 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6483 messageRef->setAlignment(16);
6484 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6485 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006486
6487 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006488 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006489 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6490 e = method->param_end(); i != e; ++i) {
6491 const ParmVarDecl *ParamDecl = (*i);
6492 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6493 if (!nullReturn.NullBB)
6494 nullReturn.init(CGF, arg0);
6495 requiresnullCheck = true;
6496 break;
6497 }
6498 }
6499
John McCallb1e81442011-05-13 23:16:18 +00006500 llvm::Value *mref =
6501 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6502
John McCall944c8432011-05-14 03:10:52 +00006503 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006504 args[1].RV = RValue::get(mref);
6505
6506 // Load the function to call from the message ref table.
6507 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6508 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6509
John McCallde5d3c72012-02-17 03:33:10 +00006510 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006511
John McCallde5d3c72012-02-17 03:33:10 +00006512 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006513 return nullReturn.complete(CGF, result, resultType, formalArgs,
6514 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006515}
6516
6517/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006518CodeGen::RValue
6519CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006520 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006521 QualType ResultType,
6522 Selector Sel,
6523 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006524 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006525 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006526 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006527 return isVTableDispatchedSelector(Sel)
6528 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006529 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006530 false, CallArgs, Method)
6531 : EmitMessageSend(CGF, Return, ResultType,
6532 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006533 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006534 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006535}
6536
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006537llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006538CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006539 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6540
Daniel Dunbardfff2302009-03-02 05:18:14 +00006541 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006542 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006543 false, llvm::GlobalValue::ExternalLinkage,
6544 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006545 }
6546
6547 return GV;
6548}
6549
John McCallf85e1932011-06-15 23:02:42 +00006550llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6551 IdentifierInfo *II) {
6552 llvm::GlobalVariable *&Entry = ClassReferences[II];
6553
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006554 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006555 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006556 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006557 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006558 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6559 false, llvm::GlobalValue::InternalLinkage,
6560 ClassGV,
6561 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006562 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006563 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006564 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006565 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006566 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006567 }
John McCallf85e1932011-06-15 23:02:42 +00006568
Benjamin Kramer578faa82011-09-27 21:06:10 +00006569 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006570}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006571
John McCallf85e1932011-06-15 23:02:42 +00006572llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6573 const ObjCInterfaceDecl *ID) {
6574 return EmitClassRefFromId(Builder, ID->getIdentifier());
6575}
6576
6577llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6578 CGBuilderTy &Builder) {
6579 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6580 return EmitClassRefFromId(Builder, II);
6581}
6582
Daniel Dunbar11394522009-04-18 08:51:00 +00006583llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006584CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006585 const ObjCInterfaceDecl *ID) {
6586 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006587
Daniel Dunbar11394522009-04-18 08:51:00 +00006588 if (!Entry) {
6589 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6590 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006591 Entry =
6592 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006593 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006594 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006595 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006596 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006597 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006598 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006599 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006600 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006601 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006602
Benjamin Kramer578faa82011-09-27 21:06:10 +00006603 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006604}
6605
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006606/// EmitMetaClassRef - Return a Value * of the address of _class_t
6607/// meta-data
6608///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006609llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6610 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006611 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6612 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006613 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006614
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006615 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006616 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006617 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006618 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006619 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006620 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006621 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006622 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006623 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006624 ObjCTypes.ClassnfABIPtrTy));
6625
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006626 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006627 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006628
Benjamin Kramer578faa82011-09-27 21:06:10 +00006629 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006630}
6631
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006632/// GetClass - Return a reference to the class for the given interface
6633/// decl.
6634llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6635 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006636 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006637 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6638 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6639 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6640 }
6641
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006642 return EmitClassRef(Builder, ID);
6643}
6644
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006645/// Generates a message send where the super is the receiver. This is
6646/// a message send to self with special delivery semantics indicating
6647/// which class's method should be called.
6648CodeGen::RValue
6649CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006650 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006651 QualType ResultType,
6652 Selector Sel,
6653 const ObjCInterfaceDecl *Class,
6654 bool isCategoryImpl,
6655 llvm::Value *Receiver,
6656 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006657 const CodeGen::CallArgList &CallArgs,
6658 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006659 // ...
6660 // Create and init a super structure; this is a (receiver, class)
6661 // pair we will pass to objc_msgSendSuper.
6662 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006663 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006664
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006665 llvm::Value *ReceiverAsObject =
6666 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6667 CGF.Builder.CreateStore(ReceiverAsObject,
6668 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006669
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006670 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006671 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006672 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006673 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006674 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006675 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006676
Mike Stumpf5408fe2009-05-16 07:57:57 +00006677 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6678 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006679 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006680 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6681 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6682 CGF.Builder.CreateStore(Target,
6683 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006684
John McCall944c8432011-05-14 03:10:52 +00006685 return (isVTableDispatchedSelector(Sel))
6686 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006687 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006688 true, CallArgs, Method)
6689 : EmitMessageSend(CGF, Return, ResultType,
6690 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006691 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006692 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006693}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006694
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006695llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006696 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006697 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006698
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006699 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006700 llvm::Constant *Casted =
6701 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6702 ObjCTypes.SelectorPtrTy);
6703 Entry =
6704 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6705 llvm::GlobalValue::InternalLinkage,
6706 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006707 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006708 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006709 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006710
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006711 if (lval)
6712 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006713 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6714
6715 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6716 llvm::MDNode::get(VMContext,
6717 ArrayRef<llvm::Value*>()));
6718 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006719}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006720/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006721/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006722///
6723void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006724 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006725 llvm::Value *dst,
6726 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006727 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006728 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006729 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006730 assert(Size <= 8 && "does not support size > 8");
6731 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6732 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006733 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6734 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006735 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6736 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006737 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6738 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006739 return;
6740}
6741
6742/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6743/// objc_assign_strongCast (id src, id *dst)
6744///
6745void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006746 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006747 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006748 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006749 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006750 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006751 assert(Size <= 8 && "does not support size > 8");
6752 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006753 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006754 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6755 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006756 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6757 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006758 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006759 src, dst, "weakassign");
6760 return;
6761}
6762
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006763void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006764 CodeGen::CodeGenFunction &CGF,
6765 llvm::Value *DestPtr,
6766 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006767 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006768 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6769 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006770 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006771 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006772 return;
6773}
6774
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006775/// EmitObjCWeakRead - Code gen for loading value of a __weak
6776/// object: objc_read_weak (id *src)
6777///
6778llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006779 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006780 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006781 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006782 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6783 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006784 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006785 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006786 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006787 return read_weak;
6788}
6789
6790/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6791/// objc_assign_weak (id src, id *dst)
6792///
6793void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006794 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006795 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006796 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006797 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006798 assert(Size <= 8 && "does not support size > 8");
6799 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6800 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006801 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6802 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006803 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6804 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006805 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006806 src, dst, "weakassign");
6807 return;
6808}
6809
6810/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6811/// objc_assign_global (id src, id *dst)
6812///
6813void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006814 llvm::Value *src, llvm::Value *dst,
6815 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006816 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006817 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006818 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006819 assert(Size <= 8 && "does not support size > 8");
6820 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6821 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006822 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6823 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006824 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6825 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006826 if (!threadlocal)
6827 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6828 src, dst, "globalassign");
6829 else
6830 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6831 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006832 return;
6833}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006834
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006835void
John McCallf1549f62010-07-06 01:34:17 +00006836CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6837 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006838 EmitAtSynchronizedStmt(CGF, S,
6839 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6840 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006841}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006842
John McCall5a180392010-07-24 00:37:23 +00006843llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006844CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006845 // There's a particular fixed type info for 'id'.
6846 if (T->isObjCIdType() ||
6847 T->isObjCQualifiedIdType()) {
6848 llvm::Constant *IDEHType =
6849 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6850 if (!IDEHType)
6851 IDEHType =
6852 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6853 false,
6854 llvm::GlobalValue::ExternalLinkage,
6855 0, "OBJC_EHTYPE_id");
6856 return IDEHType;
6857 }
6858
6859 // All other types should be Objective-C interface pointer types.
6860 const ObjCObjectPointerType *PT =
6861 T->getAs<ObjCObjectPointerType>();
6862 assert(PT && "Invalid @catch type.");
6863 const ObjCInterfaceType *IT = PT->getInterfaceType();
6864 assert(IT && "Invalid @catch type.");
6865 return GetInterfaceEHType(IT->getDecl(), false);
6866}
6867
John McCallf1549f62010-07-06 01:34:17 +00006868void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6869 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006870 EmitTryCatchStmt(CGF, S,
6871 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6872 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6873 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006874}
6875
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006876/// EmitThrowStmt - Generate code for a throw statement.
6877void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6878 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006879 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006880 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006881 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006882 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006883 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006884 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006885 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006886 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006887 }
6888
John McCall7ec404c2010-10-16 08:21:07 +00006889 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006890 CGF.Builder.ClearInsertionPoint();
6891}
Daniel Dunbare588b992009-03-01 04:46:24 +00006892
John McCall5a180392010-07-24 00:37:23 +00006893llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006894CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006895 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006896 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006897
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006898 // If we don't need a definition, return the entry if found or check
6899 // if we use an external reference.
6900 if (!ForDefinition) {
6901 if (Entry)
6902 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006903
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006904 // If this type (or a super class) has the __objc_exception__
6905 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006906 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006907 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006908 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006909 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006910 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006911 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006912 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006913 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006914
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006915 // Otherwise we need to either make a new entry or fill in the
6916 // initializer.
6917 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006918 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006919 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006920 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006921 CGM.getModule().getGlobalVariable(VTableName);
6922 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006923 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6924 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006925 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006926 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006927
Chris Lattner8b418682012-02-07 00:39:47 +00006928 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006929
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006930 llvm::Constant *Values[] = {
6931 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6932 GetClassName(ID->getIdentifier()),
6933 GetClassGlobal(ClassName)
6934 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006935 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006936 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006937
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006938 if (Entry) {
6939 Entry->setInitializer(Init);
6940 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006941 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006942 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006943 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006944 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006945 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006946 }
6947
David Blaikie4e4d0842012-03-11 07:00:24 +00006948 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006949 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006950 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006951 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006952
6953 if (ForDefinition) {
6954 Entry->setSection("__DATA,__objc_const");
6955 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6956 } else {
6957 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6958 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006959
6960 return Entry;
6961}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006962
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006963/* *** */
6964
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006965CodeGen::CGObjCRuntime *
6966CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006967 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6968 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006969 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006970
6971 case ObjCRuntime::MacOSX:
6972 case ObjCRuntime::iOS:
6973 return new CGObjCNonFragileABIMac(CGM);
6974
David Chisnall11d3f4c2012-07-03 20:49:52 +00006975 case ObjCRuntime::GNUstep:
6976 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006977 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006978 llvm_unreachable("these runtimes are not Mac runtimes");
6979 }
6980 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006981}