blob: 6c0b82bb264a9038bff87ffb62e54bf404f57095 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000039#include "llvm/DataLayout.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046
Daniel Dunbar6bff2512009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000049
Fariborz Jahanianee0af742009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000053
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000054private:
John McCall0774cb82011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCallf85e1932011-06-15 23:02:42 +000068 "objc_msgSend",
Bill Wendling50e6b182012-10-15 04:47:45 +000069 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +000070 llvm::Attributes::NonLazyBind));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000071 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000072
John McCall0774cb82011-05-15 01:53:33 +000073 /// void objc_msgSend_stret (id, SEL, ...)
74 ///
75 /// The messenger used when the return value is an aggregate returned
76 /// by indirect reference in the first argument, and therefore the
77 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000078 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000079 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000080 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
81 params, true),
82 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000083
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000084 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
John McCall0774cb82011-05-15 01:53:33 +000086 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
87 ///
88 /// The messenger used when the return value is returned on the x87
89 /// floating-point stack; without a special entrypoint, the nil case
90 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000092 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000093 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
94 params, true),
John McCall0774cb82011-05-15 01:53:33 +000095 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000096
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000097 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000098
Anders Carlssoneea64802011-10-31 16:27:11 +000099 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
100 ///
101 /// The messenger used when the return value is returned in two values on the
102 /// x87 floating point stack; without a special entrypoint, the nil case
103 /// would be unbalanced. Only used on 64-bit X86.
104 llvm::Constant *getMessageSendFp2retFn() const {
105 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
106 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
107 llvm::Type *resultType =
108 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
109
110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
111 params, true),
112 "objc_msgSend_fp2ret");
113 }
114
John McCall0774cb82011-05-15 01:53:33 +0000115 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
116 ///
117 /// The messenger used for super calls, which have different dispatch
118 /// semantics. The class passed is the superclass of the current
119 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000120 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000121 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000122 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000123 params, true),
124 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000126
John McCall0774cb82011-05-15 01:53:33 +0000127 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
128 ///
129 /// A slightly different messenger used for super calls. The class
130 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000131 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000132 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000133 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000134 params, true),
135 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000137
John McCall0774cb82011-05-15 01:53:33 +0000138 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
139 /// SEL op, ...)
140 ///
141 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000142 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000143 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000144 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000145 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000148
John McCall0774cb82011-05-15 01:53:33 +0000149 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
150 /// SEL op, ...)
151 ///
152 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000153 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000155 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000156 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000158 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160 llvm::Constant *getMessageSendSuperFpretFn() const {
161 // There is no objc_msgSendSuper_fpret? How can that work?
162 return getMessageSendSuperFn();
163 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000164
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 llvm::Constant *getMessageSendSuperFpretFn2() const {
166 // There is no objc_msgSendSuper_fpret? How can that work?
167 return getMessageSendSuperFn2();
168 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000170protected:
171 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000173public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000175 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000176
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000177 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000178 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000180 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000181 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000183 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000185
186private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000187 /// ProtocolPtrTy - LLVM type for external protocol handles
188 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000189 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000190
191public:
192 llvm::Type *getExternalProtocolPtrTy() {
193 if (!ExternalProtocolPtrTy) {
194 // FIXME: It would be nice to unify this with the opaque type, so that the
195 // IR comes out a bit cleaner.
196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
197 ASTContext &Ctx = CGM.getContext();
198 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
199 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
200 }
201
202 return ExternalProtocolPtrTy;
203 }
204
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000205 // SuperCTy - clang type for struct objc_super.
206 QualType SuperCTy;
207 // SuperPtrCTy - clang type for struct objc_super *.
208 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000209
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000210 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000211 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000212 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
216 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000217 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000219 /// PropertyListTy - LLVM type for struct objc_property_list
220 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000221 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000224
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000225 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000226 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000228 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000230 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000232
Chris Lattner72db6c32009-04-22 02:44:54 +0000233 llvm::Constant *getGetPropertyFn() {
234 CodeGen::CodeGenTypes &Types = CGM.getTypes();
235 ASTContext &Ctx = CGM.getContext();
236 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000238 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
239 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000240 Params.push_back(IdType);
241 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000242 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000243 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000244 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000245 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
246 FunctionType::ExtInfo(),
247 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Chris Lattner72db6c32009-04-22 02:44:54 +0000251 llvm::Constant *getSetPropertyFn() {
252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
253 ASTContext &Ctx = CGM.getContext();
254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000256 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
257 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000258 Params.push_back(IdType);
259 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000260 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(Ctx.BoolTy);
263 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000264 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000265 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
266 FunctionType::ExtInfo(),
267 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000268 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
269 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000271 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_setProperty_atomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_nonatomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282
283 SmallVector<CanQualType,4> Params;
284 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
285 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
286 Params.push_back(IdType);
287 Params.push_back(SelType);
288 Params.push_back(IdType);
289 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
290 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000291 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
292 FunctionType::ExtInfo(),
293 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000294 const char *name;
295 if (atomic && copy)
296 name = "objc_setProperty_atomic_copy";
297 else if (atomic && !copy)
298 name = "objc_setProperty_atomic";
299 else if (!atomic && copy)
300 name = "objc_setProperty_nonatomic_copy";
301 else
302 name = "objc_setProperty_nonatomic";
303
304 return CGM.CreateRuntimeFunction(FTy, name);
305 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000306
307 llvm::Constant *getCopyStructFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000318 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
319 FunctionType::ExtInfo(),
320 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
322 }
323
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000324 /// This routine declares and returns address of:
325 /// void objc_copyCppObjectAtomic(
326 /// void *dest, const void *src,
327 /// void (*copyHelper) (void *dest, const void *source));
328 llvm::Constant *getCppAtomicObjectFunction() {
329 CodeGen::CodeGenTypes &Types = CGM.getTypes();
330 ASTContext &Ctx = CGM.getContext();
331 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
332 SmallVector<CanQualType,3> Params;
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000337 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
338 FunctionType::ExtInfo(),
339 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
341 }
342
Chris Lattner72db6c32009-04-22 02:44:54 +0000343 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000346 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000348 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000349 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000350 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000351 FunctionType::ExtInfo(),
352 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
354 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000355
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000357 llvm::Constant *getGcReadWeakFn() {
358 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000361 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000363 }
364
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000365 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000366 llvm::Constant *getGcAssignWeakFn() {
367 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000368 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000369 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000370 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignGlobalFn() {
376 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
381 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000383 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
384 llvm::Constant *getGcAssignThreadLocalFn() {
385 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000387 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000388 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
390 }
391
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000392 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000393 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000394 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000395 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
396 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000398 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000402 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
403 llvm::Constant *GcMemmoveCollectableFn() {
404 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000406 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000410 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000411 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000412 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000413 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000414 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000415 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
417 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000418
419 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000420 llvm::Constant *getExceptionThrowFn() {
421 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000422 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000424 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
426 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000427
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000428 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
429 llvm::Constant *getExceptionRethrowFn() {
430 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000431 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
433 }
434
Daniel Dunbar1c566672009-02-24 01:43:46 +0000435 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000436 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000437 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000439 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000440 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000443
Daniel Dunbar1c566672009-02-24 01:43:46 +0000444 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000446 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000449 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
451 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000453 llvm::Constant *getSendFn(bool IsSuper) const {
454 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000456
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000457 llvm::Constant *getSendFn2(bool IsSuper) const {
458 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000461 llvm::Constant *getSendStretFn(bool IsSuper) const {
462 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
463 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000464
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000465 llvm::Constant *getSendStretFn2(bool IsSuper) const {
466 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
467 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000468
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000469 llvm::Constant *getSendFpretFn(bool IsSuper) const {
470 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000473 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
474 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
475 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000476
Anders Carlssoneea64802011-10-31 16:27:11 +0000477 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
478 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
479 }
480
481 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
482 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
483 }
484
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000485 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
486 ~ObjCCommonTypesHelper(){}
487};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000488
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000489/// ObjCTypesHelper - Helper class that encapsulates lazy
490/// construction of varies types used during ObjC generation.
491class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000492public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000493 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000497 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000499
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000500 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000503 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504 /// ProtocolExtensionTy - LLVM type for struct
505 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 /// ProtocolExtensionTy - LLVM type for struct
508 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000510 /// MethodDescriptionTy - LLVM type for struct
511 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000513 /// MethodDescriptionListTy - LLVM type for struct
514 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// MethodDescriptionListPtrTy - LLVM type for struct
517 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000523 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Anders Carlsson124526b2008-09-09 10:10:21 +0000544 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000546
Anders Carlsson124526b2008-09-09 10:10:21 +0000547 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000548 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000549 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000550 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000551 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000553 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000554
555 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000558 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000559 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000561 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000562
563 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000564 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000566 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000567 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000568 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Anders Carlsson124526b2008-09-09 10:10:21 +0000571 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000572 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000573 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000575 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 "objc_exception_match");
577
Chris Lattner34b02a12009-04-22 02:26:14 +0000578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Anders Carlsson124526b2008-09-09 10:10:21 +0000580 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000582 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000584 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
585 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000586 "_setjmp",
Bill Wendling50e6b182012-10-15 04:47:45 +0000587 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +0000588 llvm::Attributes::NonLazyBind));
Chris Lattner34b02a12009-04-22 02:26:14 +0000589 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000591public:
592 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000593 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000594};
595
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000596/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000599public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000601 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000604 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000605 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000607 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000608 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Daniel Dunbar948e2582009-02-15 07:36:20 +0000610 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000611 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000612
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000613 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000614 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000615
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000616 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000617 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000619 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000620 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000622 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000623 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000625 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000626 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000628 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000629 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000632 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000635 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000638 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000640 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000641 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000643 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000645 // MessageRefTy - LLVM for:
646 // struct _message_ref_t {
647 // IMP messenger;
648 // SEL name;
649 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000650 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000651 // MessageRefCTy - clang type for struct _message_ref_t
652 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000654 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000655 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000656 // MessageRefCPtrTy - clang type for struct _message_ref_t*
657 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahanianef163782009-02-05 01:13:09 +0000659 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000660 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000662 // SuperMessageRefTy - LLVM for:
663 // struct _super_message_ref_t {
664 // SUPER_IMP messenger;
665 // SEL name;
666 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000667 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000669 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000670 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000671
Chris Lattner1c02f862009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendFixupFn() {
673 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000674 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000676 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000677 "objc_msgSend_fixup");
678 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Chris Lattner1c02f862009-04-22 02:53:24 +0000680 llvm::Constant *getMessageSendFpretFixupFn() {
681 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000682 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000684 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000685 "objc_msgSend_fpret_fixup");
686 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000687
Chris Lattner1c02f862009-04-22 02:53:24 +0000688 llvm::Constant *getMessageSendStretFixupFn() {
689 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000692 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000693 "objc_msgSend_stret_fixup");
694 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000699 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000700 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000701 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 "objc_msgSendSuper2_fixup");
703 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Chris Lattner1c02f862009-04-22 02:53:24 +0000705 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000708 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000710 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000711 "objc_msgSendSuper2_stret_fixup");
712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000713
Chris Lattner8a569112009-04-22 02:15:23 +0000714 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000716 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner8a569112009-04-22 02:15:23 +0000720 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000721 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000723 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000724 "objc_begin_catch");
725 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000726
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000727 llvm::StructType *EHTypeTy;
728 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000729
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000730 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
731 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000733
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000735public:
736 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000737 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000738 public:
Eli Friedman38ad4f42012-11-06 01:51:20 +0000739 CharUnits ivar_bytepos;
Eli Friedman8cb809a2012-11-06 03:41:04 +0000740 CharUnits ivar_size;
741 GC_IVAR(CharUnits bytepos = CharUnits::Zero(),
742 CharUnits size = CharUnits::Zero())
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000743 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000744
745 // Allow sorting based on byte pos.
746 bool operator<(const GC_IVAR &b) const {
747 return ivar_bytepos < b.ivar_bytepos;
748 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000749 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000750
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000751 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000752 public:
753 unsigned skip;
754 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000755 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000756 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000757 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000758
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000759 /// opcode for captured block variables layout 'instructions'.
760 /// In the following descriptions, 'I' is the value of the immediate field.
761 /// (field following the opcode).
762 ///
763 enum BLOCK_LAYOUT_OPCODE {
764 /// An operator which affects how the following layout should be
765 /// interpreted.
766 /// I == 0: Halt interpretation and treat everything else as
767 /// a non-pointer. Note that this instruction is equal
768 /// to '\0'.
769 /// I != 0: Currently unused.
770 BLOCK_LAYOUT_OPERATOR = 0,
771
772 /// The next I+1 bytes do not contain a value of object pointer type.
773 /// Note that this can leave the stream unaligned, meaning that
774 /// subsequent word-size instructions do not begin at a multiple of
775 /// the pointer size.
776 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
777
778 /// The next I+1 words do not contain a value of object pointer type.
779 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
780 /// when the required skip quantity is a multiple of the pointer size.
781 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
782
783 /// The next I+1 words are __strong pointers to Objective-C
784 /// objects or blocks.
785 BLOCK_LAYOUT_STRONG = 3,
786
787 /// The next I+1 words are pointers to __block variables.
788 BLOCK_LAYOUT_BYREF = 4,
789
790 /// The next I+1 words are __weak pointers to Objective-C
791 /// objects or blocks.
792 BLOCK_LAYOUT_WEAK = 5,
793
794 /// The next I+1 words are __unsafe_unretained pointers to
795 /// Objective-C objects or blocks.
796 BLOCK_LAYOUT_UNRETAINED = 6
797
798 /// The next I+1 words are block or object pointers with some
799 /// as-yet-unspecified ownership semantics. If we add more
800 /// flavors of ownership semantics, values will be taken from
801 /// this range.
802 ///
803 /// This is included so that older tools can at least continue
804 /// processing the layout past such things.
805 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
806
807 /// All other opcodes are reserved. Halt interpretation and
808 /// treat everything else as opaque.
809 };
810
811 class RUN_SKIP {
812 public:
813 enum BLOCK_LAYOUT_OPCODE opcode;
Eli Friedman8cb809a2012-11-06 03:41:04 +0000814 CharUnits block_var_bytepos;
815 CharUnits block_var_size;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000816 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Eli Friedman8cb809a2012-11-06 03:41:04 +0000817 CharUnits BytePos = CharUnits::Zero(),
818 CharUnits Size = CharUnits::Zero())
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000819 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000820
821 // Allow sorting based on byte pos.
822 bool operator<(const RUN_SKIP &b) const {
823 return block_var_bytepos < b.block_var_bytepos;
824 }
825 };
826
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000827protected:
Owen Anderson69243822009-07-13 04:10:07 +0000828 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000829 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000830 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000831
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000832 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000833 SmallVector<GC_IVAR, 16> SkipIvars;
834 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000835
836 // arc/mrr layout of captured block literal variables.
837 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000839 /// LazySymbols - Symbols to generate a lazy reference for. See
840 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000841 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000842
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000843 /// DefinedSymbols - External symbols which are defined by this
844 /// module. The symbols in this list and LazySymbols are used to add
845 /// special linker symbols which ensure that Objective-C modules are
846 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000847 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000849 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000850 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000852 /// MethodVarNames - uniqued method variable names.
853 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000855 /// DefinedCategoryNames - list of category names in form Class_Category.
856 llvm::SetVector<std::string> DefinedCategoryNames;
857
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000858 /// MethodVarTypes - uniqued method type signatures. We have to use
859 /// a StringMap here because have no other unique reference.
860 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000861
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000862 /// MethodDefinitions - map of methods which have been defined in
863 /// this translation unit.
864 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000865
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000866 /// PropertyNames - uniqued method variable names.
867 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000869 /// ClassReferences - uniqued class references.
870 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000871
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000872 /// SelectorReferences - uniqued selector references.
873 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000874
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000875 /// Protocols - Protocols for which an objc_protocol structure has
876 /// been emitted. Forward declarations are handled by creating an
877 /// empty structure whose initializer is filled in when/if defined.
878 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000879
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000880 /// DefinedProtocols - Protocols which have actually been
881 /// defined. We should not need this, see FIXME in GenerateProtocol.
882 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000884 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000885 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000886
887 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000888 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000889
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000890 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000891 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000892
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000893 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000894 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000895
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000896 /// GetNameForMethod - Return a name for the given method.
897 /// \param[out] NameOut - The return value.
898 void GetNameForMethod(const ObjCMethodDecl *OMD,
899 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000900 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000901
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000902 /// GetMethodVarName - Return a unique constant for the given
903 /// selector's name. The return value has type char *.
904 llvm::Constant *GetMethodVarName(Selector Sel);
905 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000906
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000907 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000908 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000909
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000910 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000911 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
912 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000913 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000915 /// GetPropertyName - Return a unique constant for the given
916 /// name. The return value has type char *.
917 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000918
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000919 // FIXME: This can be dropped once string functions are unified.
920 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
921 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000922
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000923 /// GetClassName - Return a unique constant for the given selector's
924 /// name. The return value has type char *.
925 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000926
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000927 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
928
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000929 /// BuildIvarLayout - Builds ivar layout bitmap for the class
930 /// implementation for the __strong or __weak case.
931 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000932 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
933 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000934
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000935 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000937 void BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman38ad4f42012-11-06 01:51:20 +0000938 CharUnits BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000939 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000940 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000941 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000942 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000943 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman38ad4f42012-11-06 01:51:20 +0000944 CharUnits BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000945 bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000946
Fariborz Jahanianc441cd32012-11-04 18:19:40 +0000947 Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT);
Fariborz Jahanian44fcff92012-11-02 22:51:18 +0000948
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000949 void UpdateRunSkipBlockVars(bool IsByref,
950 Qualifiers::ObjCLifetime LifeTime,
Eli Friedman8cb809a2012-11-06 03:41:04 +0000951 CharUnits FieldOffset,
952 CharUnits FieldSize);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000953
954 void BuildRCBlockVarRecordLayout(const RecordType *RT,
Eli Friedman8cb809a2012-11-06 03:41:04 +0000955 CharUnits BytePos, bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000956
957 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
958 const RecordDecl *RD,
959 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cb809a2012-11-06 03:41:04 +0000960 CharUnits BytePos, bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000961
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000962 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
963
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000964
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000965 /// GetIvarLayoutName - Returns a unique constant for the given
966 /// ivar layout bitmap.
967 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
968 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000969
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000970 /// EmitPropertyList - Emit the given property list. The return
971 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000972 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000973 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000974 const ObjCContainerDecl *OCD,
975 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000976
Bob Wilsondc8dab62011-11-30 01:57:58 +0000977 /// EmitProtocolMethodTypes - Generate the array of extended method type
978 /// strings. The return value has type Int8PtrPtrTy.
979 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000980 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000981 const ObjCCommonTypesHelper &ObjCTypes);
982
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000983 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000984 void PushProtocolProperties(
985 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
986 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
987 const Decl *Container,
988 const ObjCProtocolDecl *PROTO,
989 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000990
Fariborz Jahanianda320092009-01-29 19:24:30 +0000991 /// GetProtocolRef - Return a reference to the internal protocol
992 /// description, creating an empty one if it has not been
993 /// defined. The return value has type ProtocolPtrTy.
994 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000995
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000996 /// CreateMetadataVar - Create a global variable with internal
997 /// linkage for use by the Objective-C runtime.
998 ///
999 /// This is a convenience wrapper which not only creates the
1000 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +00001001 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001002 ///
1003 /// \param Name - The variable name.
1004 /// \param Init - The variable initializer; this is also used to
1005 /// define the type of the variable.
1006 /// \param Section - The section the variable should go into, or 0.
1007 /// \param Align - The alignment for the variable, or 0.
1008 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +00001009 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +00001010 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001011 llvm::Constant *Init,
1012 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001013 unsigned Align,
1014 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001015
John McCall944c8432011-05-14 03:10:52 +00001016 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1017 ReturnValueSlot Return,
1018 QualType ResultType,
1019 llvm::Value *Sel,
1020 llvm::Value *Arg0,
1021 QualType Arg0Ty,
1022 bool IsSuper,
1023 const CallArgList &CallArgs,
1024 const ObjCMethodDecl *OMD,
1025 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001026
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001027 /// EmitImageInfo - Emit the image info marker used to encode some module
1028 /// level information.
1029 void EmitImageInfo();
1030
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001031public:
Owen Anderson69243822009-07-13 04:10:07 +00001032 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +00001033 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001034
David Chisnall0d13f6f2010-01-23 02:40:42 +00001035 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001036
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001037 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1038 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001039
Fariborz Jahanianda320092009-01-29 19:24:30 +00001040 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001041
Fariborz Jahanianda320092009-01-29 19:24:30 +00001042 /// GetOrEmitProtocol - Get the protocol object for the given
1043 /// declaration, emitting it if necessary. The return value has type
1044 /// ProtocolPtrTy.
1045 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001046
Fariborz Jahanianda320092009-01-29 19:24:30 +00001047 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1048 /// object for the given declaration, emitting it if needed. These
1049 /// forward references will be filled in with empty bodies if no
1050 /// definition is seen. The return value has type ProtocolPtrTy.
1051 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001052 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1053 const CGBlockInfo &blockInfo);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001054 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1055 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001056
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001057};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001058
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001059class CGObjCMac : public CGObjCCommonMac {
1060private:
1061 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001062
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001063 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001064 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001065 void EmitModuleInfo();
1066
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001067 /// EmitModuleSymols - Emit module symbols, the list of defined
1068 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001069 llvm::Constant *EmitModuleSymbols();
1070
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001071 /// FinishModule - Write out global data structures at the end of
1072 /// processing a translation unit.
1073 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001074
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001075 /// EmitClassExtension - Generate the class extension structure used
1076 /// to store the weak ivar layout and properties. The return value
1077 /// has type ClassExtensionPtrTy.
1078 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1079
1080 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1081 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001082 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001083 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001084
John McCallf85e1932011-06-15 23:02:42 +00001085 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1086 IdentifierInfo *II);
1087
1088 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1089
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001090 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1091 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001092
1093 /// EmitIvarList - Emit the ivar list for the given
1094 /// implementation. If ForClass is true the list of class ivars
1095 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1096 /// interface ivars will be emitted. The return value has type
1097 /// IvarListPtrTy.
1098 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001099 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001100
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001101 /// EmitMetaClass - Emit a forward reference to the class structure
1102 /// for the metaclass of the given interface. The return value has
1103 /// type ClassPtrTy.
1104 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1105
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001106 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001107 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001108 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1109 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001110 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001111
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001112 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001113
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001114 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001115
1116 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001117 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001118 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001119 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001120 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001121
1122 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001123 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001124 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001125 /// - IsProtocol: True iff these methods are for a protocol.
1126 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001127 /// - Required: When true, only "required" methods are
1128 /// listed. Similarly, when false only "optional" methods are
1129 /// listed. For classes this should always be true.
1130 /// - begin, end: The method list to output.
1131 ///
1132 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001133 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001134 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001135 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001136
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001137 /// GetOrEmitProtocol - Get the protocol object for the given
1138 /// declaration, emitting it if necessary. The return value has type
1139 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001140 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001141
1142 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1143 /// object for the given declaration, emitting it if needed. These
1144 /// forward references will be filled in with empty bodies if no
1145 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001146 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001147
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001148 /// EmitProtocolExtension - Generate the protocol extension
1149 /// structure used to store optional instance and class methods, and
1150 /// protocol properties. The return value has type
1151 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001152 llvm::Constant *
1153 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001154 ArrayRef<llvm::Constant*> OptInstanceMethods,
1155 ArrayRef<llvm::Constant*> OptClassMethods,
1156 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001157
1158 /// EmitProtocolList - Generate the list of referenced
1159 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001160 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001161 ObjCProtocolDecl::protocol_iterator begin,
1162 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001163
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001164 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1165 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001166 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1167 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001168
1169public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001170 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001171
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001172 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001173
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001174 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001175 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001176 QualType ResultType,
1177 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001178 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001179 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001180 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001181 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001182
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001183 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001184 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001185 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001186 QualType ResultType,
1187 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001188 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001189 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001190 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001191 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001192 const CallArgList &CallArgs,
1193 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001194
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001195 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001196 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001197
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001198 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1199 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001200
1201 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1202 /// untyped one.
1203 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1204 const ObjCMethodDecl *Method);
1205
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001206 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001207
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001208 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001209
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001210 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001211
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001212 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001213
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001214 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001215 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001216
Chris Lattner74391b42009-03-22 21:03:39 +00001217 virtual llvm::Constant *GetPropertyGetFunction();
1218 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001219 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1220 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001221 virtual llvm::Constant *GetGetStructFunction();
1222 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001223 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001224 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001225
John McCallf1549f62010-07-06 01:34:17 +00001226 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1227 const ObjCAtTryStmt &S);
1228 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1229 const ObjCAtSynchronizedStmt &S);
1230 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001231 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1232 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001233 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001235 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001236 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001237 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001238 llvm::Value *src, llvm::Value *dest,
1239 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001240 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001241 llvm::Value *src, llvm::Value *dest,
1242 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001243 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1244 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001245 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1246 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001247 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001248
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001249 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1250 QualType ObjectTy,
1251 llvm::Value *BaseValue,
1252 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001253 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001254 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001255 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001256 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001257
1258 /// GetClassGlobal - Return the global variable for the Objective-C
1259 /// class of the given name.
1260 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001261 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001262 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001263};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001264
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001265class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001266private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001267 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001268 llvm::GlobalVariable* ObjCEmptyCacheVar;
1269 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001270
Daniel Dunbar11394522009-04-18 08:51:00 +00001271 /// SuperClassReferences - uniqued super class references.
1272 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001274 /// MetaClassReferences - uniqued meta class references.
1275 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001276
1277 /// EHTypeReferences - uniqued class ehtype references.
1278 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
John McCall944c8432011-05-14 03:10:52 +00001280 /// VTableDispatchMethods - List of methods for which we generate
1281 /// vtable-based message dispatch.
1282 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001284 /// DefinedMetaClasses - List of defined meta-classes.
1285 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1286
John McCall944c8432011-05-14 03:10:52 +00001287 /// isVTableDispatchedSelector - Returns true if SEL is a
1288 /// vtable-based selector.
1289 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001290
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001291 /// FinishNonFragileABIModule - Write out global data structures at the end of
1292 /// processing a translation unit.
1293 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001294
Daniel Dunbar463b8762009-05-15 21:48:48 +00001295 /// AddModuleClassList - Add the given list of class pointers to the
1296 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001297 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001298 const char *SymbolName,
1299 const char *SectionName);
1300
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1302 unsigned InstanceStart,
1303 unsigned InstanceSize,
1304 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001305 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001306 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001307 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001308 llvm::Constant *ClassRoGV,
1309 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001311 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001313 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001315 /// EmitMethodList - Emit the method list for the given
1316 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001317 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001318 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001319 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001320 /// EmitIvarList - Emit the ivar list for the given
1321 /// implementation. If ForClass is true the list of class ivars
1322 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1323 /// interface ivars will be emitted. The return value has type
1324 /// IvarListnfABIPtrTy.
1325 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001326
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001327 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001328 const ObjCIvarDecl *Ivar,
Eli Friedman38ad4f42012-11-06 01:51:20 +00001329 CharUnits offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001330
Fariborz Jahanianda320092009-01-29 19:24:30 +00001331 /// GetOrEmitProtocol - Get the protocol object for the given
1332 /// declaration, emitting it if necessary. The return value has type
1333 /// ProtocolPtrTy.
1334 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001335
Fariborz Jahanianda320092009-01-29 19:24:30 +00001336 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1337 /// object for the given declaration, emitting it if needed. These
1338 /// forward references will be filled in with empty bodies if no
1339 /// definition is seen. The return value has type ProtocolPtrTy.
1340 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001341
Fariborz Jahanianda320092009-01-29 19:24:30 +00001342 /// EmitProtocolList - Generate the list of referenced
1343 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001344 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001345 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001346 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001347
John McCall944c8432011-05-14 03:10:52 +00001348 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1349 ReturnValueSlot Return,
1350 QualType ResultType,
1351 Selector Sel,
1352 llvm::Value *Receiver,
1353 QualType Arg0Ty,
1354 bool IsSuper,
1355 const CallArgList &CallArgs,
1356 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001357
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001358 /// GetClassGlobal - Return the global variable for the Objective-C
1359 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001360 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001361
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001362 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001363 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001364 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001365 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001366
1367 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1368 IdentifierInfo *II);
1369
1370 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001371
Daniel Dunbar11394522009-04-18 08:51:00 +00001372 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1373 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001374 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1375 const ObjCInterfaceDecl *ID);
1376
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001377 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1378 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001380 const ObjCInterfaceDecl *ID);
1381
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001382 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1383 /// the given ivar.
1384 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001385 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001386 const ObjCInterfaceDecl *ID,
1387 const ObjCIvarDecl *Ivar);
1388
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001389 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1390 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001391 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1392 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001393
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001394 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001395 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001396 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001397 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001398
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001399 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001400 return "OBJC_METACLASS_$_";
1401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001402
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001403 const char *getClassSymbolPrefix() const {
1404 return "OBJC_CLASS_$_";
1405 }
1406
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001407 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001408 uint32_t &InstanceStart,
1409 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001410
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001411 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001412 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001413 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1414 return CGM.getContext().Selectors.getSelector(0, &II);
1415 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001416
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001417 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001418 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1419 return CGM.getContext().Selectors.getSelector(1, &II);
1420 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001421
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001422 /// ImplementationIsNonLazy - Check whether the given category or
1423 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001424 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001425
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001426public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001427 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 // FIXME. All stubs for now!
1429 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001430
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001431 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001432 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001433 QualType ResultType,
1434 Selector Sel,
1435 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001436 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001437 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001438 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001439
1440 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001441 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001442 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001443 QualType ResultType,
1444 Selector Sel,
1445 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001446 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001447 llvm::Value *Receiver,
1448 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001449 const CallArgList &CallArgs,
1450 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001451
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001452 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001453 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001454
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001455 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1456 bool lvalue = false)
1457 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001458
1459 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1460 /// untyped one.
1461 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1462 const ObjCMethodDecl *Method)
1463 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001464
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001465 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001466
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001467 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001468
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001469 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001470
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001471 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001472 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001473
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001474 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001475
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001476 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001477 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001478 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001479 virtual llvm::Constant *GetPropertySetFunction() {
1480 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001481 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001482
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001483 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1484 bool copy) {
1485 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1486 }
1487
David Chisnall8fac25d2010-12-26 22:13:16 +00001488 virtual llvm::Constant *GetSetStructFunction() {
1489 return ObjCTypes.getCopyStructFn();
1490 }
1491 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001492 return ObjCTypes.getCopyStructFn();
1493 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001494 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1495 return ObjCTypes.getCppAtomicObjectFunction();
1496 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001497
Chris Lattner74391b42009-03-22 21:03:39 +00001498 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001499 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001500 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001501
John McCallf1549f62010-07-06 01:34:17 +00001502 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1503 const ObjCAtTryStmt &S);
1504 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1505 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001506 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001507 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001508 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001509 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001510 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001511 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001512 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001513 llvm::Value *src, llvm::Value *dest,
1514 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001515 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001516 llvm::Value *src, llvm::Value *dest,
1517 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001518 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001519 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001520 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1521 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001522 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001523 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1524 QualType ObjectTy,
1525 llvm::Value *BaseValue,
1526 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001527 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001528 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001529 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001530 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001531};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001532
1533/// A helper class for performing the null-initialization of a return
1534/// value.
1535struct NullReturnState {
1536 llvm::BasicBlock *NullBB;
1537 llvm::BasicBlock *callBB;
1538 NullReturnState() : NullBB(0), callBB(0) {}
1539
1540 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1541 // Make blocks for the null-init and call edges.
1542 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1543 callBB = CGF.createBasicBlock("msgSend.call");
1544
1545 // Check for a null receiver and, if there is one, jump to the
1546 // null-init test.
1547 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1548 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1549
1550 // Otherwise, start performing the call.
1551 CGF.EmitBlock(callBB);
1552 }
1553
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001554 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1555 const CallArgList &CallArgs,
1556 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001557 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001558
1559 llvm::Value *NullInitPtr = 0;
1560 if (result.isScalar() && !resultType->isVoidType()) {
1561 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1562 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1563 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001564
1565 // Finish the call path.
1566 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1567 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1568
1569 // Emit the null-init block and perform the null-initialization there.
1570 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001571
1572 // Release consumed arguments along the null-receiver path.
1573 if (Method) {
1574 CallArgList::const_iterator I = CallArgs.begin();
1575 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1576 e = Method->param_end(); i != e; ++i, ++I) {
1577 const ParmVarDecl *ParamDecl = (*i);
1578 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1579 RValue RV = I->RV;
1580 assert(RV.isScalar() &&
1581 "NullReturnState::complete - arg not on object");
1582 CGF.EmitARCRelease(RV.getScalarVal(), true);
1583 }
1584 }
1585 }
1586
1587 if (result.isScalar()) {
1588 if (NullInitPtr)
1589 CGF.EmitNullInitialization(NullInitPtr, resultType);
1590 // Jump to the continuation block.
1591 CGF.EmitBlock(contBB);
1592 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1593 : result;
1594 }
1595
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001596 if (!resultType->isAnyComplexType()) {
1597 assert(result.isAggregate() && "null init of non-aggregate result?");
1598 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1599 // Jump to the continuation block.
1600 CGF.EmitBlock(contBB);
1601 return result;
1602 }
1603
1604 // _Complex type
1605 // FIXME. Now easy to handle any other scalar type whose result is returned
1606 // in memory due to ABI limitations.
1607 CGF.EmitBlock(contBB);
1608 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1609 llvm::Type *MemberType = CallCV.first->getType();
1610 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1611 // Create phi instruction for scalar complex value.
1612 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1613 PHIReal->addIncoming(ZeroCV, NullBB);
1614 PHIReal->addIncoming(CallCV.first, callBB);
1615 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1616 PHIImag->addIncoming(ZeroCV, NullBB);
1617 PHIImag->addIncoming(CallCV.second, callBB);
1618 return RValue::getComplex(PHIReal, PHIImag);
1619 }
1620};
1621
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001622} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001623
1624/* *** Helper Functions *** */
1625
1626/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001627static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001628 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001629 unsigned idx0,
1630 unsigned idx1) {
1631 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001632 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1633 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001634 };
Jay Foada5c04342011-07-21 14:31:17 +00001635 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001636}
1637
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001638/// hasObjCExceptionAttribute - Return true if this class or any super
1639/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001640static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001641 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001642 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001643 return true;
1644 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001645 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001646 return false;
1647}
1648
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001649/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001650
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001651CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001652 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001653 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001654 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001655}
1656
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001657/// GetClass - Return a reference to the class for the given interface
1658/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001659llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001660 const ObjCInterfaceDecl *ID) {
1661 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001662}
1663
1664/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001665llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1666 bool lval) {
1667 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001668}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001669llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001670 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001671 return EmitSelector(Builder, Method->getSelector());
1672}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001673
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001674llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001675 if (T->isObjCIdType() ||
1676 T->isObjCQualifiedIdType()) {
1677 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001678 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001679 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001680 if (T->isObjCClassType() ||
1681 T->isObjCQualifiedClassType()) {
1682 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001683 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001684 }
1685 if (T->isObjCObjectPointerType())
1686 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1687
John McCall5a180392010-07-24 00:37:23 +00001688 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001689}
1690
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001691/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001692/*
1693 struct __builtin_CFString {
1694 const int *isa; // point to __CFConstantStringClassReference
1695 int flags;
1696 const char *str;
1697 long length;
1698 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001699*/
1700
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001701/// or Generate a constant NSString object.
1702/*
1703 struct __builtin_NSString {
1704 const int *isa; // point to __NSConstantStringClassReference
1705 const char *str;
1706 unsigned int length;
1707 };
1708*/
1709
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001710llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001711 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001712 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001713 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001714 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001715}
1716
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001717enum {
1718 kCFTaggedObjectID_Integer = (1 << 1) + 1
1719};
1720
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001721/// Generates a message send where the super is the receiver. This is
1722/// a message send to self with special delivery semantics indicating
1723/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001724CodeGen::RValue
1725CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001726 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001727 QualType ResultType,
1728 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001729 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001730 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001731 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001732 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001733 const CodeGen::CallArgList &CallArgs,
1734 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001735 // Create and init a super structure; this is a (receiver, class)
1736 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001737 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001738 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001740 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001741 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001742 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001743
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001744 // If this is a class message the metaclass is passed as the target.
1745 llvm::Value *Target;
1746 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001747 if (isCategoryImpl) {
1748 // Message sent to 'super' in a class method defined in a category
1749 // implementation requires an odd treatment.
1750 // If we are in a class method, we must retrieve the
1751 // _metaclass_ for the current class, pointed at by
1752 // the class's "isa" pointer. The following assumes that
1753 // isa" is the first ivar in a class (which it must be).
1754 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1755 Target = CGF.Builder.CreateStructGEP(Target, 0);
1756 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001757 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001758 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1759 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1760 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1761 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001762 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001763 }
1764 else if (isCategoryImpl)
1765 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1766 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001767 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1768 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1769 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001770 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001771 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1772 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001773 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001774 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001775 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001776 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001777 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001778 return EmitMessageSend(CGF, Return, ResultType,
1779 EmitSelector(CGF.Builder, Sel),
1780 ObjCSuper, ObjCTypes.SuperPtrCTy,
1781 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001782}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001783
1784/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001785CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001786 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001787 QualType ResultType,
1788 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001789 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001790 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001791 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001792 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001793 return EmitMessageSend(CGF, Return, ResultType,
1794 EmitSelector(CGF.Builder, Sel),
1795 Receiver, CGF.getContext().getObjCIdType(),
1796 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001797}
1798
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001799CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001800CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1801 ReturnValueSlot Return,
1802 QualType ResultType,
1803 llvm::Value *Sel,
1804 llvm::Value *Arg0,
1805 QualType Arg0Ty,
1806 bool IsSuper,
1807 const CallArgList &CallArgs,
1808 const ObjCMethodDecl *Method,
1809 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001810 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001811 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001812 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001813 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1814 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001815 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001816
John McCallde5d3c72012-02-17 03:33:10 +00001817 // If we're calling a method, use the formal signature.
1818 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001819
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001820 if (Method)
1821 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1822 CGM.getContext().getCanonicalType(ResultType) &&
1823 "Result type mismatch!");
1824
John McCallcba681a2011-05-14 21:12:11 +00001825 NullReturnState nullReturn;
1826
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001827 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001828 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001829 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001830 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001831 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001832 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1833 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1834 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001835 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1836 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1837 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001838 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001839 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001840 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001841 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001842
1843 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001844 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001845 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1846 e = Method->param_end(); i != e; ++i) {
1847 const ParmVarDecl *ParamDecl = (*i);
1848 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1849 if (!nullReturn.NullBB)
1850 nullReturn.init(CGF, Arg0);
1851 requiresnullCheck = true;
1852 break;
1853 }
1854 }
1855
John McCallde5d3c72012-02-17 03:33:10 +00001856 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1857 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001858 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1859 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001860}
1861
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001862static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1863 if (FQT.isObjCGCStrong())
1864 return Qualifiers::Strong;
1865
John McCallf85e1932011-06-15 23:02:42 +00001866 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001867 return Qualifiers::Weak;
1868
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001869 // check for __unsafe_unretained
1870 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1871 return Qualifiers::GCNone;
1872
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001873 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1874 return Qualifiers::Strong;
1875
1876 if (const PointerType *PT = FQT->getAs<PointerType>())
1877 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1878
1879 return Qualifiers::GCNone;
1880}
1881
John McCall6b5a61b2011-02-07 10:33:21 +00001882llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1883 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001884
Chris Lattner8b418682012-02-07 00:39:47 +00001885 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikie4e4d0842012-03-11 07:00:24 +00001886 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1887 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001888 return nullPtr;
1889
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001890 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001891 SkipIvars.clear();
1892 IvarsInfo.clear();
Eli Friedman8cb809a2012-11-06 03:41:04 +00001893 CharUnits WordSize =
1894 CGM.getContext().toCharUnitsFromBits(
1895 CGM.getContext().getTargetInfo().getPointerWidth(0));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001896
Fariborz Jahanian81979822010-09-09 00:21:45 +00001897 // __isa is the first field in block descriptor and must assume by runtime's
1898 // convention that it is GC'able.
Eli Friedman8cb809a2012-11-06 03:41:04 +00001899 IvarsInfo.push_back(GC_IVAR(CharUnits::Zero(), WordSize));
John McCall6b5a61b2011-02-07 10:33:21 +00001900
1901 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1902
1903 // Calculate the basic layout of the block structure.
1904 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001905 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001906
1907 // Ignore the optional 'this' capture: C++ objects are not assumed
1908 // to be GC'ed.
1909
1910 // Walk the captured variables.
1911 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1912 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1913 const VarDecl *variable = ci->getVariable();
1914 QualType type = variable->getType();
1915
1916 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1917
1918 // Ignore constant captures.
1919 if (capture.isConstant()) continue;
1920
Eli Friedman38ad4f42012-11-06 01:51:20 +00001921 CharUnits fieldOffset =
1922 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
John McCall6b5a61b2011-02-07 10:33:21 +00001923
1924 // __block variables are passed by their descriptor address.
1925 if (ci->isByRef()) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00001926 IvarsInfo.push_back(GC_IVAR(fieldOffset, WordSize));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001927 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001928 }
1929
1930 assert(!type->isArrayType() && "array variable should not be caught");
1931 if (const RecordType *record = type->getAs<RecordType>()) {
1932 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001933 continue;
1934 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001935
John McCall6b5a61b2011-02-07 10:33:21 +00001936 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
Eli Friedman8cb809a2012-11-06 03:41:04 +00001937 CharUnits fieldSize = CGM.getContext().getTypeSizeInChars(type);
John McCall6b5a61b2011-02-07 10:33:21 +00001938
1939 if (GCAttr == Qualifiers::Strong)
Eli Friedman8cb809a2012-11-06 03:41:04 +00001940 IvarsInfo.push_back(GC_IVAR(fieldOffset, fieldSize));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001941 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
Eli Friedman8cb809a2012-11-06 03:41:04 +00001942 SkipIvars.push_back(GC_IVAR(fieldOffset, fieldSize));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001943 }
1944
1945 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001946 return nullPtr;
1947
1948 // Sort on byte position; captures might not be allocated in order,
1949 // and unions can do funny things.
1950 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1951 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001952
1953 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001954 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001955 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001956 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001957 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001958 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001959 if (!(s[i] & 0xf0))
1960 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1961 else
1962 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1963 printf("\n");
1964 }
1965
1966 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001967}
1968
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001969/// getBlockCaptureLifetime - This routine returns life time of the captured
1970/// block variable for the purpose of block layout meta-data generation. FQT is
1971/// the type of the variable captured in the block.
1972Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT) {
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001973 if (CGM.getLangOpts().ObjCAutoRefCount)
1974 return FQT.getObjCLifetime();
1975
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001976 // MRR.
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001977 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00001978 return Qualifiers::OCL_ExplicitNone;
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001979
1980 return Qualifiers::OCL_None;
1981}
1982
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001983void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1984 Qualifiers::ObjCLifetime LifeTime,
Eli Friedman8cb809a2012-11-06 03:41:04 +00001985 CharUnits FieldOffset,
1986 CharUnits FieldSize) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001987 // __block variables are passed by their descriptor address.
1988 if (IsByref)
1989 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00001990 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001991 else if (LifeTime == Qualifiers::OCL_Strong)
1992 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00001993 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001994 else if (LifeTime == Qualifiers::OCL_Weak)
1995 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00001996 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001997 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
1998 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00001999 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002000 else
2001 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2002 FieldOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00002003 FieldSize));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002004}
2005
2006void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2007 const RecordDecl *RD,
2008 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman8cb809a2012-11-06 03:41:04 +00002009 CharUnits BytePos, bool &HasUnion) {
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002010 bool IsUnion = (RD && RD->isUnion());
Eli Friedman8cb809a2012-11-06 03:41:04 +00002011 CharUnits MaxUnionSize = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002012 const FieldDecl *MaxField = 0;
2013 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedman8cb809a2012-11-06 03:41:04 +00002014 CharUnits MaxFieldOffset = CharUnits::Zero();
2015 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002016
2017 if (RecFields.empty())
2018 return;
2019 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2020
2021 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2022 const FieldDecl *Field = RecFields[i];
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002023 // Note that 'i' here is actually the field index inside RD of Field,
2024 // although this dependency is hidden.
2025 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedman8cb809a2012-11-06 03:41:04 +00002026 CharUnits FieldOffset =
2027 CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i));
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002028
2029 // Skip over unnamed or bitfields
2030 if (!Field->getIdentifier() || Field->isBitField()) {
2031 LastFieldBitfieldOrUnnamed = Field;
2032 LastBitfieldOrUnnamedOffset = FieldOffset;
2033 continue;
2034 }
2035
2036 LastFieldBitfieldOrUnnamed = 0;
2037 QualType FQT = Field->getType();
2038 if (FQT->isRecordType() || FQT->isUnionType()) {
2039 if (FQT->isUnionType())
2040 HasUnion = true;
2041
2042 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2043 BytePos + FieldOffset, HasUnion);
2044 continue;
2045 }
2046
2047 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2048 const ConstantArrayType *CArray =
2049 dyn_cast_or_null<ConstantArrayType>(Array);
2050 uint64_t ElCount = CArray->getSize().getZExtValue();
2051 assert(CArray && "only array with known element size is supported");
2052 FQT = CArray->getElementType();
2053 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2054 const ConstantArrayType *CArray =
2055 dyn_cast_or_null<ConstantArrayType>(Array);
2056 ElCount *= CArray->getSize().getZExtValue();
2057 FQT = CArray->getElementType();
2058 }
2059
2060 assert(!FQT->isUnionType() &&
2061 "layout for array of unions not supported");
2062 if (FQT->isRecordType() && ElCount) {
2063 int OldIndex = RunSkipBlockVars.size() - 1;
2064 const RecordType *RT = FQT->getAs<RecordType>();
2065 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2066 HasUnion);
2067
2068 // Replicate layout information for each array element. Note that
2069 // one element is already done.
2070 uint64_t ElIx = 1;
2071 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00002072 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002073 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2074 RunSkipBlockVars.push_back(
2075 RUN_SKIP(RunSkipBlockVars[i].opcode,
2076 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2077 RunSkipBlockVars[i].block_var_size));
2078 }
2079 continue;
2080 }
2081 }
Eli Friedman8cb809a2012-11-06 03:41:04 +00002082 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002083 if (IsUnion) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00002084 CharUnits UnionIvarSize = FieldSize;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002085 if (UnionIvarSize > MaxUnionSize) {
2086 MaxUnionSize = UnionIvarSize;
2087 MaxField = Field;
2088 MaxFieldOffset = FieldOffset;
2089 }
2090 } else {
2091 UpdateRunSkipBlockVars(false,
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002092 getBlockCaptureLifetime(FQT),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002093 BytePos + FieldOffset,
2094 FieldSize);
2095 }
2096 }
2097
2098 if (LastFieldBitfieldOrUnnamed) {
2099 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2100 // Last field was a bitfield. Must update the info.
2101 uint64_t BitFieldSize
2102 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Eli Friedman8cb809a2012-11-06 03:41:04 +00002103 CharUnits Size =
2104 CharUnits::fromQuantity(
Eli Friedman13071202012-11-06 21:10:22 +00002105 (BitFieldSize + ByteSizeInBits - 1) / ByteSizeInBits);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002106 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,
Eli Friedman8cb809a2012-11-06 03:41:04 +00002110 Size);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002111 } else {
2112 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2113 // Last field was unnamed. Must update skip info.
Eli Friedman8cb809a2012-11-06 03:41:04 +00002114 CharUnits FieldSize
2115 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002116 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,
Eli Friedman8cb809a2012-11-06 03:41:04 +00002131 CharUnits BytePos,
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002132 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
Eli Friedman8cb809a2012-11-06 03:41:04 +00002284 CharUnits fieldOffset =
2285 CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex()));
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002286
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002287 assert(!type->isArrayType() && "array variable should not be caught");
2288 if (const RecordType *record = type->getAs<RecordType>()) {
2289 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002290 continue;
2291 }
Eli Friedman8cb809a2012-11-06 03:41:04 +00002292 CharUnits fieldSize;
2293 if (ci->isByRef())
2294 fieldSize = CharUnits::fromQuantity(WordSizeInBytes);
2295 else
2296 fieldSize = CGM.getContext().getTypeSizeInChars(type);
Fariborz Jahanianc441cd32012-11-04 18:19:40 +00002297 UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002298 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002299 }
2300
2301 if (RunSkipBlockVars.empty())
2302 return nullPtr;
2303
2304 // Sort on byte position; captures might not be allocated in order,
2305 // and unions can do funny things.
2306 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002307 SmallVector<unsigned char, 16> Layout;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002308
2309 unsigned size = RunSkipBlockVars.size();
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002310 for (unsigned i = 0; i < size; i++) {
2311 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
Eli Friedman8cb809a2012-11-06 03:41:04 +00002312 CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2313 CharUnits end_byte_pos = start_byte_pos;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002314 unsigned j = i+1;
2315 while (j < size) {
2316 if (opcode == RunSkipBlockVars[j].opcode) {
2317 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2318 i++;
2319 }
2320 else
2321 break;
2322 }
Eli Friedman8cb809a2012-11-06 03:41:04 +00002323 CharUnits size_in_bytes =
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002324 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002325 if (j < size) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00002326 CharUnits gap =
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002327 RunSkipBlockVars[j].block_var_bytepos -
2328 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2329 size_in_bytes += gap;
2330 }
Eli Friedman8cb809a2012-11-06 03:41:04 +00002331 CharUnits residue_in_bytes = CharUnits::Zero();
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002332 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00002333 residue_in_bytes = size_in_bytes % WordSizeInBytes;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002334 size_in_bytes -= residue_in_bytes;
2335 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2336 }
2337
Eli Friedman8cb809a2012-11-06 03:41:04 +00002338 unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002339 while (size_in_words >= 16) {
2340 // Note that value in imm. is one less that the actual
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002341 // value. So, 0xf means 16 words follow!
2342 unsigned char inst = (opcode << 4) | 0xf;
2343 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002344 size_in_words -= 16;
2345 }
2346 if (size_in_words > 0) {
2347 // Note that value in imm. is one less that the actual
2348 // value. So, we subtract 1 away!
2349 unsigned char inst = (opcode << 4) | (size_in_words-1);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002350 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002351 }
Eli Friedman8cb809a2012-11-06 03:41:04 +00002352 if (residue_in_bytes > CharUnits::Zero()) {
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002353 unsigned char inst =
Eli Friedman8cb809a2012-11-06 03:41:04 +00002354 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) |
2355 (residue_in_bytes.getQuantity()-1);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002356 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002357 }
2358 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002359
2360 int e = Layout.size()-1;
2361 while (e >= 0) {
2362 unsigned char inst = Layout[e--];
2363 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2364 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2365 Layout.pop_back();
2366 else
2367 break;
2368 }
2369
2370 uint64_t Result = InlineLayoutInstruction(Layout);
2371 if (Result != 0) {
2372 // Block variable layout instruction has been inlined.
2373 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2374 printf("\n Inline instruction for block variable layout: ");
Matt Beaumont-Gay00a85412012-11-01 20:26:42 +00002375 printf("0x0%llx\n", (unsigned long long)Result);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002376 }
2377 if (WordSizeInBytes == 8) {
2378 const llvm::APInt Instruction(64, Result);
2379 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2380 }
2381 else {
2382 const llvm::APInt Instruction(32, Result);
2383 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2384 }
2385 }
2386
2387 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2388 Layout.push_back(inst);
2389 std::string BitMap;
2390 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2391 BitMap += Layout[i];
2392
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002393 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2394 printf("\n block variable layout: ");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002395 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2396 unsigned char inst = BitMap[i];
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002397 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2398 unsigned delta = 1;
2399 switch (opcode) {
2400 case BLOCK_LAYOUT_OPERATOR:
2401 printf("BL_OPERATOR:");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002402 delta = 0;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002403 break;
2404 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2405 printf("BL_NON_OBJECT_BYTES:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002406 break;
2407 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2408 printf("BL_NON_OBJECT_WORD:");
2409 break;
2410 case BLOCK_LAYOUT_STRONG:
2411 printf("BL_STRONG:");
2412 break;
2413 case BLOCK_LAYOUT_BYREF:
2414 printf("BL_BYREF:");
2415 break;
2416 case BLOCK_LAYOUT_WEAK:
2417 printf("BL_WEAK:");
2418 break;
2419 case BLOCK_LAYOUT_UNRETAINED:
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002420 printf("BL_UNRETAINED:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002421 break;
2422 }
2423 // Actual value of word count is one more that what is in the imm.
2424 // field of the instruction
2425 printf("%d", (inst & 0xf) + delta);
2426 if (i < e-1)
2427 printf(", ");
2428 else
2429 printf("\n");
2430 }
2431 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002432
2433 llvm::GlobalVariable * Entry =
2434 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2435 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2436 "__TEXT,__objc_classname,cstring_literals", 1, true);
2437 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002438}
2439
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002441 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002442 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002443 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002444 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2445
Owen Anderson3c4972d2009-07-29 18:54:39 +00002446 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002447 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002448}
2449
Fariborz Jahanianda320092009-01-29 19:24:30 +00002450void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002451 // FIXME: We shouldn't need this, the protocol decl should contain enough
2452 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002453 DefinedProtocols.insert(PD->getIdentifier());
2454
2455 // If we have generated a forward reference to this protocol, emit
2456 // it now. Otherwise do nothing, the protocol objects are lazily
2457 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002458 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002459 GetOrEmitProtocol(PD);
2460}
2461
Fariborz Jahanianda320092009-01-29 19:24:30 +00002462llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002463 if (DefinedProtocols.count(PD->getIdentifier()))
2464 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002465
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002466 return GetOrEmitProtocolRef(PD);
2467}
2468
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002469/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002470// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2471struct _objc_protocol {
2472struct _objc_protocol_extension *isa;
2473char *protocol_name;
2474struct _objc_protocol_list *protocol_list;
2475struct _objc__method_prototype_list *instance_methods;
2476struct _objc__method_prototype_list *class_methods
2477};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002478
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002479See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002480*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002481llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002482 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002483
2484 // Early exit if a defining object has already been generated.
2485 if (Entry && Entry->hasInitializer())
2486 return Entry;
2487
Douglas Gregor1d784b22012-01-01 19:51:50 +00002488 // Use the protocol definition, if there is one.
2489 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2490 PD = Def;
2491
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002492 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002493 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002494 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2495
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002496 // Construct method lists.
2497 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2498 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002499 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002500 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002501 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002502 ObjCMethodDecl *MD = *i;
2503 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002504 if (!C)
2505 return GetOrEmitProtocolRef(PD);
2506
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002507 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2508 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002509 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002510 } else {
2511 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002512 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002514 }
2515
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002516 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002517 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002518 ObjCMethodDecl *MD = *i;
2519 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002520 if (!C)
2521 return GetOrEmitProtocolRef(PD);
2522
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002523 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2524 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002525 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002526 } else {
2527 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002528 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002529 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002530 }
2531
Bob Wilsondc8dab62011-11-30 01:57:58 +00002532 MethodTypesExt.insert(MethodTypesExt.end(),
2533 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2534
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002535 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002536 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2537 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002538 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002539 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00002540 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002541 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002542 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002543 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002544 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002545 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002546 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002547 ClassMethods)
2548 };
Owen Anderson08e25242009-07-27 22:29:56 +00002549 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002550 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002551
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002552 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002553 // Already created, fix the linkage and update the initializer.
2554 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002555 Entry->setInitializer(Init);
2556 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002557 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002558 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002559 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002560 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002561 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002562 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002563 // FIXME: Is this necessary? Why only for protocol?
2564 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002565
2566 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002567 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002568 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002569
2570 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002571}
2572
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002573llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002574 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2575
2576 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002577 // We use the initializer as a marker of whether this is a forward
2578 // reference or not. At module finalization we add the empty
2579 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002580 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002581 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002582 llvm::GlobalValue::ExternalLinkage,
2583 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002584 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002585 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002586 // FIXME: Is this necessary? Why only for protocol?
2587 Entry->setAlignment(4);
2588 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002589
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002590 return Entry;
2591}
2592
2593/*
2594 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002595 uint32_t size;
2596 struct objc_method_description_list *optional_instance_methods;
2597 struct objc_method_description_list *optional_class_methods;
2598 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002599 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002600 };
2601*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002602llvm::Constant *
2603CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002604 ArrayRef<llvm::Constant*> OptInstanceMethods,
2605 ArrayRef<llvm::Constant*> OptClassMethods,
2606 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002607 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002608 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002609 llvm::Constant *Values[] = {
2610 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002611 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002612 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002613 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002614 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002615 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002616 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002617 OptClassMethods),
2618 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002619 ObjCTypes),
2620 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2621 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002622 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002623
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002624 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002625 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002626 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002627 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002629 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002630 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002631
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002632 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002633 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002634 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002635 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002636}
2637
2638/*
2639 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002640 struct objc_protocol_list *next;
2641 long count;
2642 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002643 };
2644*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00002645llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002646CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00002647 ObjCProtocolDecl::protocol_iterator begin,
2648 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002649 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002650
Daniel Dunbardbc933702008-08-21 21:57:41 +00002651 for (; begin != end; ++begin)
2652 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002653
2654 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002655 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002656 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002657
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002658 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002659 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002660
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002661 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002662 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002663 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002664 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002665 ProtocolRefs.size() - 1);
2666 Values[2] =
2667 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2668 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002669 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002670
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002671 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002672 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002673 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002674 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002675 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002676}
2677
Bill Wendlingbb028552012-02-07 09:25:09 +00002678void CGObjCCommonMac::
2679PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002680 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002681 const Decl *Container,
2682 const ObjCProtocolDecl *PROTO,
2683 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002684 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2685 E = PROTO->protocol_end(); P != E; ++P)
2686 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2687 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2688 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002689 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002690 if (!PropertySet.insert(PD->getIdentifier()))
2691 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002692 llvm::Constant *Prop[] = {
2693 GetPropertyName(PD->getIdentifier()),
2694 GetPropertyTypeString(PD, Container)
2695 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002696 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2697 }
2698}
2699
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002700/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002701 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002702 const char * const name;
2703 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002704 };
2705
2706 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002707 uint32_t entsize; // sizeof (struct _objc_property)
2708 uint32_t prop_count;
2709 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002710 };
2711*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002712llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002713 const Decl *Container,
2714 const ObjCContainerDecl *OCD,
2715 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002716 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002717 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002718 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2719 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002720 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002721 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002722 llvm::Constant *Prop[] = {
2723 GetPropertyName(PD->getIdentifier()),
2724 GetPropertyTypeString(PD, Container)
2725 };
Owen Anderson08e25242009-07-27 22:29:56 +00002726 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002727 Prop));
2728 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002729 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002730 for (ObjCInterfaceDecl::all_protocol_iterator
2731 P = OID->all_referenced_protocol_begin(),
2732 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002733 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2734 ObjCTypes);
2735 }
2736 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2737 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2738 E = CD->protocol_end(); P != E; ++P)
2739 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2740 ObjCTypes);
2741 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002742
2743 // Return null for empty list.
2744 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002745 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002746
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002747 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002748 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002749 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002750 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2751 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002752 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002753 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002754 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002755 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002756
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002757 llvm::GlobalVariable *GV =
2758 CreateMetadataVar(Name, Init,
2759 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002760 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002761 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002762 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002763 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002764}
2765
Bill Wendlingbb028552012-02-07 09:25:09 +00002766llvm::Constant *
2767CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2768 ArrayRef<llvm::Constant*> MethodTypes,
2769 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002770 // Return null for empty list.
2771 if (MethodTypes.empty())
2772 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2773
2774 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2775 MethodTypes.size());
2776 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2777
2778 llvm::GlobalVariable *GV =
2779 CreateMetadataVar(Name, Init,
2780 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2781 (ObjCABI == 2) ? 8 : 4,
2782 true);
2783 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2784}
2785
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002786/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002787 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002788 int count;
2789 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002790 };
2791*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002792llvm::Constant *
2793CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002794 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002795 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002796 ObjCTypes.SelectorPtrTy),
2797 GetMethodVarType(MD)
2798 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002799 if (!Desc[1])
2800 return 0;
2801
Owen Anderson08e25242009-07-27 22:29:56 +00002802 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002803 Desc);
2804}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002805
Bill Wendlingbb028552012-02-07 09:25:09 +00002806llvm::Constant *
2807CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2808 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002809 // Return null for empty list.
2810 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002811 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002812
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002813 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002814 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002815 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002816 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002817 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002818 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002819
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002820 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002821 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002822 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002823}
2824
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002825/*
2826 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002827 char *category_name;
2828 char *class_name;
2829 struct _objc_method_list *instance_methods;
2830 struct _objc_method_list *class_methods;
2831 struct _objc_protocol_list *protocols;
2832 uint32_t size; // <rdar://4585769>
2833 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002834 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002835*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002836void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002837 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002838
Mike Stumpf5408fe2009-05-16 07:57:57 +00002839 // FIXME: This is poor design, the OCD should have a pointer to the category
2840 // decl. Additionally, note that Category can be null for the @implementation
2841 // w/o an @interface case. Sema should just create one for us as it does for
2842 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002843 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002844 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002845 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002846
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002847 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002848 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2849 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002850
Bill Wendling3964e622012-02-09 22:16:49 +00002851 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002852 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002853 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002854 // Instance methods should always be defined.
2855 InstanceMethods.push_back(GetMethodConstant(*i));
2856 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002857 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002858 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002859 // Class methods should always be defined.
2860 ClassMethods.push_back(GetMethodConstant(*i));
2861 }
2862
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002863 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002864 Values[0] = GetClassName(OCD->getIdentifier());
2865 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002866 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002867 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002868 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002869 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002870 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002871 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002872 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002873 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002874 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002875 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002876 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002877 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002878 Category->protocol_begin(),
2879 Category->protocol_end());
2880 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002881 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002882 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002883 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002884
2885 // If there is no category @interface then there can be no properties.
2886 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002887 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002888 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002889 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002890 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002891 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002892
Owen Anderson08e25242009-07-27 22:29:56 +00002893 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002894 Values);
2895
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002896 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002897 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002898 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002899 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002900 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002901 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002902 // method definition entries must be clear for next implementation.
2903 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002904}
2905
John McCall621915c2012-10-17 04:53:23 +00002906enum FragileClassFlags {
2907 FragileABI_Class_Factory = 0x00001,
2908 FragileABI_Class_Meta = 0x00002,
2909 FragileABI_Class_HasCXXStructors = 0x02000,
2910 FragileABI_Class_Hidden = 0x20000
2911};
2912
2913enum NonFragileClassFlags {
2914 /// Is a meta-class.
2915 NonFragileABI_Class_Meta = 0x00001,
2916
2917 /// Is a root class.
2918 NonFragileABI_Class_Root = 0x00002,
2919
2920 /// Has a C++ constructor and destructor.
2921 NonFragileABI_Class_HasCXXStructors = 0x00004,
2922
2923 /// Has hidden visibility.
2924 NonFragileABI_Class_Hidden = 0x00010,
2925
2926 /// Has the exception attribute.
2927 NonFragileABI_Class_Exception = 0x00020,
2928
2929 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2930 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2931
2932 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00002933 NonFragileABI_Class_CompiledByARC = 0x00080,
2934
2935 /// Class has non-trivial destructors, but zero-initialization is okay.
2936 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002937};
2938
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002939/*
2940 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002941 Class isa;
2942 Class super_class;
2943 const char *name;
2944 long version;
2945 long info;
2946 long instance_size;
2947 struct _objc_ivar_list *ivars;
2948 struct _objc_method_list *methods;
2949 struct _objc_cache *cache;
2950 struct _objc_protocol_list *protocols;
2951 // Objective-C 1.0 extensions (<rdr://4585769>)
2952 const char *ivar_layout;
2953 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002954 };
2955
2956 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002957*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002958void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002959 DefinedSymbols.insert(ID->getIdentifier());
2960
Chris Lattner8ec03f52008-11-24 03:54:41 +00002961 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002962 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002963 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002964 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002965 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002966 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002967 Interface->all_referenced_protocol_begin(),
2968 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00002969 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00002970 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00002971 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002972 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002973 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002974
2975 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002976 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00002977 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002978
Bill Wendling3964e622012-02-09 22:16:49 +00002979 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002980 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002981 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002982 // Instance methods should always be defined.
2983 InstanceMethods.push_back(GetMethodConstant(*i));
2984 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002985 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002986 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002987 // Class methods should always be defined.
2988 ClassMethods.push_back(GetMethodConstant(*i));
2989 }
2990
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002991 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002992 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002993 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002994
2995 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2996 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2997
2998 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2999 if (llvm::Constant *C = GetMethodConstant(MD))
3000 InstanceMethods.push_back(C);
3001 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3002 if (llvm::Constant *C = GetMethodConstant(MD))
3003 InstanceMethods.push_back(C);
3004 }
3005 }
3006
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003007 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003008 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003009 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003010 // Record a reference to the super class.
3011 LazySymbols.insert(Super->getIdentifier());
3012
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003013 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003014 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003015 ObjCTypes.ClassPtrTy);
3016 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003017 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003018 }
3019 Values[ 2] = GetClassName(ID->getIdentifier());
3020 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003021 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3022 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3023 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003024 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003025 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003026 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003027 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003028 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003029 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003030 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003031 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003032 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003033 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003034 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003035 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003036 std::string Name("\01L_OBJC_CLASS_");
3037 Name += ClassName;
3038 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3039 // Check for a forward reference.
3040 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3041 if (GV) {
3042 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3043 "Forward metaclass reference has incorrect type.");
3044 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3045 GV->setInitializer(Init);
3046 GV->setSection(Section);
3047 GV->setAlignment(4);
3048 CGM.AddUsedGlobal(GV);
3049 }
3050 else
3051 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003052 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003053 // method definition entries must be clear for next implementation.
3054 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003055}
3056
3057llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3058 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003059 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003060 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003061 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003062
John McCall1fb0caa2010-10-22 21:05:15 +00003063 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003064 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003065
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003066 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003067 // The isa for the metaclass is the root of the hierarchy.
3068 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3069 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3070 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003071 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003072 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003073 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003074 // The super class for the metaclass is emitted as the name of the
3075 // super class. The runtime fixes this up to point to the
3076 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003077 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003078 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003079 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003080 ObjCTypes.ClassPtrTy);
3081 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003082 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003083 }
3084 Values[ 2] = GetClassName(ID->getIdentifier());
3085 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003086 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3087 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3088 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003089 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003091 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003092 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003093 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003094 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003095 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003096 Values[ 9] = Protocols;
3097 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003098 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003099 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003100 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003101 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003102 Values);
3103
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003104 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003105 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003106
3107 // Check for a forward reference.
3108 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3109 if (GV) {
3110 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3111 "Forward metaclass reference has incorrect type.");
3112 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3113 GV->setInitializer(Init);
3114 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003115 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003116 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003117 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003118 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003119 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003120 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003121 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003122
3123 return GV;
3124}
3125
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003126llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003127 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003128
Mike Stumpf5408fe2009-05-16 07:57:57 +00003129 // FIXME: Should we look these up somewhere other than the module. Its a bit
3130 // silly since we only generate these while processing an implementation, so
3131 // exactly one pointer would work if know when we entered/exitted an
3132 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003133
3134 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003135 // Previously, metaclass with internal linkage may have been defined.
3136 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003137 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3138 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003139 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3140 "Forward metaclass reference has incorrect type.");
3141 return GV;
3142 } else {
3143 // Generate as an external reference to keep a consistent
3144 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003145 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003146 llvm::GlobalValue::ExternalLinkage,
3147 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003148 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003149 }
3150}
3151
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003152llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3153 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3154
3155 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3156 true)) {
3157 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3158 "Forward class metadata reference has incorrect type.");
3159 return GV;
3160 } else {
3161 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3162 llvm::GlobalValue::ExternalLinkage,
3163 0,
3164 Name);
3165 }
3166}
3167
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003168/*
3169 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003170 uint32_t size;
3171 const char *weak_ivar_layout;
3172 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003173 };
3174*/
3175llvm::Constant *
3176CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003177 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003178 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003179
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003180 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003181 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003182 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003183 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003184 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003185
3186 // Return null if no extension bits are used.
3187 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003188 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003189
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003190 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003191 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003192 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003193 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003194 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003195}
3196
3197/*
3198 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003199 char *ivar_name;
3200 char *ivar_type;
3201 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003202 };
3203
3204 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003205 int ivar_count;
3206 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003207 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003208*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003209llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003210 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003211 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003212
3213 // When emitting the root class GCC emits ivar entries for the
3214 // actual class structure. It is not clear if we need to follow this
3215 // behavior; for now lets try and get away with not doing it. If so,
3216 // the cleanest solution would be to make up an ObjCInterfaceDecl
3217 // for the class.
3218 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003219 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003220
Jordy Rosedb8264e2011-07-22 02:08:32 +00003221 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003222
Jordy Rosedb8264e2011-07-22 02:08:32 +00003223 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003224 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003225 // Ignore unnamed bit-fields.
3226 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003227 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003228 llvm::Constant *Ivar[] = {
3229 GetMethodVarName(IVD->getIdentifier()),
3230 GetMethodVarType(IVD),
3231 llvm::ConstantInt::get(ObjCTypes.IntTy,
Eli Friedman38ad4f42012-11-06 01:51:20 +00003232 ComputeIvarBaseOffset(CGM, OID, IVD).getQuantity())
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003233 };
Owen Anderson08e25242009-07-27 22:29:56 +00003234 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003235 }
3236
3237 // Return null for empty list.
3238 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003239 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003240
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003241 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003242 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003243 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003244 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003245 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003246 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003247
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003248 llvm::GlobalVariable *GV;
3249 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003250 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003251 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003252 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003253 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003254 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003255 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003256 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003257 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003258}
3259
3260/*
3261 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003262 SEL method_name;
3263 char *method_types;
3264 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003265 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003266
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003267 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003268 struct objc_method_list *obsolete;
3269 int count;
3270 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003271 };
3272*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003273
3274/// GetMethodConstant - Return a struct objc_method constant for the
3275/// given method if it has been defined. The result is null if the
3276/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003277llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003278 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003279 if (!Fn)
3280 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003282 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003283 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003284 ObjCTypes.SelectorPtrTy),
3285 GetMethodVarType(MD),
3286 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3287 };
Owen Anderson08e25242009-07-27 22:29:56 +00003288 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003289}
3290
Chris Lattner5f9e2722011-07-23 10:55:15 +00003291llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003292 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003293 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003294 // Return null for empty list.
3295 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003296 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003297
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003298 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003299 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003300 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003301 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003302 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003303 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003304 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003305
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003306 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003307 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003308}
3309
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003310llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003311 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003312 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003313 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003314
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003315 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003316 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003317 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003318 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003319 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003320 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003321 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003322 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003323 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003324
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003325 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003326}
3327
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003328llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003329CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003330 llvm::Constant *Init,
3331 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003332 unsigned Align,
3333 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003334 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003335 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003336 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003337 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003338 if (Section)
3339 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003340 if (Align)
3341 GV->setAlignment(Align);
3342 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003343 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003344 return GV;
3345}
3346
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003347llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003348 // Abuse this interface function as a place to finalize.
3349 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003350 return NULL;
3351}
3352
Chris Lattner74391b42009-03-22 21:03:39 +00003353llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003354 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003355}
3356
Chris Lattner74391b42009-03-22 21:03:39 +00003357llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003358 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003359}
3360
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003361llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3362 bool copy) {
3363 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3364}
3365
David Chisnall8fac25d2010-12-26 22:13:16 +00003366llvm::Constant *CGObjCMac::GetGetStructFunction() {
3367 return ObjCTypes.getCopyStructFn();
3368}
3369llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003370 return ObjCTypes.getCopyStructFn();
3371}
3372
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003373llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
3374 return ObjCTypes.getCppAtomicObjectFunction();
3375}
3376
Chris Lattner74391b42009-03-22 21:03:39 +00003377llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003378 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003379}
3380
John McCallf1549f62010-07-06 01:34:17 +00003381void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3382 return EmitTryOrSynchronizedStmt(CGF, S);
3383}
3384
3385void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3386 const ObjCAtSynchronizedStmt &S) {
3387 return EmitTryOrSynchronizedStmt(CGF, S);
3388}
3389
John McCallcc505292010-07-21 06:59:36 +00003390namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003391 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003392 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003393 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003394 llvm::Value *CallTryExitVar;
3395 llvm::Value *ExceptionData;
3396 ObjCTypesHelper &ObjCTypes;
3397 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003398 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003399 llvm::Value *CallTryExitVar,
3400 llvm::Value *ExceptionData,
3401 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003402 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003403 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3404
John McCallad346f42011-07-12 20:27:29 +00003405 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003406 // Check whether we need to call objc_exception_try_exit.
3407 // In optimized code, this branch will always be folded.
3408 llvm::BasicBlock *FinallyCallExit =
3409 CGF.createBasicBlock("finally.call_exit");
3410 llvm::BasicBlock *FinallyNoCallExit =
3411 CGF.createBasicBlock("finally.no_call_exit");
3412 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3413 FinallyCallExit, FinallyNoCallExit);
3414
3415 CGF.EmitBlock(FinallyCallExit);
3416 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3417 ->setDoesNotThrow();
3418
3419 CGF.EmitBlock(FinallyNoCallExit);
3420
3421 if (isa<ObjCAtTryStmt>(S)) {
3422 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003423 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3424 // Save the current cleanup destination in case there's
3425 // control flow inside the finally statement.
3426 llvm::Value *CurCleanupDest =
3427 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3428
John McCallcc505292010-07-21 06:59:36 +00003429 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3430
John McCalld96a8e72010-08-11 00:16:14 +00003431 if (CGF.HaveInsertPoint()) {
3432 CGF.Builder.CreateStore(CurCleanupDest,
3433 CGF.getNormalCleanupDestSlot());
3434 } else {
3435 // Currently, the end of the cleanup must always exist.
3436 CGF.EnsureInsertPoint();
3437 }
3438 }
John McCallcc505292010-07-21 06:59:36 +00003439 } else {
3440 // Emit objc_sync_exit(expr); as finally's sole statement for
3441 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003442 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003443 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3444 ->setDoesNotThrow();
3445 }
3446 }
3447 };
John McCall87bb5822010-07-31 23:20:56 +00003448
3449 class FragileHazards {
3450 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003451 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003452 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3453
3454 llvm::InlineAsm *ReadHazard;
3455 llvm::InlineAsm *WriteHazard;
3456
3457 llvm::FunctionType *GetAsmFnType();
3458
3459 void collectLocals();
3460 void emitReadHazard(CGBuilderTy &Builder);
3461
3462 public:
3463 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003464
John McCall87bb5822010-07-31 23:20:56 +00003465 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003466 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003467 };
3468}
3469
3470/// Create the fragile-ABI read and write hazards based on the current
3471/// state of the function, which is presumed to be immediately prior
3472/// to a @try block. These hazards are used to maintain correct
3473/// semantics in the face of optimization and the fragile ABI's
3474/// cavalier use of setjmp/longjmp.
3475FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3476 collectLocals();
3477
3478 if (Locals.empty()) return;
3479
3480 // Collect all the blocks in the function.
3481 for (llvm::Function::iterator
3482 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3483 BlocksBeforeTry.insert(&*I);
3484
3485 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3486
3487 // Create a read hazard for the allocas. This inhibits dead-store
3488 // optimizations and forces the values to memory. This hazard is
3489 // inserted before any 'throwing' calls in the protected scope to
3490 // reflect the possibility that the variables might be read from the
3491 // catch block if the call throws.
3492 {
3493 std::string Constraint;
3494 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3495 if (I) Constraint += ',';
3496 Constraint += "*m";
3497 }
3498
3499 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3500 }
3501
3502 // Create a write hazard for the allocas. This inhibits folding
3503 // loads across the hazard. This hazard is inserted at the
3504 // beginning of the catch path to reflect the possibility that the
3505 // variables might have been written within the protected scope.
3506 {
3507 std::string Constraint;
3508 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3509 if (I) Constraint += ',';
3510 Constraint += "=*m";
3511 }
3512
3513 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3514 }
3515}
3516
3517/// Emit a write hazard at the current location.
3518void FragileHazards::emitWriteHazard() {
3519 if (Locals.empty()) return;
3520
Jay Foad4c7d9f12011-07-15 08:37:34 +00003521 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003522}
3523
John McCall87bb5822010-07-31 23:20:56 +00003524void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3525 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003526 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003527}
3528
3529/// Emit read hazards in all the protected blocks, i.e. all the blocks
3530/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003531void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003532 if (Locals.empty()) return;
3533
3534 CGBuilderTy Builder(CGF.getLLVMContext());
3535
3536 // Iterate through all blocks, skipping those prior to the try.
3537 for (llvm::Function::iterator
3538 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3539 llvm::BasicBlock &BB = *FI;
3540 if (BlocksBeforeTry.count(&BB)) continue;
3541
3542 // Walk through all the calls in the block.
3543 for (llvm::BasicBlock::iterator
3544 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3545 llvm::Instruction &I = *BI;
3546
3547 // Ignore instructions that aren't non-intrinsic calls.
3548 // These are the only calls that can possibly call longjmp.
3549 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3550 if (isa<llvm::IntrinsicInst>(I))
3551 continue;
3552
3553 // Ignore call sites marked nounwind. This may be questionable,
3554 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3555 llvm::CallSite CS(&I);
3556 if (CS.doesNotThrow()) continue;
3557
John McCall0b251722010-08-04 05:59:32 +00003558 // Insert a read hazard before the call. This will ensure that
3559 // any writes to the locals are performed before making the
3560 // call. If the call throws, then this is sufficient to
3561 // guarantee correctness as long as it doesn't also write to any
3562 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003563 Builder.SetInsertPoint(&BB, BI);
3564 emitReadHazard(Builder);
3565 }
3566 }
3567}
3568
3569static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3570 if (V) S.insert(V);
3571}
3572
3573void FragileHazards::collectLocals() {
3574 // Compute a set of allocas to ignore.
3575 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3576 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3577 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003578
3579 // Collect all the allocas currently in the function. This is
3580 // probably way too aggressive.
3581 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3582 for (llvm::BasicBlock::iterator
3583 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3584 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3585 Locals.push_back(&*I);
3586}
3587
3588llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003589 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003590 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3591 tys[i] = Locals[i]->getType();
3592 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003593}
3594
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003595/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003596
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003597 Objective-C setjmp-longjmp (sjlj) Exception Handling
3598 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003599
John McCallf1549f62010-07-06 01:34:17 +00003600 A catch buffer is a setjmp buffer plus:
3601 - a pointer to the exception that was caught
3602 - a pointer to the previous exception data buffer
3603 - two pointers of reserved storage
3604 Therefore catch buffers form a stack, with a pointer to the top
3605 of the stack kept in thread-local storage.
3606
3607 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3608 objc_exception_try_exit pops the given catch buffer, which is
3609 required to be the top of the EH stack.
3610 objc_exception_throw pops the top of the EH stack, writes the
3611 thrown exception into the appropriate field, and longjmps
3612 to the setjmp buffer. It crashes the process (with a printf
3613 and an abort()) if there are no catch buffers on the stack.
3614 objc_exception_extract just reads the exception pointer out of the
3615 catch buffer.
3616
3617 There's no reason an implementation couldn't use a light-weight
3618 setjmp here --- something like __builtin_setjmp, but API-compatible
3619 with the heavyweight setjmp. This will be more important if we ever
3620 want to implement correct ObjC/C++ exception interactions for the
3621 fragile ABI.
3622
3623 Note that for this use of setjmp/longjmp to be correct, we may need
3624 to mark some local variables volatile: if a non-volatile local
3625 variable is modified between the setjmp and the longjmp, it has
3626 indeterminate value. For the purposes of LLVM IR, it may be
3627 sufficient to make loads and stores within the @try (to variables
3628 declared outside the @try) volatile. This is necessary for
3629 optimized correctness, but is not currently being done; this is
3630 being tracked as rdar://problem/8160285
3631
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003632 The basic framework for a @try-catch-finally is as follows:
3633 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003634 objc_exception_data d;
3635 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003636 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003637
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003638 objc_exception_try_enter(&d);
3639 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003640 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003641 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003642 // exception path
3643 id _caught = objc_exception_extract(&d);
3644
3645 // enter new try scope for handlers
3646 if (!setjmp(d.jmp_buf)) {
3647 ... match exception and execute catch blocks ...
3648
3649 // fell off end, rethrow.
3650 _rethrow = _caught;
3651 ... jump-through-finally to finally_rethrow ...
3652 } else {
3653 // exception in catch block
3654 _rethrow = objc_exception_extract(&d);
3655 _call_try_exit = false;
3656 ... jump-through-finally to finally_rethrow ...
3657 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003658 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003659 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003660
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003661 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003662 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003663 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003664
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003665 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003666 ... dispatch to finally destination ...
3667
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003668 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003669 objc_exception_throw(_rethrow);
3670
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003671 finally_end:
3672 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003673
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003674 This framework differs slightly from the one gcc uses, in that gcc
3675 uses _rethrow to determine if objc_exception_try_exit should be called
3676 and if the object should be rethrown. This breaks in the face of
3677 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003679 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003680
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 - If there are no catch blocks, then we avoid emitting the second
3682 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003683
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003684 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3685 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003686
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687 - FIXME: If there is no @finally block we can do a few more
3688 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003689
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003690 Rethrows and Jumps-Through-Finally
3691 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003692
John McCallf1549f62010-07-06 01:34:17 +00003693 '@throw;' is supported by pushing the currently-caught exception
3694 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003695
John McCallf1549f62010-07-06 01:34:17 +00003696 Branches through the @finally block are handled with an ordinary
3697 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3698 exceptions are not compatible with C++ exceptions, and this is
3699 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003700
John McCallf1549f62010-07-06 01:34:17 +00003701 @synchronized(expr) { stmt; } is emitted as if it were:
3702 id synch_value = expr;
3703 objc_sync_enter(synch_value);
3704 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003705*/
3706
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003707void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3708 const Stmt &S) {
3709 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003710
3711 // A destination for the fall-through edges of the catch handlers to
3712 // jump to.
3713 CodeGenFunction::JumpDest FinallyEnd =
3714 CGF.getJumpDestInCurrentScope("finally.end");
3715
3716 // A destination for the rethrow edge of the catch handlers to jump
3717 // to.
3718 CodeGenFunction::JumpDest FinallyRethrow =
3719 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003720
Daniel Dunbar1c566672009-02-24 01:43:46 +00003721 // For @synchronized, call objc_sync_enter(sync.expr). The
3722 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003723 // @synchronized. We can't avoid a temp here because we need the
3724 // value to be preserved. If the backend ever does liveness
3725 // correctly after setjmp, this will be unnecessary.
3726 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003727 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003728 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003729 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3730 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003731 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3732 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003733
3734 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3735 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003736 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003737
John McCall0b251722010-08-04 05:59:32 +00003738 // Allocate memory for the setjmp buffer. This needs to be kept
3739 // live throughout the try and catch blocks.
3740 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3741 "exceptiondata.ptr");
3742
John McCall87bb5822010-07-31 23:20:56 +00003743 // Create the fragile hazards. Note that this will not capture any
3744 // of the allocas required for exception processing, but will
3745 // capture the current basic block (which extends all the way to the
3746 // setjmp call) as "before the @try".
3747 FragileHazards Hazards(CGF);
3748
John McCallf1549f62010-07-06 01:34:17 +00003749 // Create a flag indicating whether the cleanup needs to call
3750 // objc_exception_try_exit. This is true except when
3751 // - no catches match and we're branching through the cleanup
3752 // just to rethrow the exception, or
3753 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003754 // The setjmp-safety rule here is that we should always store to this
3755 // variable in a place that dominates the branch through the cleanup
3756 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003757 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003758 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003759
John McCall9e2213d2010-10-04 23:42:51 +00003760 // A slot containing the exception to rethrow. Only needed when we
3761 // have both a @catch and a @finally.
3762 llvm::Value *PropagatingExnVar = 0;
3763
John McCallf1549f62010-07-06 01:34:17 +00003764 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003765 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003766 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003767 CallTryExitVar,
3768 ExceptionData,
3769 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003770
3771 // Enter a try block:
3772 // - Call objc_exception_try_enter to push ExceptionData on top of
3773 // the EH stack.
3774 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3775 ->setDoesNotThrow();
3776
3777 // - Call setjmp on the exception data buffer.
3778 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3779 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3780 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003781 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003782 llvm::CallInst *SetJmpResult =
3783 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3784 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003785 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003786
3787 // If setjmp returned 0, enter the protected block; otherwise,
3788 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003789 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3790 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003791 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003792 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3793 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003794
John McCallf1549f62010-07-06 01:34:17 +00003795 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003796 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003797 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003798 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003799 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003800
3801 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003802
John McCallf1549f62010-07-06 01:34:17 +00003803 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003804 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003805
John McCall87bb5822010-07-31 23:20:56 +00003806 // Don't optimize loads of the in-scope locals across this point.
3807 Hazards.emitWriteHazard();
3808
John McCallf1549f62010-07-06 01:34:17 +00003809 // For a @synchronized (or a @try with no catches), just branch
3810 // through the cleanup to the rethrow block.
3811 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3812 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003813 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003814 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003815
3816 // Otherwise, we have to match against the caught exceptions.
3817 } else {
John McCall0b251722010-08-04 05:59:32 +00003818 // Retrieve the exception object. We may emit multiple blocks but
3819 // nothing can cross this so the value is already in SSA form.
3820 llvm::CallInst *Caught =
3821 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3822 ExceptionData, "caught");
3823 Caught->setDoesNotThrow();
3824
John McCallf1549f62010-07-06 01:34:17 +00003825 // Push the exception to rethrow onto the EH value stack for the
3826 // benefit of any @throws in the handlers.
3827 CGF.ObjCEHValueStack.push_back(Caught);
3828
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003829 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003830
John McCall0b251722010-08-04 05:59:32 +00003831 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003832
John McCall0b251722010-08-04 05:59:32 +00003833 llvm::BasicBlock *CatchBlock = 0;
3834 llvm::BasicBlock *CatchHandler = 0;
3835 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003836 // Save the currently-propagating exception before
3837 // objc_exception_try_enter clears the exception slot.
3838 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3839 "propagating_exception");
3840 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3841
John McCall0b251722010-08-04 05:59:32 +00003842 // Enter a new exception try block (in case a @catch block
3843 // throws an exception).
3844 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3845 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003846
John McCall0b251722010-08-04 05:59:32 +00003847 llvm::CallInst *SetJmpResult =
3848 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3849 "setjmp.result");
3850 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003851 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003852
John McCall0b251722010-08-04 05:59:32 +00003853 llvm::Value *Threw =
3854 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3855
3856 CatchBlock = CGF.createBasicBlock("catch");
3857 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3858 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3859
3860 CGF.EmitBlock(CatchBlock);
3861 }
3862
3863 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003864
Daniel Dunbar55e40722008-09-27 07:03:52 +00003865 // Handle catch list. As a special case we check if everything is
3866 // matched and avoid generating code for falling off the end if
3867 // so.
3868 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003869 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3870 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003871
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003872 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003873 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003874
Anders Carlsson80f25672008-09-09 17:59:25 +00003875 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003876 if (!CatchParam) {
3877 AllMatched = true;
3878 } else {
John McCall183700f2009-09-21 23:43:11 +00003879 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003880
John McCallf1549f62010-07-06 01:34:17 +00003881 // catch(id e) always matches under this ABI, since only
3882 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003883 // FIXME: For the time being we also match id<X>; this should
3884 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003885 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003886 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003887 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003888
John McCallf1549f62010-07-06 01:34:17 +00003889 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003890 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003891 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3892
Anders Carlssondde0a942008-09-11 09:15:33 +00003893 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003894 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003895 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003896
3897 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003898 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003899 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003900
Anders Carlssondde0a942008-09-11 09:15:33 +00003901 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003902
3903 // The scope of the catch variable ends right here.
3904 CatchVarCleanups.ForceCleanup();
3905
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003906 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003907 break;
3908 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003909
Steve Naroff14108da2009-07-10 23:34:53 +00003910 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003911 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003912
3913 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003914 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3915 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003916
3917 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003918 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003919
John McCallf1549f62010-07-06 01:34:17 +00003920 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003921 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3922 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003923 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003924
John McCallf1549f62010-07-06 01:34:17 +00003925 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3926 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003927
3928 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003929 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003930
Anders Carlsson80f25672008-09-09 17:59:25 +00003931 // Emit the @catch block.
3932 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003933
3934 // Collect any cleanups for the catch variable. The scope lasts until
3935 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003936 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003937
John McCallb6bbcc92010-10-15 04:57:14 +00003938 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003939 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003940
John McCallf1549f62010-07-06 01:34:17 +00003941 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003942 llvm::Value *Tmp =
3943 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003944 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003945 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946
Anders Carlssondde0a942008-09-11 09:15:33 +00003947 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003948
3949 // We're done with the catch variable.
3950 CatchVarCleanups.ForceCleanup();
3951
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003952 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003953
Anders Carlsson80f25672008-09-09 17:59:25 +00003954 CGF.EmitBlock(NextCatchBlock);
3955 }
3956
John McCallf1549f62010-07-06 01:34:17 +00003957 CGF.ObjCEHValueStack.pop_back();
3958
John McCall0b251722010-08-04 05:59:32 +00003959 // If nothing wanted anything to do with the caught exception,
3960 // kill the extract call.
3961 if (Caught->use_empty())
3962 Caught->eraseFromParent();
3963
3964 if (!AllMatched)
3965 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3966
3967 if (HasFinally) {
3968 // Emit the exception handler for the @catch blocks.
3969 CGF.EmitBlock(CatchHandler);
3970
3971 // In theory we might now need a write hazard, but actually it's
3972 // unnecessary because there's no local-accessing code between
3973 // the try's write hazard and here.
3974 //Hazards.emitWriteHazard();
3975
John McCall9e2213d2010-10-04 23:42:51 +00003976 // Extract the new exception and save it to the
3977 // propagating-exception slot.
3978 assert(PropagatingExnVar);
3979 llvm::CallInst *NewCaught =
3980 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3981 ExceptionData, "caught");
3982 NewCaught->setDoesNotThrow();
3983 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3984
John McCall0b251722010-08-04 05:59:32 +00003985 // Don't pop the catch handler; the throw already did.
3986 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003987 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003988 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003989 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003990
John McCall87bb5822010-07-31 23:20:56 +00003991 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003992 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003993
John McCallf1549f62010-07-06 01:34:17 +00003994 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003995 CGF.Builder.restoreIP(TryFallthroughIP);
3996 if (CGF.HaveInsertPoint())
3997 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003998 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003999 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004000
John McCallf1549f62010-07-06 01:34:17 +00004001 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00004002 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004003 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004004 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004005 // If we have a propagating-exception variable, check it.
4006 llvm::Value *PropagatingExn;
4007 if (PropagatingExnVar) {
4008 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004009
John McCall9e2213d2010-10-04 23:42:51 +00004010 // Otherwise, just look in the buffer for the exception to throw.
4011 } else {
4012 llvm::CallInst *Caught =
4013 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4014 ExceptionData);
4015 Caught->setDoesNotThrow();
4016 PropagatingExn = Caught;
4017 }
4018
4019 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004020 ->setDoesNotThrow();
4021 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004022 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004023
John McCall87bb5822010-07-31 23:20:56 +00004024 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004025}
4026
4027void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00004028 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004029 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004030
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004031 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004032 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004033 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004034 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004035 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004036 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004037 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004038 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004039 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004040
John McCallf1549f62010-07-06 01:34:17 +00004041 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4042 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004043 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004044
4045 // Clear the insertion point to indicate we are in unreachable code.
4046 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004047}
4048
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004049/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004050/// object: objc_read_weak (id *src)
4051///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004052llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004053 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004054 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004055 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4056 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4057 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004058 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004059 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004060 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004061 return read_weak;
4062}
4063
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004064/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4065/// objc_assign_weak (id src, id *dst)
4066///
4067void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004068 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004069 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004070 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004071 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004072 assert(Size <= 8 && "does not support size > 8");
4073 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004074 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004075 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4076 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004077 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4078 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004079 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004080 src, dst, "weakassign");
4081 return;
4082}
4083
Fariborz Jahanian58626502008-11-19 00:59:10 +00004084/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4085/// objc_assign_global (id src, id *dst)
4086///
4087void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004088 llvm::Value *src, llvm::Value *dst,
4089 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004090 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004091 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004092 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004093 assert(Size <= 8 && "does not support size > 8");
4094 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004095 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004096 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4097 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004098 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4099 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004100 if (!threadlocal)
4101 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4102 src, dst, "globalassign");
4103 else
4104 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4105 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004106 return;
4107}
4108
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004109/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004110/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004111///
4112void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004113 llvm::Value *src, llvm::Value *dst,
4114 llvm::Value *ivarOffset) {
4115 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004116 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004117 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004118 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004119 assert(Size <= 8 && "does not support size > 8");
4120 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004122 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4123 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004124 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4125 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004126 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4127 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004128 return;
4129}
4130
Fariborz Jahanian58626502008-11-19 00:59:10 +00004131/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4132/// objc_assign_strongCast (id src, id *dst)
4133///
4134void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004135 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004136 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004137 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004138 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004139 assert(Size <= 8 && "does not support size > 8");
4140 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004142 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4143 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004144 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4145 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004146 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004147 src, dst, "weakassign");
4148 return;
4149}
4150
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004151void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 llvm::Value *DestPtr,
4153 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004154 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004155 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4156 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004157 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004158 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004159 return;
4160}
4161
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004162/// EmitObjCValueForIvar - Code Gen for ivar reference.
4163///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004164LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4165 QualType ObjectTy,
4166 llvm::Value *BaseValue,
4167 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004168 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004169 const ObjCInterfaceDecl *ID =
4170 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004171 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4172 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004173}
4174
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004175llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004176 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004177 const ObjCIvarDecl *Ivar) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004178 CharUnits Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
4179 return llvm::ConstantInt::get(ObjCTypes.LongTy, Offset.getQuantity());
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004180}
4181
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004182/* *** Private Interface *** */
4183
4184/// EmitImageInfo - Emit the image info marker used to encode some module
4185/// level information.
4186///
4187/// See: <rdr://4810609&4810587&4810587>
4188/// struct IMAGE_INFO {
4189/// unsigned version;
4190/// unsigned flags;
4191/// };
4192enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004193 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004194 eImageInfo_GarbageCollected = (1 << 1),
4195 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004196 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4197
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004198 // A flag indicating that the module has no instances of a @synthesize of a
4199 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004200 eImageInfo_CorrectedSynthesize = (1 << 4),
4201 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004202};
4203
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004204void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004205 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004206 const char *Section = (ObjCABI == 1) ?
4207 "__OBJC, __image_info,regular" :
4208 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004209
Bill Wendling3973acc2012-02-16 01:13:30 +00004210 // Generate module-level named metadata to convey this information to the
4211 // linker and code-gen.
4212 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004213
Bill Wendling3973acc2012-02-16 01:13:30 +00004214 // Add the ObjC ABI version to the module flags.
4215 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4216 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4217 version);
4218 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4219 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004220
David Blaikie4e4d0842012-03-11 07:00:24 +00004221 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004222 // Non-GC overrides those files which specify GC.
4223 Mod.addModuleFlag(llvm::Module::Override,
4224 "Objective-C Garbage Collection", (uint32_t)0);
4225 } else {
4226 // Add the ObjC garbage collection value.
4227 Mod.addModuleFlag(llvm::Module::Error,
4228 "Objective-C Garbage Collection",
4229 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004230
David Blaikie4e4d0842012-03-11 07:00:24 +00004231 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004232 // Add the ObjC GC Only value.
4233 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4234 eImageInfo_GCOnly);
4235
4236 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4237 llvm::Value *Ops[2] = {
4238 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4239 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4240 eImageInfo_GarbageCollected)
4241 };
4242 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4243 llvm::MDNode::get(VMContext, Ops));
4244 }
4245 }
Bill Wendling09822512012-04-24 11:04:57 +00004246
4247 // Indicate whether we're compiling this to run on a simulator.
4248 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4249 if (Triple.getOS() == llvm::Triple::IOS &&
4250 (Triple.getArch() == llvm::Triple::x86 ||
4251 Triple.getArch() == llvm::Triple::x86_64))
4252 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4253 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004254}
4255
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004256// struct objc_module {
4257// unsigned long version;
4258// unsigned long size;
4259// const char *name;
4260// Symtab symtab;
4261// };
4262
4263// FIXME: Get from somewhere
4264static const int ModuleVersion = 7;
4265
4266void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004267 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004268
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004269 llvm::Constant *Values[] = {
4270 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4271 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4272 // This used to be the filename, now it is unused. <rdr://4327263>
4273 GetClassName(&CGM.getContext().Idents.get("")),
4274 EmitModuleSymbols()
4275 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004276 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004277 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004278 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004279 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004280}
4281
4282llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004283 unsigned NumClasses = DefinedClasses.size();
4284 unsigned NumCategories = DefinedCategories.size();
4285
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004286 // Return null if no symbols were defined.
4287 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004288 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004289
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004290 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004291 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004292 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004293 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4294 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004295
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004296 // The runtime expects exactly the list of defined classes followed
4297 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004298 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004299 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004300 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004301 ObjCTypes.Int8PtrTy);
4302 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004303 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004304 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004305 ObjCTypes.Int8PtrTy);
4306
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004307 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004308 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004309 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004310 Symbols);
4311
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004312 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004313
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004314 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004315 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4316 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004317 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004318 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004319}
4320
John McCallf85e1932011-06-15 23:02:42 +00004321llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4322 IdentifierInfo *II) {
4323 LazySymbols.insert(II);
4324
4325 llvm::GlobalVariable *&Entry = ClassReferences[II];
4326
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004327 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004328 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004329 llvm::ConstantExpr::getBitCast(GetClassName(II),
4330 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004331 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004332 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4333 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4334 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004335 }
John McCallf85e1932011-06-15 23:02:42 +00004336
Benjamin Kramer578faa82011-09-27 21:06:10 +00004337 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004338}
4339
John McCallf85e1932011-06-15 23:02:42 +00004340llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4341 const ObjCInterfaceDecl *ID) {
4342 return EmitClassRefFromId(Builder, ID->getIdentifier());
4343}
4344
4345llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4346 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4347 return EmitClassRefFromId(Builder, II);
4348}
4349
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004350llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4351 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004352 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004354 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004355 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004356 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004357 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004358 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004359 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4360 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004361 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004362 }
4363
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004364 if (lvalue)
4365 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004366 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004367}
4368
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004369llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004370 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004371
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004372 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004374 llvm::ConstantDataArray::getString(VMContext,
4375 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004376 ((ObjCABI == 2) ?
4377 "__TEXT,__objc_classname,cstring_literals" :
4378 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004379 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004380
Owen Andersona1cf15f2009-07-14 23:10:40 +00004381 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004382}
4383
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004384llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4385 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4386 I = MethodDefinitions.find(MD);
4387 if (I != MethodDefinitions.end())
4388 return I->second;
4389
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004390 return NULL;
4391}
4392
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004393/// GetIvarLayoutName - Returns a unique constant for the given
4394/// ivar layout bitmap.
4395llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004396 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004397 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004398}
4399
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004400void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Eli Friedman38ad4f42012-11-06 01:51:20 +00004401 CharUnits BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004402 bool ForStrongLayout,
4403 bool &HasUnion) {
4404 const RecordDecl *RD = RT->getDecl();
4405 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004406 SmallVector<const FieldDecl*, 16> Fields;
4407 for (RecordDecl::field_iterator i = RD->field_begin(),
4408 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004409 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004410 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004412 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004413
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004414 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4415 ForStrongLayout, HasUnion);
4416}
4417
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004418void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004419 const llvm::StructLayout *Layout,
4420 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004421 ArrayRef<const FieldDecl*> RecFields,
Eli Friedman38ad4f42012-11-06 01:51:20 +00004422 CharUnits BytePos, bool ForStrongLayout,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004424 bool IsUnion = (RD && RD->isUnion());
Eli Friedman8cb809a2012-11-06 03:41:04 +00004425 CharUnits MaxUnionIvarSize = CharUnits::Zero();
4426 CharUnits MaxSkippedUnionIvarSize = CharUnits::Zero();
Jordy Rosedb8264e2011-07-22 02:08:32 +00004427 const FieldDecl *MaxField = 0;
4428 const FieldDecl *MaxSkippedField = 0;
4429 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004430 CharUnits MaxFieldOffset = CharUnits::Zero();
4431 CharUnits MaxSkippedFieldOffset = CharUnits::Zero();
4432 CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero();
4433 CharUnits FirstFieldDelta = CharUnits::Zero();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004434
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004435 if (RecFields.empty())
4436 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004437 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004438 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004439 const ObjCIvarDecl *FirstField = cast<ObjCIvarDecl>(RecFields[0]);
4440 FirstFieldDelta = ComputeIvarBaseOffset(CGM, OI, FirstField);
John McCallf85e1932011-06-15 23:02:42 +00004441 }
4442
Chris Lattnerf1690852009-03-31 08:48:01 +00004443 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004444 const FieldDecl *Field = RecFields[i];
Eli Friedman38ad4f42012-11-06 01:51:20 +00004445 CharUnits FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004446 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004447 // Note that 'i' here is actually the field index inside RD of Field,
4448 // although this dependency is hidden.
4449 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Eli Friedman38ad4f42012-11-06 01:51:20 +00004450 FieldOffset = CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)) -
4451 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++) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004503 CharUnits Size = CGM.getContext().getTypeSizeInChars(RT);
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
Eli Friedman8cb809a2012-11-06 03:41:04 +00004518 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(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) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00004522 CharUnits UnionIvarSize = FieldSize;
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 {
Eli Friedman8cb809a2012-11-06 03:41:04 +00004529 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, FieldSize));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004530 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004531 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004532 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4533 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004534 if (IsUnion) {
Eli Friedman8cb809a2012-11-06 03:41:04 +00004535 CharUnits UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004536 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004537 MaxSkippedUnionIvarSize = UnionIvarSize;
4538 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004539 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004540 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004541 } else {
Eli Friedman8cb809a2012-11-06 03:41:04 +00004542 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, FieldSize));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004543 }
4544 }
4545 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004546
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004547 if (LastFieldBitfieldOrUnnamed) {
4548 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4549 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004550 uint64_t BitFieldSize
4551 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004552 GC_IVAR skivar;
4553 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
Eli Friedman8cb809a2012-11-06 03:41:04 +00004554 skivar.ivar_size = CharUnits::fromQuantity(
Eli Friedman13071202012-11-06 21:10:22 +00004555 (BitFieldSize + ByteSizeInBits - 1) / ByteSizeInBits);
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004556 SkipIvars.push_back(skivar);
4557 } else {
4558 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4559 // Last field was unnamed. Must update skip info.
Eli Friedman8cb809a2012-11-06 03:41:04 +00004560 CharUnits FieldSize
4561 = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004562 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
Eli Friedman8cb809a2012-11-06 03:41:04 +00004563 FieldSize));
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004564 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004565 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004567 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004568 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004569 MaxUnionIvarSize));
4570 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004571 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004572 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004573}
4574
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004575/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4576/// the computations and returning the layout bitmap (for ivar or blocks) in
4577/// the given argument BitMap string container. Routine reads
4578/// two containers, IvarsInfo and SkipIvars which are assumed to be
4579/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004580llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Chris Lattner8b418682012-02-07 00:39:47 +00004581 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004582
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004583 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004584 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004585 CharUnits WordSize = CharUnits::fromQuantity(
4586 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy));
Eli Friedman8cb809a2012-11-06 03:41:04 +00004587 unsigned WordsToSkip = IvarsInfo[0].ivar_bytepos / WordSize;
4588 unsigned WordsToScan = IvarsInfo[0].ivar_size / WordSize;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004589
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004590 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004591 CharUnits TailPrevGCObjC =
Eli Friedman8cb809a2012-11-06 03:41:04 +00004592 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004593 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004594 // consecutive 'scanned' object pointers.
Eli Friedman8cb809a2012-11-06 03:41:04 +00004595 WordsToScan += IvarsInfo[i].ivar_size / WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004596 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004597 // Skip over 'gc'able object pointer which lay over each other.
4598 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4599 continue;
4600 // Must skip over 1 or more words. We save current skip/scan values
4601 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004602 SKIP_SCAN SkScan;
4603 SkScan.skip = WordsToSkip;
4604 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004605 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004606
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004607 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004608 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4609 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004610 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004611 WordsToSkip = 0;
Eli Friedman8cb809a2012-11-06 03:41:04 +00004612 WordsToScan = IvarsInfo[i].ivar_size / WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004613 }
4614 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004615 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004616 SKIP_SCAN SkScan;
4617 SkScan.skip = WordsToSkip;
4618 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004619 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004620 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004621
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004622 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004623 unsigned int LastIndex = SkipIvars.size()-1;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004624 CharUnits LastByteSkipped =
4625 SkipIvars[LastIndex].ivar_bytepos +
Eli Friedman8cb809a2012-11-06 03:41:04 +00004626 SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004627 LastIndex = IvarsInfo.size()-1;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004628 CharUnits LastByteScanned =
4629 IvarsInfo[LastIndex].ivar_bytepos +
Eli Friedman8cb809a2012-11-06 03:41:04 +00004630 IvarsInfo[LastIndex].ivar_size;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004631 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004632 if (LastByteSkipped > LastByteScanned) {
Eli Friedman38ad4f42012-11-06 01:51:20 +00004633 unsigned int TotalWords =
4634 (LastByteSkipped + (WordSize - CharUnits::One())) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004635 SKIP_SCAN SkScan;
Eli Friedman38ad4f42012-11-06 01:51:20 +00004636 SkScan.skip = TotalWords - LastByteScanned / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004637 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004638 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004639 }
4640 }
4641 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4642 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004643 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004644 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004645 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4646 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4647 // 0xM0 followed by 0x0N detected.
4648 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4649 for (int j = i+1; j < SkipScan; j++)
4650 SkipScanIvars[j] = SkipScanIvars[j+1];
4651 --SkipScan;
4652 }
4653 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004654
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004655 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004656 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004657 unsigned char byte;
4658 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4659 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4660 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4661 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004662
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004663 // first skip big.
4664 for (unsigned int ix = 0; ix < skip_big; ix++)
4665 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004666
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004667 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004668 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004669 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004670 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004671 byte |= 0xf;
4672 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004673 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004674 byte |= scan_small;
4675 scan_small = 0;
4676 }
4677 BitMap += byte;
4678 }
4679 // next scan big
4680 for (unsigned int ix = 0; ix < scan_big; ix++)
4681 BitMap += (unsigned char)(0x0f);
4682 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004683 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004684 byte = scan_small;
4685 BitMap += byte;
4686 }
4687 }
4688 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004689 unsigned char zero = 0;
4690 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004691
4692 llvm::GlobalVariable * Entry =
4693 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004694 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004695 ((ObjCABI == 2) ?
4696 "__TEXT,__objc_classname,cstring_literals" :
4697 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004698 1, true);
4699 return getConstantGEP(VMContext, Entry, 0, 0);
4700}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004702/// BuildIvarLayout - Builds ivar layout bitmap for the class
4703/// implementation for the __strong or __weak case.
4704/// The layout map displays which words in ivar list must be skipped
4705/// and which must be scanned by GC (see below). String is built of bytes.
4706/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4707/// of words to skip and right nibble is count of words to scan. So, each
4708/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4709/// represented by a 0x00 byte which also ends the string.
4710/// 1. when ForStrongLayout is true, following ivars are scanned:
4711/// - id, Class
4712/// - object *
4713/// - __strong anything
4714///
4715/// 2. When ForStrongLayout is false, following ivars are scanned:
4716/// - __weak anything
4717///
4718llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4719 const ObjCImplementationDecl *OMD,
4720 bool ForStrongLayout) {
4721 bool hasUnion = false;
4722
Chris Lattner8b418682012-02-07 00:39:47 +00004723 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004724 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4725 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004726 return llvm::Constant::getNullValue(PtrTy);
4727
Jordy Rosedb8264e2011-07-22 02:08:32 +00004728 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4729 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004730 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004731 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004732 IVD; IVD = IVD->getNextIvar())
4733 RecFields.push_back(cast<FieldDecl>(IVD));
4734 }
4735 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004736 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004737 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004738
Jordy Rosedb8264e2011-07-22 02:08:32 +00004739 // FIXME: This is not ideal; we shouldn't have to do this copy.
4740 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004741 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004742
4743 if (RecFields.empty())
4744 return llvm::Constant::getNullValue(PtrTy);
4745
4746 SkipIvars.clear();
4747 IvarsInfo.clear();
4748
Eli Friedman38ad4f42012-11-06 01:51:20 +00004749 BuildAggrIvarLayout(OMD, 0, 0, RecFields, CharUnits::Zero(),
4750 ForStrongLayout, hasUnion);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004751 if (IvarsInfo.empty())
4752 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004753 // Sort on byte position in case we encounterred a union nested in
4754 // the ivar list.
4755 if (hasUnion && !IvarsInfo.empty())
4756 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4757 if (hasUnion && !SkipIvars.empty())
4758 std::sort(SkipIvars.begin(), SkipIvars.end());
4759
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004760 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004761 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004762
David Blaikie4e4d0842012-03-11 07:00:24 +00004763 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004765 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004766 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004767 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004768 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004769 if (!(s[i] & 0xf0))
4770 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4771 else
4772 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4773 printf("\n");
4774 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004775 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004776}
4777
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004778llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004779 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4780
Chris Lattner0b239712012-02-06 22:16:34 +00004781 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004782 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004783 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004784 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004785 ((ObjCABI == 2) ?
4786 "__TEXT,__objc_methname,cstring_literals" :
4787 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004788 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004789
Owen Andersona1cf15f2009-07-14 23:10:40 +00004790 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004791}
4792
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004793// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004794llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004795 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4796}
4797
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004798llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004799 std::string TypeStr;
4800 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4801
4802 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004803
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004804 if (!Entry)
4805 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004806 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004807 ((ObjCABI == 2) ?
4808 "__TEXT,__objc_methtype,cstring_literals" :
4809 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004810 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004811
Owen Andersona1cf15f2009-07-14 23:10:40 +00004812 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004813}
4814
Bob Wilsondc8dab62011-11-30 01:57:58 +00004815llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4816 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004817 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004818 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004819 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004820
4821 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4822
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004823 if (!Entry)
4824 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004825 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004826 ((ObjCABI == 2) ?
4827 "__TEXT,__objc_methtype,cstring_literals" :
4828 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004829 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004830
Owen Andersona1cf15f2009-07-14 23:10:40 +00004831 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004832}
4833
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004834// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004835llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004836 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004837
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004838 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004839 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004840 llvm::ConstantDataArray::getString(VMContext,
4841 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004842 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004843 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004844
Owen Andersona1cf15f2009-07-14 23:10:40 +00004845 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004846}
4847
4848// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004849// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004850llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4852 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004853 std::string TypeStr;
4854 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004855 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4856}
4857
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004858void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004859 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004860 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004861 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004862 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004863 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4864 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004865 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004866 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004867 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004868 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004869}
4870
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004871void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004872 EmitModuleInfo();
4873
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004874 // Emit the dummy bodies for any protocols which were referenced but
4875 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004876 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004877 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4878 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004879 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004880
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004881 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004882 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004883 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004884 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004885 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004886 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004887 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004888 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004889 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004890 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004891 }
4892
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004893 // Add assembler directives to add lazy undefined symbol references
4894 // for classes which are referenced but not defined. This is
4895 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004896 //
4897 // FIXME: It would be nice if we had an LLVM construct for this.
4898 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004899 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004900 Asm += CGM.getModule().getModuleInlineAsm();
4901 if (!Asm.empty() && Asm.back() != '\n')
4902 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004903
Daniel Dunbar33063492009-09-07 00:20:42 +00004904 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004905 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4906 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004907 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4908 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004909 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004910 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004911 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004912 }
4913
Bill Wendling13562a12012-02-07 09:06:01 +00004914 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004915 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4916 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4917 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004918
Daniel Dunbar33063492009-09-07 00:20:42 +00004919 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004920 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004921}
4922
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004924 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004925 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004926 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004927 ObjCABI = 2;
4928}
4929
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004930/* *** */
4931
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004932ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004933 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4934{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004935 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4936 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004937
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004938 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004939 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004940 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004941 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004942 Int8PtrTy = CGM.Int8PtrTy;
4943 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004944
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004945 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004946 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004947 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004948
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004949 // I'm not sure I like this. The implicit coordination is a bit
4950 // gross. We should solve this in a reasonable fashion because this
4951 // is a pretty common task (match some runtime data structure with
4952 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004953
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004954 // FIXME: This is leaked.
4955 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004957 // struct _objc_super {
4958 // id self;
4959 // Class cls;
4960 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004961 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004962 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004963 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004964 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004965 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004966 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004967 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004968 Ctx.getObjCClassType(), 0, 0, false,
4969 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004970 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004971
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004972 SuperCTy = Ctx.getTagDeclType(RD);
4973 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004974
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004975 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004976 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4977
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004978 // struct _prop_t {
4979 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004980 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004981 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004982 PropertyTy = llvm::StructType::create("struct._prop_t",
4983 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004984
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004985 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004986 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004987 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004988 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004989 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004990 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004991 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4992 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004993 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004994 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004995
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004996 // struct _objc_method {
4997 // SEL _cmd;
4998 // char *method_type;
4999 // char *_imp;
5000 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005001 MethodTy = llvm::StructType::create("struct._objc_method",
5002 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5003 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005004
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005005 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005006 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005007 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005008
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005009}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005010
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005011ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005012 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005013 // struct _objc_method_description {
5014 // SEL name;
5015 // char *types;
5016 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005017 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005018 llvm::StructType::create("struct._objc_method_description",
5019 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005020
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005021 // struct _objc_method_description_list {
5022 // int count;
5023 // struct _objc_method_description[1];
5024 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005026 llvm::StructType::create("struct._objc_method_description_list",
5027 IntTy,
5028 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005030 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005031 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005032 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005033
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005034 // Protocol description structures
5035
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005036 // struct _objc_protocol_extension {
5037 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5038 // struct _objc_method_description_list *optional_instance_methods;
5039 // struct _objc_method_description_list *optional_class_methods;
5040 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005041 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005042 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005043 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005044 llvm::StructType::create("struct._objc_protocol_extension",
5045 IntTy, MethodDescriptionListPtrTy,
5046 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005047 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005048
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005049 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005050 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005051
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005052 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005053
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005054 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005055 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005056
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005057 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005058 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005059 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005060 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005061 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005062 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005063
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005064 // struct _objc_protocol {
5065 // struct _objc_protocol_extension *isa;
5066 // char *protocol_name;
5067 // struct _objc_protocol **_objc_protocol_list;
5068 // struct _objc_method_description_list *instance_methods;
5069 // struct _objc_method_description_list *class_methods;
5070 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005071 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5072 llvm::PointerType::getUnqual(ProtocolListTy),
5073 MethodDescriptionListPtrTy,
5074 MethodDescriptionListPtrTy,
5075 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005076
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005077 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005078 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005079
Owen Anderson96e0fc72009-07-29 22:16:19 +00005080 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005081
5082 // Class description structures
5083
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005084 // struct _objc_ivar {
5085 // char *ivar_name;
5086 // char *ivar_type;
5087 // int ivar_offset;
5088 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005089 IvarTy = llvm::StructType::create("struct._objc_ivar",
5090 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005091
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005092 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005093 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005094 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005095 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005096
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005097 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005098 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005099 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005100 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005101
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005102 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005103 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005104 llvm::StructType::create("struct._objc_class_extension",
5105 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005106 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005107
Chris Lattnerc1c20112011-08-12 17:43:31 +00005108 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005109
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005110 // struct _objc_class {
5111 // Class isa;
5112 // Class super_class;
5113 // char *name;
5114 // long version;
5115 // long info;
5116 // long instance_size;
5117 // struct _objc_ivar_list *ivars;
5118 // struct _objc_method_list *methods;
5119 // struct _objc_cache *cache;
5120 // struct _objc_protocol_list *protocols;
5121 // char *ivar_layout;
5122 // struct _objc_class_ext *ext;
5123 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005124 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5125 llvm::PointerType::getUnqual(ClassTy),
5126 Int8PtrTy,
5127 LongTy,
5128 LongTy,
5129 LongTy,
5130 IvarListPtrTy,
5131 MethodListPtrTy,
5132 CachePtrTy,
5133 ProtocolListPtrTy,
5134 Int8PtrTy,
5135 ClassExtensionPtrTy,
5136 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137
Owen Anderson96e0fc72009-07-29 22:16:19 +00005138 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005139
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005140 // struct _objc_category {
5141 // char *category_name;
5142 // char *class_name;
5143 // struct _objc_method_list *instance_method;
5144 // struct _objc_method_list *class_method;
5145 // uint32_t size; // sizeof(struct _objc_category)
5146 // struct _objc_property_list *instance_properties;// category's @property
5147 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005148 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005149 llvm::StructType::create("struct._objc_category",
5150 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5151 MethodListPtrTy, ProtocolListPtrTy,
5152 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005153
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005154 // Global metadata structures
5155
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005156 // struct _objc_symtab {
5157 // long sel_ref_cnt;
5158 // SEL *refs;
5159 // short cls_def_cnt;
5160 // short cat_def_cnt;
5161 // char *defs[cls_def_cnt + cat_def_cnt];
5162 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005163 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005164 llvm::StructType::create("struct._objc_symtab",
5165 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5166 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005167 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005168
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005169 // struct _objc_module {
5170 // long version;
5171 // long size; // sizeof(struct _objc_module)
5172 // char *name;
5173 // struct _objc_symtab* symtab;
5174 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005175 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005176 llvm::StructType::create("struct._objc_module",
5177 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005178
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005179
Mike Stumpf5408fe2009-05-16 07:57:57 +00005180 // FIXME: This is the size of the setjmp buffer and should be target
5181 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005182 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005183
Anders Carlsson124526b2008-09-09 10:10:21 +00005184 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005185 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186
5187 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005188 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005189 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5190 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005191
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005192}
5193
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005194ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005195 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005196 // struct _method_list_t {
5197 // uint32_t entsize; // sizeof(struct _objc_method)
5198 // uint32_t method_count;
5199 // struct _objc_method method_list[method_count];
5200 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005201 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005202 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5203 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005204 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005205 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005206
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005207 // struct _protocol_t {
5208 // id isa; // NULL
5209 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005210 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005211 // const struct method_list_t * const instance_methods;
5212 // const struct method_list_t * const class_methods;
5213 // const struct method_list_t *optionalInstanceMethods;
5214 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005215 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005216 // const uint32_t size; // sizeof(struct _protocol_t)
5217 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005218 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005219 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005220
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005221 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005222 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005223 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005224
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005225 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005226 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5227 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5228 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5229 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005230 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5231 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005232
5233 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005234 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005235
Fariborz Jahanianda320092009-01-29 19:24:30 +00005236 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005237 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005238 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005239 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005240 ProtocolListnfABITy->setBody(LongTy,
5241 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5242 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005243
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005244 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005245 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005246
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005247 // struct _ivar_t {
5248 // unsigned long int *offset; // pointer to ivar offset location
5249 // char *name;
5250 // char *type;
5251 // uint32_t alignment;
5252 // uint32_t size;
5253 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005254 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005255 llvm::StructType::create("struct._ivar_t",
5256 llvm::PointerType::getUnqual(LongTy),
5257 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005258
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005259 // struct _ivar_list_t {
5260 // uint32 entsize; // sizeof(struct _ivar_t)
5261 // uint32 count;
5262 // struct _iver_t list[count];
5263 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005264 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005265 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5266 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005267
Owen Anderson96e0fc72009-07-29 22:16:19 +00005268 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005269
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005270 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005271 // uint32_t const flags;
5272 // uint32_t const instanceStart;
5273 // uint32_t const instanceSize;
5274 // uint32_t const reserved; // only when building for 64bit targets
5275 // const uint8_t * const ivarLayout;
5276 // const char *const name;
5277 // const struct _method_list_t * const baseMethods;
5278 // const struct _objc_protocol_list *const baseProtocols;
5279 // const struct _ivar_list_t *const ivars;
5280 // const uint8_t * const weakIvarLayout;
5281 // const struct _prop_list_t * const properties;
5282 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005283
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005284 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005285 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5286 IntTy, IntTy, IntTy, Int8PtrTy,
5287 Int8PtrTy, MethodListnfABIPtrTy,
5288 ProtocolListnfABIPtrTy,
5289 IvarListnfABIPtrTy,
5290 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005292 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005293 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005294 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5295 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005296
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005297 // struct _class_t {
5298 // struct _class_t *isa;
5299 // struct _class_t * const superclass;
5300 // void *cache;
5301 // IMP *vtable;
5302 // struct class_ro_t *ro;
5303 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304
Chris Lattnerc1c20112011-08-12 17:43:31 +00005305 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005306 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5307 llvm::PointerType::getUnqual(ClassnfABITy),
5308 CachePtrTy,
5309 llvm::PointerType::getUnqual(ImpnfABITy),
5310 llvm::PointerType::getUnqual(ClassRonfABITy),
5311 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005313 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005314 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005315
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005316 // struct _category_t {
5317 // const char * const name;
5318 // struct _class_t *const cls;
5319 // const struct _method_list_t * const instance_methods;
5320 // const struct _method_list_t * const class_methods;
5321 // const struct _protocol_list_t * const protocols;
5322 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005323 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005324 CategorynfABITy = llvm::StructType::create("struct._category_t",
5325 Int8PtrTy, ClassnfABIPtrTy,
5326 MethodListnfABIPtrTy,
5327 MethodListnfABIPtrTy,
5328 ProtocolListnfABIPtrTy,
5329 PropertyListPtrTy,
5330 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005331
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005332 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005333 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5334 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005335
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005336 // MessageRefTy - LLVM for:
5337 // struct _message_ref_t {
5338 // IMP messenger;
5339 // SEL name;
5340 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005341
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005342 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005343 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005344 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005345 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005346 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005347 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005348 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005349 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005350 Ctx.getObjCSelType(), 0, 0, false,
5351 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005352 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005353
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005354 MessageRefCTy = Ctx.getTagDeclType(RD);
5355 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5356 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005358 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005359 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005361 // SuperMessageRefTy - LLVM for:
5362 // struct _super_message_ref_t {
5363 // SUPER_IMP messenger;
5364 // SEL name;
5365 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005366 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005367 llvm::StructType::create("struct._super_message_ref_t",
5368 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005370 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005371 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005372
Daniel Dunbare588b992009-03-01 04:46:24 +00005373
5374 // struct objc_typeinfo {
5375 // const void** vtable; // objc_ehtype_vtable + 2
5376 // const char* name; // c++ typeinfo string
5377 // Class cls;
5378 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005379 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005380 llvm::StructType::create("struct._objc_typeinfo",
5381 llvm::PointerType::getUnqual(Int8PtrTy),
5382 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005383 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005384}
5385
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005387 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005388
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005389 return NULL;
5390}
5391
Bill Wendlingbb028552012-02-07 09:25:09 +00005392void CGObjCNonFragileABIMac::
5393AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5394 const char *SymbolName,
5395 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005396 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397
Daniel Dunbar463b8762009-05-15 21:48:48 +00005398 if (!NumClasses)
5399 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005400
Chris Lattner0b239712012-02-06 22:16:34 +00005401 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005402 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005403 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005404 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005405 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005406 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005407 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005408 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409
Daniel Dunbar463b8762009-05-15 21:48:48 +00005410 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005411 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005412 llvm::GlobalValue::InternalLinkage,
5413 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005414 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005415 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005416 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005417 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005418}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005419
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005420void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5421 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005422
Daniel Dunbar463b8762009-05-15 21:48:48 +00005423 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005424 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005426 "\01L_OBJC_LABEL_CLASS_$",
5427 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005428
Bill Wendling13562a12012-02-07 09:06:01 +00005429 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005430 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5431 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5432 continue;
5433 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005434 }
5435
Bill Wendling13562a12012-02-07 09:06:01 +00005436 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005437 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5438 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5439 continue;
5440 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5441 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005442
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005444 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5445 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005447 // Build list of all implemented category addresses in array
5448 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005450 "\01L_OBJC_LABEL_CATEGORY_$",
5451 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005452 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005453 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5454 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005456 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005457}
5458
John McCall944c8432011-05-14 03:10:52 +00005459/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5460/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005462/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005463bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5464 // At various points we've experimented with using vtable-based
5465 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005466 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005467 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005468 return false;
John McCall944c8432011-05-14 03:10:52 +00005469 case CodeGenOptions::NonLegacy:
5470 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005471 case CodeGenOptions::Mixed:
5472 break;
5473 }
5474
5475 // If so, see whether this selector is in the white-list of things which must
5476 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005477 if (VTableDispatchMethods.empty()) {
5478 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5479 VTableDispatchMethods.insert(GetNullarySelector("class"));
5480 VTableDispatchMethods.insert(GetNullarySelector("self"));
5481 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5482 VTableDispatchMethods.insert(GetNullarySelector("length"));
5483 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484
John McCall944c8432011-05-14 03:10:52 +00005485 // These are vtable-based if GC is disabled.
5486 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005487 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005488 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5489 VTableDispatchMethods.insert(GetNullarySelector("release"));
5490 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5491 }
5492
5493 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5494 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5495 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5496 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5497 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5498 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5499 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5500
5501 // These are vtable-based if GC is enabled.
5502 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005503 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005504 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5505 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5506
5507 // "countByEnumeratingWithState:objects:count"
5508 IdentifierInfo *KeyIdents[] = {
5509 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5510 &CGM.getContext().Idents.get("objects"),
5511 &CGM.getContext().Idents.get("count")
5512 };
5513 VTableDispatchMethods.insert(
5514 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5515 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005516 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005517
John McCall944c8432011-05-14 03:10:52 +00005518 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005519}
5520
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005521/// BuildClassRoTInitializer - generate meta-data for:
5522/// struct _class_ro_t {
5523/// uint32_t const flags;
5524/// uint32_t const instanceStart;
5525/// uint32_t const instanceSize;
5526/// uint32_t const reserved; // only when building for 64bit targets
5527/// const uint8_t * const ivarLayout;
5528/// const char *const name;
5529/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005530/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005531/// const struct _ivar_list_t *const ivars;
5532/// const uint8_t * const weakIvarLayout;
5533/// const struct _prop_list_t * const properties;
5534/// }
5535///
5536llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005537 unsigned flags,
5538 unsigned InstanceStart,
5539 unsigned InstanceSize,
5540 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005541 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005542 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005543
David Blaikie4e4d0842012-03-11 07:00:24 +00005544 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005545 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005546
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005547 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5548 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5549 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005550 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005551 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5552 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005553 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005554 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005555 // const struct _method_list_t * const baseMethods;
5556 std::vector<llvm::Constant*> Methods;
5557 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005558 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005559 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005561 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005562 // Class methods should always be defined.
5563 Methods.push_back(GetMethodConstant(*i));
5564 }
5565 } else {
5566 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005567 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005568 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005569 // Instance methods should always be defined.
5570 Methods.push_back(GetMethodConstant(*i));
5571 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005572 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005573 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005574 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005575
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005576 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5577 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005578
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005579 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5580 if (llvm::Constant *C = GetMethodConstant(MD))
5581 Methods.push_back(C);
5582 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5583 if (llvm::Constant *C = GetMethodConstant(MD))
5584 Methods.push_back(C);
5585 }
5586 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005587 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005588 Values[ 5] = EmitMethodList(MethodListName,
5589 "__DATA, __objc_const", Methods);
5590
Fariborz Jahanianda320092009-01-29 19:24:30 +00005591 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5592 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005593 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005594 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005595 OID->all_referenced_protocol_begin(),
5596 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005597
John McCall621915c2012-10-17 04:53:23 +00005598 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005599 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005600 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005601 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005602 } else {
5603 Values[ 7] = EmitIvarList(ID);
5604 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005605 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5606 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005607 }
Owen Anderson08e25242009-07-27 22:29:56 +00005608 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005609 Values);
5610 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005611 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5612 llvm::GlobalValue::InternalLinkage,
5613 Init,
John McCall621915c2012-10-17 04:53:23 +00005614 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005615 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5616 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005617 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005618 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005619 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005620 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005621
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005622}
5623
5624/// BuildClassMetaData - This routine defines that to-level meta-data
5625/// for the given ClassName for:
5626/// struct _class_t {
5627/// struct _class_t *isa;
5628/// struct _class_t * const superclass;
5629/// void *cache;
5630/// IMP *vtable;
5631/// struct class_ro_t *ro;
5632/// }
5633///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005634llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005635 std::string &ClassName,
5636 llvm::Constant *IsAGV,
5637 llvm::Constant *SuperClassGV,
5638 llvm::Constant *ClassRoGV,
5639 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005640 llvm::Constant *Values[] = {
5641 IsAGV,
5642 SuperClassGV,
5643 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5644 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5645 ClassRoGV // &CLASS_RO_GV
5646 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005647 if (!Values[1])
5648 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005649 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005650 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005651 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5652 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005653 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005654 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005655 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005656 if (HiddenVisibility)
5657 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005658 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005659}
5660
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005661bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005662CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005663 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005664}
5665
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005666void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005667 uint32_t &InstanceStart,
5668 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005669 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005670 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005671
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005672 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005673 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005674
5675 // If there are no fields, the start is the same as the end.
5676 if (!RL.getFieldCount())
5677 InstanceStart = InstanceSize;
5678 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005679 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005680}
5681
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005682void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5683 std::string ClassName = ID->getNameAsString();
5684 if (!ObjCEmptyCacheVar) {
5685 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005686 CGM.getModule(),
5687 ObjCTypes.CacheTy,
5688 false,
5689 llvm::GlobalValue::ExternalLinkage,
5690 0,
5691 "_objc_empty_cache");
5692
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005693 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005694 CGM.getModule(),
5695 ObjCTypes.ImpnfABITy,
5696 false,
5697 llvm::GlobalValue::ExternalLinkage,
5698 0,
5699 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005700 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005702 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005703 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005704 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005705 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005706 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005707 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005708 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5709 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005710
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005711 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712
John McCallb03527a2012-10-17 04:53:31 +00005713 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005714 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005715 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005716 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005717 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005718
5719 // FIXME: why is this flag set on the metaclass?
5720 // ObjC metaclasses have no fields and don't really get constructed.
5721 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005722 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005723 if (!ID->hasNonZeroConstructors())
5724 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5725 }
5726
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005727 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005728 // class is root
John McCall621915c2012-10-17 04:53:23 +00005729 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005730 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005731 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005732 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005733 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005734 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5735 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5736 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005737 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005738 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005739 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005740 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005742 ObjCMetaClassName +
5743 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005744 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005745 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005746 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005747 }
5748 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5749 InstanceStart,
5750 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005751 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005752 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005753 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5754 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005755 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005756
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005757 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005758 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005759 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005760 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005761
5762 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005763 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005764
John McCallb03527a2012-10-17 04:53:31 +00005765 // Set a flag to enable a runtime optimization when a class has
5766 // fields that require destruction but which don't require
5767 // anything except zero-initialization during construction. This
5768 // is most notably true of __strong and __weak types, but you can
5769 // also imagine there being C++ types with non-trivial default
5770 // constructors that merely set all fields to null.
5771 if (!ID->hasNonZeroConstructors())
5772 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5773 }
5774
Douglas Gregor68584ed2009-06-18 16:11:24 +00005775 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005776 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005777
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005778 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005779 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005780 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005781 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005782 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005783 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005784 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005785 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005786 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005787 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005788 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005789 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005790 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005791 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005792 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005793 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005794
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005795 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005796 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005797 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5798 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005799 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005800
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005801 // Determine if this class is also "non-lazy".
5802 if (ImplementationIsNonLazy(ID))
5803 DefinedNonLazyClasses.push_back(ClassMD);
5804
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005805 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005806 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005807 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005808 // Make sure method definition entries are all clear for next implementation.
5809 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005810}
5811
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005812/// GenerateProtocolRef - This routine is called to generate code for
5813/// a protocol reference expression; as in:
5814/// @code
5815/// @protocol(Proto1);
5816/// @endcode
5817/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5818/// which will hold address of the protocol meta-data.
5819///
5820llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005821 const ObjCProtocolDecl *PD) {
5822
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005823 // This routine is called for @protocol only. So, we must build definition
5824 // of protocol's meta-data (not a reference to it!)
5825 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005826 llvm::Constant *Init =
5827 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005828 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005829
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005830 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005831 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005833 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5834 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005835 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005836 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005837 CGM.getModule(),
5838 Init->getType(), false,
5839 llvm::GlobalValue::WeakAnyLinkage,
5840 Init,
5841 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005842 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5843 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005844 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005845 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005846}
5847
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005848/// GenerateCategory - Build metadata for a category implementation.
5849/// struct _category_t {
5850/// const char * const name;
5851/// struct _class_t *const cls;
5852/// const struct _method_list_t * const instance_methods;
5853/// const struct _method_list_t * const class_methods;
5854/// const struct _protocol_list_t * const protocols;
5855/// const struct _prop_list_t * const properties;
5856/// }
5857///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005858void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005859 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005860 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005861 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5862 "_$_" + OCD->getNameAsString());
5863 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005864 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005865
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005866 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005867 Values[0] = GetClassName(OCD->getIdentifier());
5868 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005869 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005870 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005871 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5872
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005873 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005874 std::vector<llvm::Constant*> Methods;
5875 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005876 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005877 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005878
5879 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005880 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005881 // Instance methods should always be defined.
5882 Methods.push_back(GetMethodConstant(*i));
5883 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005884
5885 Values[2] = EmitMethodList(MethodListName,
5886 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005887 Methods);
5888
5889 MethodListName = Prefix;
5890 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5891 OCD->getNameAsString();
5892 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005893 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005894 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005895 // Class methods should always be defined.
5896 Methods.push_back(GetMethodConstant(*i));
5897 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005898
5899 Values[3] = EmitMethodList(MethodListName,
5900 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005901 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005902 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005903 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005904 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005905 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005906 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5907 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005908 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005909 + Interface->getName() + "_$_"
5910 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005911 Category->protocol_begin(),
5912 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005913 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5914 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005915 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005916 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5917 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005918 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005919
5920 llvm::Constant *Init =
5921 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005922 Values);
5923 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005924 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005925 false,
5926 llvm::GlobalValue::InternalLinkage,
5927 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005928 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005929 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005930 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005931 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005932 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005933 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005934
5935 // Determine if this category is also "non-lazy".
5936 if (ImplementationIsNonLazy(OCD))
5937 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005938 // method definition entries must be clear for next implementation.
5939 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005940}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005941
5942/// GetMethodConstant - Return a struct objc_method constant for the
5943/// given method if it has been defined. The result is null if the
5944/// method has not been defined. The return value has type MethodPtrTy.
5945llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005946 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005947 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005948 if (!Fn)
5949 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005950
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005951 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005952 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005953 ObjCTypes.SelectorPtrTy),
5954 GetMethodVarType(MD),
5955 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5956 };
Owen Anderson08e25242009-07-27 22:29:56 +00005957 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005958}
5959
5960/// EmitMethodList - Build meta-data for method declarations
5961/// struct _method_list_t {
5962/// uint32_t entsize; // sizeof(struct _objc_method)
5963/// uint32_t method_count;
5964/// struct _objc_method method_list[method_count];
5965/// }
5966///
Bill Wendlingbb028552012-02-07 09:25:09 +00005967llvm::Constant *
5968CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5969 const char *Section,
5970 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005971 // Return null for empty list.
5972 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005973 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005974
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005975 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005976 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005977 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005978 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005979 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005980 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005981 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005982 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005983 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005984 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005985
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005986 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005987 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005988 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005989 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005990 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005991 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005992 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005993}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005994
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005995/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5996/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005997llvm::GlobalVariable *
5998CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5999 const ObjCIvarDecl *Ivar) {
6000 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006001 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006002 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006004 CGM.getModule().getGlobalVariable(Name);
6005 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006007 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006008 false,
6009 llvm::GlobalValue::ExternalLinkage,
6010 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006011 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006012 return IvarOffsetGV;
6013}
6014
Daniel Dunbare83be122010-04-02 21:14:02 +00006015llvm::Constant *
6016CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6017 const ObjCIvarDecl *Ivar,
Eli Friedman38ad4f42012-11-06 01:51:20 +00006018 CharUnits Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006019 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006020 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Eli Friedman38ad4f42012-11-06 01:51:20 +00006021 Offset.getQuantity()));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006022 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006023 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006024
Mike Stumpf5408fe2009-05-16 07:57:57 +00006025 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6026 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006027 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6028 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006029 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006030 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006031 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006032 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006033 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006034 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006035}
6036
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006037/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006038/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006039/// IvarListnfABIPtrTy.
6040/// struct _ivar_t {
6041/// unsigned long int *offset; // pointer to ivar offset location
6042/// char *name;
6043/// char *type;
6044/// uint32_t alignment;
6045/// uint32_t size;
6046/// }
6047/// struct _ivar_list_t {
6048/// uint32 entsize; // sizeof(struct _ivar_t)
6049/// uint32 count;
6050/// struct _iver_t list[count];
6051/// }
6052///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006053
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006054llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006055 const ObjCImplementationDecl *ID) {
6056
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006057 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006058
Jordy Rosedb8264e2011-07-22 02:08:32 +00006059 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006060 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006061
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006062 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006063
Jordy Rosedb8264e2011-07-22 02:08:32 +00006064 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006065 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006066 // Ignore unnamed bit-fields.
6067 if (!IVD->getDeclName())
6068 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006069 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006070 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006071 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006072 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6073 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006074 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006075 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006076 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006077 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006078 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006079 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006080 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006081 // NOTE. Size of a bitfield does not match gcc's, because of the
6082 // way bitfields are treated special in each. But I am told that
6083 // 'size' for bitfield ivars is ignored by the runtime so it does
6084 // not matter. If it matters, there is enough info to get the
6085 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006086 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006087 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006088 }
6089 // Return null for empty list.
6090 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006091 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006092
6093 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006094 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006095 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6096 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006097 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006098 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006099 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006100 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006101 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6102 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006103 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006104 llvm::GlobalValue::InternalLinkage,
6105 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006106 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006107 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006108 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006109 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006110
Chris Lattnerad64e022009-07-17 23:57:13 +00006111 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006112 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006113}
6114
6115llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006116 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006117 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006118
Fariborz Jahanianda320092009-01-29 19:24:30 +00006119 if (!Entry) {
6120 // We use the initializer as a marker of whether this is a forward
6121 // reference or not. At module finalization we add the empty
6122 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006123 Entry =
6124 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6125 llvm::GlobalValue::ExternalLinkage,
6126 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006127 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006128 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006129 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006130
Fariborz Jahanianda320092009-01-29 19:24:30 +00006131 return Entry;
6132}
6133
6134/// GetOrEmitProtocol - Generate the protocol meta-data:
6135/// @code
6136/// struct _protocol_t {
6137/// id isa; // NULL
6138/// const char * const protocol_name;
6139/// const struct _protocol_list_t * protocol_list; // super protocols
6140/// const struct method_list_t * const instance_methods;
6141/// const struct method_list_t * const class_methods;
6142/// const struct method_list_t *optionalInstanceMethods;
6143/// const struct method_list_t *optionalClassMethods;
6144/// const struct _prop_list_t * properties;
6145/// const uint32_t size; // sizeof(struct _protocol_t)
6146/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006147/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006148/// }
6149/// @endcode
6150///
6151
6152llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006153 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006154 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006155
Fariborz Jahanianda320092009-01-29 19:24:30 +00006156 // Early exit if a defining object has already been generated.
6157 if (Entry && Entry->hasInitializer())
6158 return Entry;
6159
Douglas Gregor1d784b22012-01-01 19:51:50 +00006160 // Use the protocol definition, if there is one.
6161 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6162 PD = Def;
6163
Fariborz Jahanianda320092009-01-29 19:24:30 +00006164 // Construct method lists.
6165 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6166 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006167 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006168 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006169 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006170 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006171 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006172 if (!C)
6173 return GetOrEmitProtocolRef(PD);
6174
Fariborz Jahanianda320092009-01-29 19:24:30 +00006175 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6176 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006177 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006178 } else {
6179 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006180 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006181 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006182 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006183
6184 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006185 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006186 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006187 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006188 if (!C)
6189 return GetOrEmitProtocolRef(PD);
6190
Fariborz Jahanianda320092009-01-29 19:24:30 +00006191 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6192 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006193 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006194 } else {
6195 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006196 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006197 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006198 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006199
Bob Wilsondc8dab62011-11-30 01:57:58 +00006200 MethodTypesExt.insert(MethodTypesExt.end(),
6201 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6202
6203 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006204 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006205 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006206 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006207 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6208 PD->protocol_begin(),
6209 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006210
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006211 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006212 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006213 "__DATA, __objc_const",
6214 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006215 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006216 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006217 "__DATA, __objc_const",
6218 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006219 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006220 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006221 "__DATA, __objc_const",
6222 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006223 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006224 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006225 "__DATA, __objc_const",
6226 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006227 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006228 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006229 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006230 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006231 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006232 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006233 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6234 + PD->getName(),
6235 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006236 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006237 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006238
Fariborz Jahanianda320092009-01-29 19:24:30 +00006239 if (Entry) {
6240 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006241 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006242 Entry->setInitializer(Init);
6243 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006244 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006245 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6246 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6247 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006248 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006249 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006250 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006251
6252 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006253 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006254 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006255 CGM.AddUsedGlobal(Entry);
6256
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006257 // Use this protocol meta-data to build protocol list table in section
6258 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006259 llvm::GlobalVariable *PTGV =
6260 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6261 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6262 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006263 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006264 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006265 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006266 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006267 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006268 return Entry;
6269}
6270
6271/// EmitProtocolList - Generate protocol list meta-data:
6272/// @code
6273/// struct _protocol_list_t {
6274/// long protocol_count; // Note, this is 32/64 bit
6275/// struct _protocol_t[protocol_count];
6276/// }
6277/// @endcode
6278///
6279llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006280CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006281 ObjCProtocolDecl::protocol_iterator begin,
6282 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00006283 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006284
Fariborz Jahanianda320092009-01-29 19:24:30 +00006285 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006286 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006287 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006288
Daniel Dunbar948e2582009-02-15 07:36:20 +00006289 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006290 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006291 Name.toVector(TmpName);
6292 llvm::GlobalVariable *GV =
6293 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006294 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006295 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006296
Daniel Dunbar948e2582009-02-15 07:36:20 +00006297 for (; begin != end; ++begin)
6298 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6299
Fariborz Jahanianda320092009-01-29 19:24:30 +00006300 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006301 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006302 ObjCTypes.ProtocolnfABIPtrTy));
6303
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006304 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006305 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006306 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006307 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006308 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6309 ProtocolRefs.size()),
6310 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006311
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006312 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006313 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006314 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006315 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006316 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006317 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006318 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006319 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006320 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006321 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006322}
6323
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006324/// GetMethodDescriptionConstant - This routine build following meta-data:
6325/// struct _objc_method {
6326/// SEL _cmd;
6327/// char *method_type;
6328/// char *_imp;
6329/// }
6330
6331llvm::Constant *
6332CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006333 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006334 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006335 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6336 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006337 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006338 if (!Desc[1])
6339 return 0;
6340
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006341 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006342 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006343 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006344}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006345
6346/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6347/// This code gen. amounts to generating code for:
6348/// @code
6349/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6350/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006351///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006352LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006353 CodeGen::CodeGenFunction &CGF,
6354 QualType ObjectTy,
6355 llvm::Value *BaseValue,
6356 const ObjCIvarDecl *Ivar,
6357 unsigned CVRQualifiers) {
6358 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006359 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6360 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6361 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6362 llvm::MDNode::get(VMContext,
6363 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006364 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006365 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006366}
6367
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006368llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006369 CodeGen::CodeGenFunction &CGF,
6370 const ObjCInterfaceDecl *Interface,
6371 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006372 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006373}
6374
John McCallb1e81442011-05-13 23:16:18 +00006375static void appendSelectorForMessageRefTable(std::string &buffer,
6376 Selector selector) {
6377 if (selector.isUnarySelector()) {
6378 buffer += selector.getNameForSlot(0);
6379 return;
6380 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006381
John McCallb1e81442011-05-13 23:16:18 +00006382 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6383 buffer += selector.getNameForSlot(i);
6384 buffer += '_';
6385 }
6386}
6387
John McCall944c8432011-05-14 03:10:52 +00006388/// Emit a "v-table" message send. We emit a weak hidden-visibility
6389/// struct, initially containing the selector pointer and a pointer to
6390/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6391/// load and call the function pointer, passing the address of the
6392/// struct as the second parameter. The runtime determines whether
6393/// the selector is currently emitted using vtable dispatch; if so, it
6394/// substitutes a stub function which simply tail-calls through the
6395/// appropriate vtable slot, and if not, it substitues a stub function
6396/// which tail-calls objc_msgSend. Both stubs adjust the selector
6397/// argument to correctly point to the selector.
6398RValue
6399CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6400 ReturnValueSlot returnSlot,
6401 QualType resultType,
6402 Selector selector,
6403 llvm::Value *arg0,
6404 QualType arg0Type,
6405 bool isSuper,
6406 const CallArgList &formalArgs,
6407 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006408 // Compute the actual arguments.
6409 CallArgList args;
6410
John McCall944c8432011-05-14 03:10:52 +00006411 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006412 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006413 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6414 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006415
John McCall944c8432011-05-14 03:10:52 +00006416 // Second argument: a pointer to the message ref structure. Leave
6417 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006418 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6419
6420 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6421
John McCallde5d3c72012-02-17 03:33:10 +00006422 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006423
John McCallcba681a2011-05-14 21:12:11 +00006424 NullReturnState nullReturn;
6425
John McCall944c8432011-05-14 03:10:52 +00006426 // Find the function to call and the mangled name for the message
6427 // ref structure. Using a different mangled name wouldn't actually
6428 // be a problem; it would just be a waste.
6429 //
6430 // The runtime currently never uses vtable dispatch for anything
6431 // except normal, non-super message-sends.
6432 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006433 llvm::Constant *fn = 0;
6434 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006435 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006436 if (isSuper) {
6437 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6438 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006439 } else {
John McCallcba681a2011-05-14 21:12:11 +00006440 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006441 fn = ObjCTypes.getMessageSendStretFixupFn();
6442 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006443 }
John McCallb1e81442011-05-13 23:16:18 +00006444 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6445 fn = ObjCTypes.getMessageSendFpretFixupFn();
6446 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006447 } else {
John McCallb1e81442011-05-13 23:16:18 +00006448 if (isSuper) {
6449 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6450 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006451 } else {
John McCallb1e81442011-05-13 23:16:18 +00006452 fn = ObjCTypes.getMessageSendFixupFn();
6453 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006454 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006455 }
John McCallb1e81442011-05-13 23:16:18 +00006456 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6457 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006458
John McCallb1e81442011-05-13 23:16:18 +00006459 // Append the selector name, except use underscores anywhere we
6460 // would have used colons.
6461 appendSelectorForMessageRefTable(messageRefName, selector);
6462
6463 llvm::GlobalVariable *messageRef
6464 = CGM.getModule().getGlobalVariable(messageRefName);
6465 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006466 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006467 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006468 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006469 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6470 init->getType(),
6471 /*constant*/ false,
6472 llvm::GlobalValue::WeakAnyLinkage,
6473 init,
6474 messageRefName);
6475 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6476 messageRef->setAlignment(16);
6477 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6478 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006479
6480 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006481 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006482 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6483 e = method->param_end(); i != e; ++i) {
6484 const ParmVarDecl *ParamDecl = (*i);
6485 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6486 if (!nullReturn.NullBB)
6487 nullReturn.init(CGF, arg0);
6488 requiresnullCheck = true;
6489 break;
6490 }
6491 }
6492
John McCallb1e81442011-05-13 23:16:18 +00006493 llvm::Value *mref =
6494 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6495
John McCall944c8432011-05-14 03:10:52 +00006496 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006497 args[1].RV = RValue::get(mref);
6498
6499 // Load the function to call from the message ref table.
6500 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6501 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6502
John McCallde5d3c72012-02-17 03:33:10 +00006503 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006504
John McCallde5d3c72012-02-17 03:33:10 +00006505 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006506 return nullReturn.complete(CGF, result, resultType, formalArgs,
6507 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006508}
6509
6510/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006511CodeGen::RValue
6512CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006513 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006514 QualType ResultType,
6515 Selector Sel,
6516 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006517 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006518 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006519 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006520 return isVTableDispatchedSelector(Sel)
6521 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006522 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006523 false, CallArgs, Method)
6524 : EmitMessageSend(CGF, Return, ResultType,
6525 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006526 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006527 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006528}
6529
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006530llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006531CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006532 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6533
Daniel Dunbardfff2302009-03-02 05:18:14 +00006534 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006535 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006536 false, llvm::GlobalValue::ExternalLinkage,
6537 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006538 }
6539
6540 return GV;
6541}
6542
John McCallf85e1932011-06-15 23:02:42 +00006543llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6544 IdentifierInfo *II) {
6545 llvm::GlobalVariable *&Entry = ClassReferences[II];
6546
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006547 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006548 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006549 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006550 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006551 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6552 false, llvm::GlobalValue::InternalLinkage,
6553 ClassGV,
6554 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006555 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006556 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006557 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006558 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006559 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006560 }
John McCallf85e1932011-06-15 23:02:42 +00006561
Benjamin Kramer578faa82011-09-27 21:06:10 +00006562 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006563}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006564
John McCallf85e1932011-06-15 23:02:42 +00006565llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6566 const ObjCInterfaceDecl *ID) {
6567 return EmitClassRefFromId(Builder, ID->getIdentifier());
6568}
6569
6570llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6571 CGBuilderTy &Builder) {
6572 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6573 return EmitClassRefFromId(Builder, II);
6574}
6575
Daniel Dunbar11394522009-04-18 08:51:00 +00006576llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006577CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006578 const ObjCInterfaceDecl *ID) {
6579 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006580
Daniel Dunbar11394522009-04-18 08:51:00 +00006581 if (!Entry) {
6582 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6583 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006584 Entry =
6585 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006586 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006587 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006588 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006589 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006590 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006591 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006592 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006593 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006594 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006595
Benjamin Kramer578faa82011-09-27 21:06:10 +00006596 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006597}
6598
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006599/// EmitMetaClassRef - Return a Value * of the address of _class_t
6600/// meta-data
6601///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006602llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6603 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006604 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6605 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006606 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006607
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006608 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006609 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006610 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006611 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006612 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006613 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006614 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006615 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006616 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006617 ObjCTypes.ClassnfABIPtrTy));
6618
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006619 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006620 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006621
Benjamin Kramer578faa82011-09-27 21:06:10 +00006622 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006623}
6624
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006625/// GetClass - Return a reference to the class for the given interface
6626/// decl.
6627llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6628 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006629 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006630 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6631 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6632 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6633 }
6634
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006635 return EmitClassRef(Builder, ID);
6636}
6637
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006638/// Generates a message send where the super is the receiver. This is
6639/// a message send to self with special delivery semantics indicating
6640/// which class's method should be called.
6641CodeGen::RValue
6642CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006643 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006644 QualType ResultType,
6645 Selector Sel,
6646 const ObjCInterfaceDecl *Class,
6647 bool isCategoryImpl,
6648 llvm::Value *Receiver,
6649 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006650 const CodeGen::CallArgList &CallArgs,
6651 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006652 // ...
6653 // Create and init a super structure; this is a (receiver, class)
6654 // pair we will pass to objc_msgSendSuper.
6655 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006656 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006657
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006658 llvm::Value *ReceiverAsObject =
6659 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6660 CGF.Builder.CreateStore(ReceiverAsObject,
6661 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006662
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006663 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006664 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006665 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006666 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006667 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006668 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006669
Mike Stumpf5408fe2009-05-16 07:57:57 +00006670 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6671 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006672 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006673 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6674 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6675 CGF.Builder.CreateStore(Target,
6676 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006677
John McCall944c8432011-05-14 03:10:52 +00006678 return (isVTableDispatchedSelector(Sel))
6679 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006680 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006681 true, CallArgs, Method)
6682 : EmitMessageSend(CGF, Return, ResultType,
6683 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006684 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006685 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006686}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006687
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006688llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006689 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006690 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006691
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006692 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006693 llvm::Constant *Casted =
6694 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6695 ObjCTypes.SelectorPtrTy);
6696 Entry =
6697 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6698 llvm::GlobalValue::InternalLinkage,
6699 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006700 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006701 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006702 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006703
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006704 if (lval)
6705 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006706 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6707
6708 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6709 llvm::MDNode::get(VMContext,
6710 ArrayRef<llvm::Value*>()));
6711 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006712}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006713/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006714/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006715///
6716void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006717 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006718 llvm::Value *dst,
6719 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006720 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006721 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006722 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006723 assert(Size <= 8 && "does not support size > 8");
6724 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6725 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006726 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6727 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006728 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6729 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006730 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6731 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006732 return;
6733}
6734
6735/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6736/// objc_assign_strongCast (id src, id *dst)
6737///
6738void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006739 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006740 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006741 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006742 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006743 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006744 assert(Size <= 8 && "does not support size > 8");
6745 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006746 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006747 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6748 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006749 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6750 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006751 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006752 src, dst, "weakassign");
6753 return;
6754}
6755
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006756void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006757 CodeGen::CodeGenFunction &CGF,
6758 llvm::Value *DestPtr,
6759 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006760 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006761 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6762 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006763 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006764 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006765 return;
6766}
6767
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006768/// EmitObjCWeakRead - Code gen for loading value of a __weak
6769/// object: objc_read_weak (id *src)
6770///
6771llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006772 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006773 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006774 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006775 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6776 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006777 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006778 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006779 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006780 return read_weak;
6781}
6782
6783/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6784/// objc_assign_weak (id src, id *dst)
6785///
6786void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006787 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006788 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006789 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006790 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006791 assert(Size <= 8 && "does not support size > 8");
6792 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6793 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006794 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6795 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006796 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6797 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006798 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006799 src, dst, "weakassign");
6800 return;
6801}
6802
6803/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6804/// objc_assign_global (id src, id *dst)
6805///
6806void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006807 llvm::Value *src, llvm::Value *dst,
6808 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006809 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006810 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006811 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006812 assert(Size <= 8 && "does not support size > 8");
6813 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6814 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006815 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6816 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006817 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6818 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006819 if (!threadlocal)
6820 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6821 src, dst, "globalassign");
6822 else
6823 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6824 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006825 return;
6826}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006827
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006828void
John McCallf1549f62010-07-06 01:34:17 +00006829CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6830 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006831 EmitAtSynchronizedStmt(CGF, S,
6832 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6833 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006834}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006835
John McCall5a180392010-07-24 00:37:23 +00006836llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006837CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006838 // There's a particular fixed type info for 'id'.
6839 if (T->isObjCIdType() ||
6840 T->isObjCQualifiedIdType()) {
6841 llvm::Constant *IDEHType =
6842 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6843 if (!IDEHType)
6844 IDEHType =
6845 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6846 false,
6847 llvm::GlobalValue::ExternalLinkage,
6848 0, "OBJC_EHTYPE_id");
6849 return IDEHType;
6850 }
6851
6852 // All other types should be Objective-C interface pointer types.
6853 const ObjCObjectPointerType *PT =
6854 T->getAs<ObjCObjectPointerType>();
6855 assert(PT && "Invalid @catch type.");
6856 const ObjCInterfaceType *IT = PT->getInterfaceType();
6857 assert(IT && "Invalid @catch type.");
6858 return GetInterfaceEHType(IT->getDecl(), false);
6859}
6860
John McCallf1549f62010-07-06 01:34:17 +00006861void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6862 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006863 EmitTryCatchStmt(CGF, S,
6864 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6865 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6866 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006867}
6868
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006869/// EmitThrowStmt - Generate code for a throw statement.
6870void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6871 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006872 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006873 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006874 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006875 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006876 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006877 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006878 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006879 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006880 }
6881
John McCall7ec404c2010-10-16 08:21:07 +00006882 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006883 CGF.Builder.ClearInsertionPoint();
6884}
Daniel Dunbare588b992009-03-01 04:46:24 +00006885
John McCall5a180392010-07-24 00:37:23 +00006886llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006887CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006888 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006889 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006890
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006891 // If we don't need a definition, return the entry if found or check
6892 // if we use an external reference.
6893 if (!ForDefinition) {
6894 if (Entry)
6895 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006896
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006897 // If this type (or a super class) has the __objc_exception__
6898 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006899 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006900 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006901 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006902 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006903 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006904 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006905 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006906 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006907
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006908 // Otherwise we need to either make a new entry or fill in the
6909 // initializer.
6910 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006911 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006912 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006913 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006914 CGM.getModule().getGlobalVariable(VTableName);
6915 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006916 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6917 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006918 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006919 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006920
Chris Lattner8b418682012-02-07 00:39:47 +00006921 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006922
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006923 llvm::Constant *Values[] = {
6924 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6925 GetClassName(ID->getIdentifier()),
6926 GetClassGlobal(ClassName)
6927 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006928 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006929 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006930
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006931 if (Entry) {
6932 Entry->setInitializer(Init);
6933 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006934 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006935 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006936 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006937 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006938 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006939 }
6940
David Blaikie4e4d0842012-03-11 07:00:24 +00006941 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006942 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006943 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006944 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006945
6946 if (ForDefinition) {
6947 Entry->setSection("__DATA,__objc_const");
6948 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6949 } else {
6950 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6951 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006952
6953 return Entry;
6954}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006955
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006956/* *** */
6957
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006958CodeGen::CGObjCRuntime *
6959CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006960 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6961 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006962 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006963
6964 case ObjCRuntime::MacOSX:
6965 case ObjCRuntime::iOS:
6966 return new CGObjCNonFragileABIMac(CGM);
6967
David Chisnall11d3f4c2012-07-03 20:49:52 +00006968 case ObjCRuntime::GNUstep:
6969 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006970 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006971 llvm_unreachable("these runtimes are not Mac runtimes");
6972 }
6973 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006974}