blob: 514f550ad49efc94e3e1ae171cea1c16d3c9a357 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Micah Villmow25a6a842012-10-08 16:25:52 +000039#include "llvm/DataLayout.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046
Daniel Dunbar6bff2512009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000049
Fariborz Jahanianee0af742009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000053
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000054private:
John McCall0774cb82011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCallf85e1932011-06-15 23:02:42 +000068 "objc_msgSend",
Bill Wendling50e6b182012-10-15 04:47:45 +000069 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +000070 llvm::Attributes::NonLazyBind));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000071 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000072
John McCall0774cb82011-05-15 01:53:33 +000073 /// void objc_msgSend_stret (id, SEL, ...)
74 ///
75 /// The messenger used when the return value is an aggregate returned
76 /// by indirect reference in the first argument, and therefore the
77 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000078 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000079 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000080 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
81 params, true),
82 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000083
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000084 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
John McCall0774cb82011-05-15 01:53:33 +000086 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
87 ///
88 /// The messenger used when the return value is returned on the x87
89 /// floating-point stack; without a special entrypoint, the nil case
90 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000091 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000092 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000093 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
94 params, true),
John McCall0774cb82011-05-15 01:53:33 +000095 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000096
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000097 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000098
Anders Carlssoneea64802011-10-31 16:27:11 +000099 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
100 ///
101 /// The messenger used when the return value is returned in two values on the
102 /// x87 floating point stack; without a special entrypoint, the nil case
103 /// would be unbalanced. Only used on 64-bit X86.
104 llvm::Constant *getMessageSendFp2retFn() const {
105 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
106 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
107 llvm::Type *resultType =
108 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
109
110 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
111 params, true),
112 "objc_msgSend_fp2ret");
113 }
114
John McCall0774cb82011-05-15 01:53:33 +0000115 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
116 ///
117 /// The messenger used for super calls, which have different dispatch
118 /// semantics. The class passed is the superclass of the current
119 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000120 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000121 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000122 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000123 params, true),
124 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000126
John McCall0774cb82011-05-15 01:53:33 +0000127 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
128 ///
129 /// A slightly different messenger used for super calls. The class
130 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000131 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000132 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000133 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000134 params, true),
135 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000137
John McCall0774cb82011-05-15 01:53:33 +0000138 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
139 /// SEL op, ...)
140 ///
141 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000142 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000143 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000144 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000145 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000146 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000147 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000148
John McCall0774cb82011-05-15 01:53:33 +0000149 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
150 /// SEL op, ...)
151 ///
152 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000153 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000154 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000155 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000156 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000157 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000158 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160 llvm::Constant *getMessageSendSuperFpretFn() const {
161 // There is no objc_msgSendSuper_fpret? How can that work?
162 return getMessageSendSuperFn();
163 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000164
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000165 llvm::Constant *getMessageSendSuperFpretFn2() const {
166 // There is no objc_msgSendSuper_fpret? How can that work?
167 return getMessageSendSuperFn2();
168 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000170protected:
171 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000173public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000174 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000175 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000176
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000177 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000178 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000180 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000181 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000183 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000185
186private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000187 /// ProtocolPtrTy - LLVM type for external protocol handles
188 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000189 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000190
191public:
192 llvm::Type *getExternalProtocolPtrTy() {
193 if (!ExternalProtocolPtrTy) {
194 // FIXME: It would be nice to unify this with the opaque type, so that the
195 // IR comes out a bit cleaner.
196 CodeGen::CodeGenTypes &Types = CGM.getTypes();
197 ASTContext &Ctx = CGM.getContext();
198 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
199 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
200 }
201
202 return ExternalProtocolPtrTy;
203 }
204
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000205 // SuperCTy - clang type for struct objc_super.
206 QualType SuperCTy;
207 // SuperPtrCTy - clang type for struct objc_super *.
208 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000209
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000210 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000211 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000212 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
216 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000217 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000219 /// PropertyListTy - LLVM type for struct objc_property_list
220 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000221 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000222 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000224
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000225 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000226 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000227
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000228 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000229 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000230 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000232
Chris Lattner72db6c32009-04-22 02:44:54 +0000233 llvm::Constant *getGetPropertyFn() {
234 CodeGen::CodeGenTypes &Types = CGM.getTypes();
235 ASTContext &Ctx = CGM.getContext();
236 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000238 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
239 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000240 Params.push_back(IdType);
241 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000242 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000243 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000244 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000245 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
246 FunctionType::ExtInfo(),
247 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
249 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000250
Chris Lattner72db6c32009-04-22 02:44:54 +0000251 llvm::Constant *getSetPropertyFn() {
252 CodeGen::CodeGenTypes &Types = CGM.getTypes();
253 ASTContext &Ctx = CGM.getContext();
254 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000256 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
257 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000258 Params.push_back(IdType);
259 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000260 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000261 Params.push_back(IdType);
262 Params.push_back(Ctx.BoolTy);
263 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000264 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000265 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
266 FunctionType::ExtInfo(),
267 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000268 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
269 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000270
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000271 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_setProperty_atomic(id self, SEL _cmd,
275 // id newValue, ptrdiff_t offset);
276 // void objc_setProperty_nonatomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282
283 SmallVector<CanQualType,4> Params;
284 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
285 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
286 Params.push_back(IdType);
287 Params.push_back(SelType);
288 Params.push_back(IdType);
289 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
290 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000291 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
292 FunctionType::ExtInfo(),
293 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000294 const char *name;
295 if (atomic && copy)
296 name = "objc_setProperty_atomic_copy";
297 else if (atomic && !copy)
298 name = "objc_setProperty_atomic";
299 else if (!atomic && copy)
300 name = "objc_setProperty_nonatomic_copy";
301 else
302 name = "objc_setProperty_nonatomic";
303
304 return CGM.CreateRuntimeFunction(FTy, name);
305 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000306
307 llvm::Constant *getCopyStructFn() {
308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
310 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000317 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000318 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
319 FunctionType::ExtInfo(),
320 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
322 }
323
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000324 /// This routine declares and returns address of:
325 /// void objc_copyCppObjectAtomic(
326 /// void *dest, const void *src,
327 /// void (*copyHelper) (void *dest, const void *source));
328 llvm::Constant *getCppAtomicObjectFunction() {
329 CodeGen::CodeGenTypes &Types = CGM.getTypes();
330 ASTContext &Ctx = CGM.getContext();
331 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
332 SmallVector<CanQualType,3> Params;
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000337 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
338 FunctionType::ExtInfo(),
339 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
341 }
342
Chris Lattner72db6c32009-04-22 02:44:54 +0000343 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000344 CodeGen::CodeGenTypes &Types = CGM.getTypes();
345 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000346 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000348 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000349 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000350 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000351 FunctionType::ExtInfo(),
352 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
354 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000355
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000356 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000357 llvm::Constant *getGcReadWeakFn() {
358 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000361 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000363 }
364
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000365 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000366 llvm::Constant *getGcAssignWeakFn() {
367 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000368 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000369 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000370 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
372 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000373
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000374 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignGlobalFn() {
376 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
381 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000382
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000383 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
384 llvm::Constant *getGcAssignThreadLocalFn() {
385 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000387 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000388 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
390 }
391
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000392 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000393 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000394 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000395 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
396 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000397 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000398 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000399 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000402 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
403 llvm::Constant *GcMemmoveCollectableFn() {
404 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000405 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000406 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000407 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000410 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000411 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000412 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000413 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000414 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000415 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
417 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000418
419 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000420 llvm::Constant *getExceptionThrowFn() {
421 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000422 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000424 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
426 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000427
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000428 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
429 llvm::Constant *getExceptionRethrowFn() {
430 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000431 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
433 }
434
Daniel Dunbar1c566672009-02-24 01:43:46 +0000435 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000436 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000437 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000438 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000439 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000440 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
442 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000443
Daniel Dunbar1c566672009-02-24 01:43:46 +0000444 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000446 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000447 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000448 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000449 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
451 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000452
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000453 llvm::Constant *getSendFn(bool IsSuper) const {
454 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
455 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000456
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000457 llvm::Constant *getSendFn2(bool IsSuper) const {
458 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000461 llvm::Constant *getSendStretFn(bool IsSuper) const {
462 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
463 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000464
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000465 llvm::Constant *getSendStretFn2(bool IsSuper) const {
466 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
467 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000468
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000469 llvm::Constant *getSendFpretFn(bool IsSuper) const {
470 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000473 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
474 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
475 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000476
Anders Carlssoneea64802011-10-31 16:27:11 +0000477 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
478 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
479 }
480
481 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
482 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
483 }
484
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000485 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
486 ~ObjCCommonTypesHelper(){}
487};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000488
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000489/// ObjCTypesHelper - Helper class that encapsulates lazy
490/// construction of varies types used during ObjC generation.
491class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000492public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000493 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000495 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000497 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000499
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000500 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000503 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504 /// ProtocolExtensionTy - LLVM type for struct
505 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 /// ProtocolExtensionTy - LLVM type for struct
508 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000510 /// MethodDescriptionTy - LLVM type for struct
511 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000512 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000513 /// MethodDescriptionListTy - LLVM type for struct
514 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000515 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// MethodDescriptionListPtrTy - LLVM type for struct
517 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000518 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000523 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000525 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000543
Anders Carlsson124526b2008-09-09 10:10:21 +0000544 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000545 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000546
Anders Carlsson124526b2008-09-09 10:10:21 +0000547 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000548 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000549 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000550 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000551 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000552 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000553 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000554
555 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000556 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000557 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000558 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000559 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000561 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000562
563 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000564 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000566 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000567 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000568 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Anders Carlsson124526b2008-09-09 10:10:21 +0000571 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000572 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000573 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000575 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 "objc_exception_match");
577
Chris Lattner34b02a12009-04-22 02:26:14 +0000578 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000579
Anders Carlsson124526b2008-09-09 10:10:21 +0000580 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000581 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000582 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000584 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
585 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000586 "_setjmp",
Bill Wendling50e6b182012-10-15 04:47:45 +0000587 llvm::Attributes::get(CGM.getLLVMContext(),
Bill Wendlinga6375562012-10-16 05:23:44 +0000588 llvm::Attributes::NonLazyBind));
Chris Lattner34b02a12009-04-22 02:26:14 +0000589 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000591public:
592 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000593 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000594};
595
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000596/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000599public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000601 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000604 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000605 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000606
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000607 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000608 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Daniel Dunbar948e2582009-02-15 07:36:20 +0000610 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000611 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000612
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000613 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000614 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000615
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000616 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000617 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000619 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000620 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000621
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000622 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000623 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000625 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000626 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000628 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000629 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000632 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000635 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000638 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000640 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000641 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000643 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000645 // MessageRefTy - LLVM for:
646 // struct _message_ref_t {
647 // IMP messenger;
648 // SEL name;
649 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000650 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000651 // MessageRefCTy - clang type for struct _message_ref_t
652 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000653
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000654 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000655 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000656 // MessageRefCPtrTy - clang type for struct _message_ref_t*
657 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000658
Fariborz Jahanianef163782009-02-05 01:13:09 +0000659 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000660 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000661
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000662 // SuperMessageRefTy - LLVM for:
663 // struct _super_message_ref_t {
664 // SUPER_IMP messenger;
665 // SEL name;
666 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000667 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000669 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000670 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000671
Chris Lattner1c02f862009-04-22 02:53:24 +0000672 llvm::Constant *getMessageSendFixupFn() {
673 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000674 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000675 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000676 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000677 "objc_msgSend_fixup");
678 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000679
Chris Lattner1c02f862009-04-22 02:53:24 +0000680 llvm::Constant *getMessageSendFpretFixupFn() {
681 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000682 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000684 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000685 "objc_msgSend_fpret_fixup");
686 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000687
Chris Lattner1c02f862009-04-22 02:53:24 +0000688 llvm::Constant *getMessageSendStretFixupFn() {
689 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000690 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000691 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000692 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000693 "objc_msgSend_stret_fixup");
694 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695
Chris Lattner1c02f862009-04-22 02:53:24 +0000696 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000699 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000700 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000701 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 "objc_msgSendSuper2_fixup");
703 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Chris Lattner1c02f862009-04-22 02:53:24 +0000705 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000708 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000710 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000711 "objc_msgSendSuper2_stret_fixup");
712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000713
Chris Lattner8a569112009-04-22 02:15:23 +0000714 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000715 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000716 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner8a569112009-04-22 02:15:23 +0000720 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000721 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000722 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000723 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000724 "objc_begin_catch");
725 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000726
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000727 llvm::StructType *EHTypeTy;
728 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000729
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000730 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
731 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000732};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000733
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000735public:
736 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000737 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000738 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000739 unsigned ivar_bytepos;
740 unsigned ivar_size;
741 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000742 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000743
744 // Allow sorting based on byte pos.
745 bool operator<(const GC_IVAR &b) const {
746 return ivar_bytepos < b.ivar_bytepos;
747 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000748 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000749
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000750 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000751 public:
752 unsigned skip;
753 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000754 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000755 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000756 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000757
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000758 /// opcode for captured block variables layout 'instructions'.
759 /// In the following descriptions, 'I' is the value of the immediate field.
760 /// (field following the opcode).
761 ///
762 enum BLOCK_LAYOUT_OPCODE {
763 /// An operator which affects how the following layout should be
764 /// interpreted.
765 /// I == 0: Halt interpretation and treat everything else as
766 /// a non-pointer. Note that this instruction is equal
767 /// to '\0'.
768 /// I != 0: Currently unused.
769 BLOCK_LAYOUT_OPERATOR = 0,
770
771 /// The next I+1 bytes do not contain a value of object pointer type.
772 /// Note that this can leave the stream unaligned, meaning that
773 /// subsequent word-size instructions do not begin at a multiple of
774 /// the pointer size.
775 BLOCK_LAYOUT_NON_OBJECT_BYTES = 1,
776
777 /// The next I+1 words do not contain a value of object pointer type.
778 /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for
779 /// when the required skip quantity is a multiple of the pointer size.
780 BLOCK_LAYOUT_NON_OBJECT_WORDS = 2,
781
782 /// The next I+1 words are __strong pointers to Objective-C
783 /// objects or blocks.
784 BLOCK_LAYOUT_STRONG = 3,
785
786 /// The next I+1 words are pointers to __block variables.
787 BLOCK_LAYOUT_BYREF = 4,
788
789 /// The next I+1 words are __weak pointers to Objective-C
790 /// objects or blocks.
791 BLOCK_LAYOUT_WEAK = 5,
792
793 /// The next I+1 words are __unsafe_unretained pointers to
794 /// Objective-C objects or blocks.
795 BLOCK_LAYOUT_UNRETAINED = 6
796
797 /// The next I+1 words are block or object pointers with some
798 /// as-yet-unspecified ownership semantics. If we add more
799 /// flavors of ownership semantics, values will be taken from
800 /// this range.
801 ///
802 /// This is included so that older tools can at least continue
803 /// processing the layout past such things.
804 //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10,
805
806 /// All other opcodes are reserved. Halt interpretation and
807 /// treat everything else as opaque.
808 };
809
810 class RUN_SKIP {
811 public:
812 enum BLOCK_LAYOUT_OPCODE opcode;
813 unsigned block_var_bytepos;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000814 unsigned block_var_size;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000815 RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR,
Fariborz Jahanianc46b4352012-10-27 21:10:38 +0000816 unsigned BytePos = 0, unsigned Size = 0)
817 : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {}
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000818
819 // Allow sorting based on byte pos.
820 bool operator<(const RUN_SKIP &b) const {
821 return block_var_bytepos < b.block_var_bytepos;
822 }
823 };
824
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000825protected:
Owen Anderson69243822009-07-13 04:10:07 +0000826 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000827 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000828 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000829
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000830 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000831 SmallVector<GC_IVAR, 16> SkipIvars;
832 SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniane11dba82012-10-25 21:15:04 +0000833
834 // arc/mrr layout of captured block literal variables.
835 SmallVector<RUN_SKIP, 16> RunSkipBlockVars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000836
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000837 /// LazySymbols - Symbols to generate a lazy reference for. See
838 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000839 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000841 /// DefinedSymbols - External symbols which are defined by this
842 /// module. The symbols in this list and LazySymbols are used to add
843 /// special linker symbols which ensure that Objective-C modules are
844 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000845 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000847 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000848 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000849
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000850 /// MethodVarNames - uniqued method variable names.
851 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000853 /// DefinedCategoryNames - list of category names in form Class_Category.
854 llvm::SetVector<std::string> DefinedCategoryNames;
855
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000856 /// MethodVarTypes - uniqued method type signatures. We have to use
857 /// a StringMap here because have no other unique reference.
858 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000859
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000860 /// MethodDefinitions - map of methods which have been defined in
861 /// this translation unit.
862 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000863
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000864 /// PropertyNames - uniqued method variable names.
865 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000867 /// ClassReferences - uniqued class references.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000869
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000870 /// SelectorReferences - uniqued selector references.
871 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000873 /// Protocols - Protocols for which an objc_protocol structure has
874 /// been emitted. Forward declarations are handled by creating an
875 /// empty structure whose initializer is filled in when/if defined.
876 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000878 /// DefinedProtocols - Protocols which have actually been
879 /// defined. We should not need this, see FIXME in GenerateProtocol.
880 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000882 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000883 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000884
885 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000886 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000888 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000889 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000891 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000892 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000893
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000894 /// GetNameForMethod - Return a name for the given method.
895 /// \param[out] NameOut - The return value.
896 void GetNameForMethod(const ObjCMethodDecl *OMD,
897 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000898 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000899
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000900 /// GetMethodVarName - Return a unique constant for the given
901 /// selector's name. The return value has type char *.
902 llvm::Constant *GetMethodVarName(Selector Sel);
903 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000905 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000906 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000909 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
910 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000911 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000912
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000913 /// GetPropertyName - Return a unique constant for the given
914 /// name. The return value has type char *.
915 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000916
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000917 // FIXME: This can be dropped once string functions are unified.
918 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
919 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000920
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000921 /// GetClassName - Return a unique constant for the given selector's
922 /// name. The return value has type char *.
923 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000924
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000925 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
926
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000927 /// BuildIvarLayout - Builds ivar layout bitmap for the class
928 /// implementation for the __strong or __weak case.
929 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000930 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
931 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000932
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000933 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000934
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000935 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000936 unsigned int BytePos, bool ForStrongLayout,
937 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000938 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000939 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000940 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000941 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000942 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000943 bool &HasUnion);
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000944
Fariborz Jahanian44fcff92012-11-02 22:51:18 +0000945 Qualifiers::ObjCLifetime GetObjCLifeTime(QualType QT);
946
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +0000947 void UpdateRunSkipBlockVars(bool IsByref,
948 Qualifiers::ObjCLifetime LifeTime,
949 unsigned FieldOffset,
950 unsigned FieldSize);
951
952 void BuildRCBlockVarRecordLayout(const RecordType *RT,
953 unsigned int BytePos, bool &HasUnion);
954
955 void BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
956 const RecordDecl *RD,
957 ArrayRef<const FieldDecl*> RecFields,
958 unsigned int BytePos, bool &HasUnion);
959
Fariborz Jahanianf22ae652012-11-01 18:32:55 +0000960 uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout);
961
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000962
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000963 /// GetIvarLayoutName - Returns a unique constant for the given
964 /// ivar layout bitmap.
965 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
966 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000967
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000968 /// EmitPropertyList - Emit the given property list. The return
969 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000970 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000971 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000972 const ObjCContainerDecl *OCD,
973 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000974
Bob Wilsondc8dab62011-11-30 01:57:58 +0000975 /// EmitProtocolMethodTypes - Generate the array of extended method type
976 /// strings. The return value has type Int8PtrPtrTy.
977 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000978 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000979 const ObjCCommonTypesHelper &ObjCTypes);
980
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000981 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000982 void PushProtocolProperties(
983 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
984 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
985 const Decl *Container,
986 const ObjCProtocolDecl *PROTO,
987 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000988
Fariborz Jahanianda320092009-01-29 19:24:30 +0000989 /// GetProtocolRef - Return a reference to the internal protocol
990 /// description, creating an empty one if it has not been
991 /// defined. The return value has type ProtocolPtrTy.
992 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000993
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000994 /// CreateMetadataVar - Create a global variable with internal
995 /// linkage for use by the Objective-C runtime.
996 ///
997 /// This is a convenience wrapper which not only creates the
998 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000999 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001000 ///
1001 /// \param Name - The variable name.
1002 /// \param Init - The variable initializer; this is also used to
1003 /// define the type of the variable.
1004 /// \param Section - The section the variable should go into, or 0.
1005 /// \param Align - The alignment for the variable, or 0.
1006 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +00001007 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +00001008 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001009 llvm::Constant *Init,
1010 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00001011 unsigned Align,
1012 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00001013
John McCall944c8432011-05-14 03:10:52 +00001014 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1015 ReturnValueSlot Return,
1016 QualType ResultType,
1017 llvm::Value *Sel,
1018 llvm::Value *Arg0,
1019 QualType Arg0Ty,
1020 bool IsSuper,
1021 const CallArgList &CallArgs,
1022 const ObjCMethodDecl *OMD,
1023 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001024
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001025 /// EmitImageInfo - Emit the image info marker used to encode some module
1026 /// level information.
1027 void EmitImageInfo();
1028
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001029public:
Owen Anderson69243822009-07-13 04:10:07 +00001030 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +00001031 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001032
David Chisnall0d13f6f2010-01-23 02:40:42 +00001033 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001034
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001035 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1036 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001037
Fariborz Jahanianda320092009-01-29 19:24:30 +00001038 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001039
Fariborz Jahanianda320092009-01-29 19:24:30 +00001040 /// GetOrEmitProtocol - Get the protocol object for the given
1041 /// declaration, emitting it if necessary. The return value has type
1042 /// ProtocolPtrTy.
1043 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001044
Fariborz Jahanianda320092009-01-29 19:24:30 +00001045 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1046 /// object for the given declaration, emitting it if needed. These
1047 /// forward references will be filled in with empty bodies if no
1048 /// definition is seen. The return value has type ProtocolPtrTy.
1049 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +00001050 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
1051 const CGBlockInfo &blockInfo);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001052 virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM,
1053 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001054
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001055};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001056
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001057class CGObjCMac : public CGObjCCommonMac {
1058private:
1059 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001060
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001061 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001062 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001063 void EmitModuleInfo();
1064
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065 /// EmitModuleSymols - Emit module symbols, the list of defined
1066 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001067 llvm::Constant *EmitModuleSymbols();
1068
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001069 /// FinishModule - Write out global data structures at the end of
1070 /// processing a translation unit.
1071 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001072
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001073 /// EmitClassExtension - Generate the class extension structure used
1074 /// to store the weak ivar layout and properties. The return value
1075 /// has type ClassExtensionPtrTy.
1076 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1077
1078 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1079 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001080 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001081 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001082
John McCallf85e1932011-06-15 23:02:42 +00001083 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1084 IdentifierInfo *II);
1085
1086 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1087
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001088 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1089 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001090
1091 /// EmitIvarList - Emit the ivar list for the given
1092 /// implementation. If ForClass is true the list of class ivars
1093 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1094 /// interface ivars will be emitted. The return value has type
1095 /// IvarListPtrTy.
1096 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001097 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001098
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001099 /// EmitMetaClass - Emit a forward reference to the class structure
1100 /// for the metaclass of the given interface. The return value has
1101 /// type ClassPtrTy.
1102 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1103
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001104 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001105 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001106 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1107 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001108 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001109
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001110 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001111
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001112 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113
1114 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001115 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001116 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001117 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001118 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001119
1120 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001121 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001122 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001123 /// - IsProtocol: True iff these methods are for a protocol.
1124 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001125 /// - Required: When true, only "required" methods are
1126 /// listed. Similarly, when false only "optional" methods are
1127 /// listed. For classes this should always be true.
1128 /// - begin, end: The method list to output.
1129 ///
1130 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001131 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001132 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001133 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001135 /// GetOrEmitProtocol - Get the protocol object for the given
1136 /// declaration, emitting it if necessary. The return value has type
1137 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001138 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001139
1140 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1141 /// object for the given declaration, emitting it if needed. These
1142 /// forward references will be filled in with empty bodies if no
1143 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001144 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001145
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001146 /// EmitProtocolExtension - Generate the protocol extension
1147 /// structure used to store optional instance and class methods, and
1148 /// protocol properties. The return value has type
1149 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001150 llvm::Constant *
1151 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001152 ArrayRef<llvm::Constant*> OptInstanceMethods,
1153 ArrayRef<llvm::Constant*> OptClassMethods,
1154 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001155
1156 /// EmitProtocolList - Generate the list of referenced
1157 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001158 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001159 ObjCProtocolDecl::protocol_iterator begin,
1160 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001161
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001162 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1163 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001164 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1165 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001166
1167public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001168 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001169
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001170 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001171
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001172 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001173 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001174 QualType ResultType,
1175 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001176 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001177 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001178 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001179 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001180
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001182 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001183 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001184 QualType ResultType,
1185 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001186 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001187 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001188 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001189 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001190 const CallArgList &CallArgs,
1191 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001192
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001193 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001194 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001195
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001196 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1197 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001198
1199 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1200 /// untyped one.
1201 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1202 const ObjCMethodDecl *Method);
1203
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001204 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001205
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001206 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001207
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001208 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001209
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001210 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001211
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001212 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001213 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214
Chris Lattner74391b42009-03-22 21:03:39 +00001215 virtual llvm::Constant *GetPropertyGetFunction();
1216 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001217 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1218 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001219 virtual llvm::Constant *GetGetStructFunction();
1220 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001221 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001222 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001223
John McCallf1549f62010-07-06 01:34:17 +00001224 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1225 const ObjCAtTryStmt &S);
1226 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1227 const ObjCAtSynchronizedStmt &S);
1228 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001229 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1230 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001231 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001232 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001233 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001235 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001236 llvm::Value *src, llvm::Value *dest,
1237 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001238 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001239 llvm::Value *src, llvm::Value *dest,
1240 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001241 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1242 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001243 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1244 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001245 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001247 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1248 QualType ObjectTy,
1249 llvm::Value *BaseValue,
1250 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001251 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001252 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001253 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001254 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001255
1256 /// GetClassGlobal - Return the global variable for the Objective-C
1257 /// class of the given name.
1258 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001259 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001260 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001261};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001262
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001263class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001264private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001265 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001266 llvm::GlobalVariable* ObjCEmptyCacheVar;
1267 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268
Daniel Dunbar11394522009-04-18 08:51:00 +00001269 /// SuperClassReferences - uniqued super class references.
1270 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001271
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001272 /// MetaClassReferences - uniqued meta class references.
1273 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001274
1275 /// EHTypeReferences - uniqued class ehtype references.
1276 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
John McCall944c8432011-05-14 03:10:52 +00001278 /// VTableDispatchMethods - List of methods for which we generate
1279 /// vtable-based message dispatch.
1280 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001282 /// DefinedMetaClasses - List of defined meta-classes.
1283 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1284
John McCall944c8432011-05-14 03:10:52 +00001285 /// isVTableDispatchedSelector - Returns true if SEL is a
1286 /// vtable-based selector.
1287 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001288
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001289 /// FinishNonFragileABIModule - Write out global data structures at the end of
1290 /// processing a translation unit.
1291 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001292
Daniel Dunbar463b8762009-05-15 21:48:48 +00001293 /// AddModuleClassList - Add the given list of class pointers to the
1294 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001295 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001296 const char *SymbolName,
1297 const char *SectionName);
1298
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1300 unsigned InstanceStart,
1301 unsigned InstanceSize,
1302 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001303 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001304 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001305 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001306 llvm::Constant *ClassRoGV,
1307 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001309 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001311 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001313 /// EmitMethodList - Emit the method list for the given
1314 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001315 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001316 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001317 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001318 /// EmitIvarList - Emit the ivar list for the given
1319 /// implementation. If ForClass is true the list of class ivars
1320 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1321 /// interface ivars will be emitted. The return value has type
1322 /// IvarListnfABIPtrTy.
1323 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001325 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001326 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001327 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001328
Fariborz Jahanianda320092009-01-29 19:24:30 +00001329 /// GetOrEmitProtocol - Get the protocol object for the given
1330 /// declaration, emitting it if necessary. The return value has type
1331 /// ProtocolPtrTy.
1332 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001333
Fariborz Jahanianda320092009-01-29 19:24:30 +00001334 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1335 /// object for the given declaration, emitting it if needed. These
1336 /// forward references will be filled in with empty bodies if no
1337 /// definition is seen. The return value has type ProtocolPtrTy.
1338 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001339
Fariborz Jahanianda320092009-01-29 19:24:30 +00001340 /// EmitProtocolList - Generate the list of referenced
1341 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001342 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001343 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001344 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001345
John McCall944c8432011-05-14 03:10:52 +00001346 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1347 ReturnValueSlot Return,
1348 QualType ResultType,
1349 Selector Sel,
1350 llvm::Value *Receiver,
1351 QualType Arg0Ty,
1352 bool IsSuper,
1353 const CallArgList &CallArgs,
1354 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001355
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001356 /// GetClassGlobal - Return the global variable for the Objective-C
1357 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001358 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001359
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001360 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001361 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001362 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001363 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001364
1365 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1366 IdentifierInfo *II);
1367
1368 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001369
Daniel Dunbar11394522009-04-18 08:51:00 +00001370 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1371 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001372 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1373 const ObjCInterfaceDecl *ID);
1374
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001375 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1376 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001377 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001378 const ObjCInterfaceDecl *ID);
1379
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001380 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1381 /// the given ivar.
1382 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001383 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001384 const ObjCInterfaceDecl *ID,
1385 const ObjCIvarDecl *Ivar);
1386
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001387 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1388 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001389 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1390 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001391
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001392 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001393 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001394 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001395 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001396
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001397 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001398 return "OBJC_METACLASS_$_";
1399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001400
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001401 const char *getClassSymbolPrefix() const {
1402 return "OBJC_CLASS_$_";
1403 }
1404
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001405 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001406 uint32_t &InstanceStart,
1407 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001408
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001409 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001410 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001411 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1412 return CGM.getContext().Selectors.getSelector(0, &II);
1413 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001414
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001415 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001416 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1417 return CGM.getContext().Selectors.getSelector(1, &II);
1418 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001419
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001420 /// ImplementationIsNonLazy - Check whether the given category or
1421 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001422 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001423
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001424public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001425 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426 // FIXME. All stubs for now!
1427 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001428
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001429 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001430 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001431 QualType ResultType,
1432 Selector Sel,
1433 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001434 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001435 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001436 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001437
1438 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001439 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001440 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001441 QualType ResultType,
1442 Selector Sel,
1443 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001444 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001445 llvm::Value *Receiver,
1446 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001447 const CallArgList &CallArgs,
1448 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001449
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001450 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001451 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001452
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001453 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1454 bool lvalue = false)
1455 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001456
1457 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1458 /// untyped one.
1459 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1460 const ObjCMethodDecl *Method)
1461 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001462
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001463 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001464
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001465 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001466
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001467 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001468
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001469 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001470 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001471
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001472 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001473
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001474 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001475 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001476 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477 virtual llvm::Constant *GetPropertySetFunction() {
1478 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001479 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001480
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001481 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1482 bool copy) {
1483 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1484 }
1485
David Chisnall8fac25d2010-12-26 22:13:16 +00001486 virtual llvm::Constant *GetSetStructFunction() {
1487 return ObjCTypes.getCopyStructFn();
1488 }
1489 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001490 return ObjCTypes.getCopyStructFn();
1491 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001492 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1493 return ObjCTypes.getCppAtomicObjectFunction();
1494 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001495
Chris Lattner74391b42009-03-22 21:03:39 +00001496 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001497 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001498 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001499
John McCallf1549f62010-07-06 01:34:17 +00001500 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1501 const ObjCAtTryStmt &S);
1502 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1503 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001504 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001505 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001506 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001507 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001508 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001509 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001510 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001511 llvm::Value *src, llvm::Value *dest,
1512 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001513 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001514 llvm::Value *src, llvm::Value *dest,
1515 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001516 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001517 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001518 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1519 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001520 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001521 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1522 QualType ObjectTy,
1523 llvm::Value *BaseValue,
1524 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001525 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001526 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001527 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001528 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001529};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001530
1531/// A helper class for performing the null-initialization of a return
1532/// value.
1533struct NullReturnState {
1534 llvm::BasicBlock *NullBB;
1535 llvm::BasicBlock *callBB;
1536 NullReturnState() : NullBB(0), callBB(0) {}
1537
1538 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1539 // Make blocks for the null-init and call edges.
1540 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1541 callBB = CGF.createBasicBlock("msgSend.call");
1542
1543 // Check for a null receiver and, if there is one, jump to the
1544 // null-init test.
1545 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1546 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1547
1548 // Otherwise, start performing the call.
1549 CGF.EmitBlock(callBB);
1550 }
1551
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001552 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1553 const CallArgList &CallArgs,
1554 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001555 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001556
1557 llvm::Value *NullInitPtr = 0;
1558 if (result.isScalar() && !resultType->isVoidType()) {
1559 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1560 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1561 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001562
1563 // Finish the call path.
1564 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1565 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1566
1567 // Emit the null-init block and perform the null-initialization there.
1568 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001569
1570 // Release consumed arguments along the null-receiver path.
1571 if (Method) {
1572 CallArgList::const_iterator I = CallArgs.begin();
1573 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1574 e = Method->param_end(); i != e; ++i, ++I) {
1575 const ParmVarDecl *ParamDecl = (*i);
1576 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1577 RValue RV = I->RV;
1578 assert(RV.isScalar() &&
1579 "NullReturnState::complete - arg not on object");
1580 CGF.EmitARCRelease(RV.getScalarVal(), true);
1581 }
1582 }
1583 }
1584
1585 if (result.isScalar()) {
1586 if (NullInitPtr)
1587 CGF.EmitNullInitialization(NullInitPtr, resultType);
1588 // Jump to the continuation block.
1589 CGF.EmitBlock(contBB);
1590 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1591 : result;
1592 }
1593
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001594 if (!resultType->isAnyComplexType()) {
1595 assert(result.isAggregate() && "null init of non-aggregate result?");
1596 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1597 // Jump to the continuation block.
1598 CGF.EmitBlock(contBB);
1599 return result;
1600 }
1601
1602 // _Complex type
1603 // FIXME. Now easy to handle any other scalar type whose result is returned
1604 // in memory due to ABI limitations.
1605 CGF.EmitBlock(contBB);
1606 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1607 llvm::Type *MemberType = CallCV.first->getType();
1608 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1609 // Create phi instruction for scalar complex value.
1610 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1611 PHIReal->addIncoming(ZeroCV, NullBB);
1612 PHIReal->addIncoming(CallCV.first, callBB);
1613 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1614 PHIImag->addIncoming(ZeroCV, NullBB);
1615 PHIImag->addIncoming(CallCV.second, callBB);
1616 return RValue::getComplex(PHIReal, PHIImag);
1617 }
1618};
1619
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001620} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001621
1622/* *** Helper Functions *** */
1623
1624/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001625static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001626 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001627 unsigned idx0,
1628 unsigned idx1) {
1629 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001630 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1631 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001632 };
Jay Foada5c04342011-07-21 14:31:17 +00001633 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001634}
1635
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001636/// hasObjCExceptionAttribute - Return true if this class or any super
1637/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001638static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001639 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001640 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001641 return true;
1642 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001643 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001644 return false;
1645}
1646
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001647/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001648
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001649CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001650 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001651 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001652 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001653}
1654
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001655/// GetClass - Return a reference to the class for the given interface
1656/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001657llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001658 const ObjCInterfaceDecl *ID) {
1659 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001660}
1661
1662/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001663llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1664 bool lval) {
1665 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001666}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001667llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001668 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001669 return EmitSelector(Builder, Method->getSelector());
1670}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001671
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001672llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001673 if (T->isObjCIdType() ||
1674 T->isObjCQualifiedIdType()) {
1675 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001676 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001677 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001678 if (T->isObjCClassType() ||
1679 T->isObjCQualifiedClassType()) {
1680 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001681 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001682 }
1683 if (T->isObjCObjectPointerType())
1684 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1685
John McCall5a180392010-07-24 00:37:23 +00001686 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001687}
1688
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001689/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001690/*
1691 struct __builtin_CFString {
1692 const int *isa; // point to __CFConstantStringClassReference
1693 int flags;
1694 const char *str;
1695 long length;
1696 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001697*/
1698
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001699/// or Generate a constant NSString object.
1700/*
1701 struct __builtin_NSString {
1702 const int *isa; // point to __NSConstantStringClassReference
1703 const char *str;
1704 unsigned int length;
1705 };
1706*/
1707
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001708llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001709 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001710 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001711 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001712 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001713}
1714
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001715enum {
1716 kCFTaggedObjectID_Integer = (1 << 1) + 1
1717};
1718
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001719/// Generates a message send where the super is the receiver. This is
1720/// a message send to self with special delivery semantics indicating
1721/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001722CodeGen::RValue
1723CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001724 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001725 QualType ResultType,
1726 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001727 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001728 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001729 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001730 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001731 const CodeGen::CallArgList &CallArgs,
1732 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001733 // Create and init a super structure; this is a (receiver, class)
1734 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001735 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001736 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001737 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001738 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001740 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001741
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001742 // If this is a class message the metaclass is passed as the target.
1743 llvm::Value *Target;
1744 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001745 if (isCategoryImpl) {
1746 // Message sent to 'super' in a class method defined in a category
1747 // implementation requires an odd treatment.
1748 // If we are in a class method, we must retrieve the
1749 // _metaclass_ for the current class, pointed at by
1750 // the class's "isa" pointer. The following assumes that
1751 // isa" is the first ivar in a class (which it must be).
1752 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1753 Target = CGF.Builder.CreateStructGEP(Target, 0);
1754 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001755 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001756 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1757 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1758 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1759 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001760 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001761 }
1762 else if (isCategoryImpl)
1763 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1764 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001765 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1766 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1767 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001768 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001769 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1770 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001771 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001772 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001773 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001774 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001775 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001776 return EmitMessageSend(CGF, Return, ResultType,
1777 EmitSelector(CGF.Builder, Sel),
1778 ObjCSuper, ObjCTypes.SuperPtrCTy,
1779 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001780}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781
1782/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001783CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001784 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001785 QualType ResultType,
1786 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001787 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001788 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001789 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001790 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001791 return EmitMessageSend(CGF, Return, ResultType,
1792 EmitSelector(CGF.Builder, Sel),
1793 Receiver, CGF.getContext().getObjCIdType(),
1794 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001795}
1796
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001797CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001798CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1799 ReturnValueSlot Return,
1800 QualType ResultType,
1801 llvm::Value *Sel,
1802 llvm::Value *Arg0,
1803 QualType Arg0Ty,
1804 bool IsSuper,
1805 const CallArgList &CallArgs,
1806 const ObjCMethodDecl *Method,
1807 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001808 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001809 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001810 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001811 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1812 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001813 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814
John McCallde5d3c72012-02-17 03:33:10 +00001815 // If we're calling a method, use the formal signature.
1816 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001817
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001818 if (Method)
1819 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1820 CGM.getContext().getCanonicalType(ResultType) &&
1821 "Result type mismatch!");
1822
John McCallcba681a2011-05-14 21:12:11 +00001823 NullReturnState nullReturn;
1824
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001825 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001826 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001827 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001828 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001829 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001830 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1831 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1832 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001833 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1834 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1835 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001836 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001837 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001839 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001840
1841 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001842 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001843 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1844 e = Method->param_end(); i != e; ++i) {
1845 const ParmVarDecl *ParamDecl = (*i);
1846 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1847 if (!nullReturn.NullBB)
1848 nullReturn.init(CGF, Arg0);
1849 requiresnullCheck = true;
1850 break;
1851 }
1852 }
1853
John McCallde5d3c72012-02-17 03:33:10 +00001854 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1855 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001856 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1857 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001858}
1859
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001860static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1861 if (FQT.isObjCGCStrong())
1862 return Qualifiers::Strong;
1863
John McCallf85e1932011-06-15 23:02:42 +00001864 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001865 return Qualifiers::Weak;
1866
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001867 // check for __unsafe_unretained
1868 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1869 return Qualifiers::GCNone;
1870
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001871 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1872 return Qualifiers::Strong;
1873
1874 if (const PointerType *PT = FQT->getAs<PointerType>())
1875 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1876
1877 return Qualifiers::GCNone;
1878}
1879
John McCall6b5a61b2011-02-07 10:33:21 +00001880llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1881 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00001882
Chris Lattner8b418682012-02-07 00:39:47 +00001883 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
David Blaikie4e4d0842012-03-11 07:00:24 +00001884 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1885 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001886 return nullPtr;
1887
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001888 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001889 SkipIvars.clear();
1890 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001891 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1892 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001893
Fariborz Jahanian81979822010-09-09 00:21:45 +00001894 // __isa is the first field in block descriptor and must assume by runtime's
1895 // convention that it is GC'able.
1896 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001897
1898 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1899
1900 // Calculate the basic layout of the block structure.
1901 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001902 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001903
1904 // Ignore the optional 'this' capture: C++ objects are not assumed
1905 // to be GC'ed.
1906
1907 // Walk the captured variables.
1908 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1909 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1910 const VarDecl *variable = ci->getVariable();
1911 QualType type = variable->getType();
1912
1913 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1914
1915 // Ignore constant captures.
1916 if (capture.isConstant()) continue;
1917
1918 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1919
1920 // __block variables are passed by their descriptor address.
1921 if (ci->isByRef()) {
1922 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001923 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001924 }
1925
1926 assert(!type->isArrayType() && "array variable should not be caught");
1927 if (const RecordType *record = type->getAs<RecordType>()) {
1928 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001929 continue;
1930 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001931
John McCall6b5a61b2011-02-07 10:33:21 +00001932 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1933 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1934
1935 if (GCAttr == Qualifiers::Strong)
1936 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1937 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001938 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001939 SkipIvars.push_back(GC_IVAR(fieldOffset,
1940 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001941 }
1942
1943 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001944 return nullPtr;
1945
1946 // Sort on byte position; captures might not be allocated in order,
1947 // and unions can do funny things.
1948 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1949 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001950
1951 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001952 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001954 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001955 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001956 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001957 if (!(s[i] & 0xf0))
1958 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1959 else
1960 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1961 printf("\n");
1962 }
1963
1964 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001965}
1966
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00001967Qualifiers::ObjCLifetime CGObjCCommonMac::GetObjCLifeTime(QualType FQT) {
1968 if (CGM.getLangOpts().ObjCAutoRefCount)
1969 return FQT.getObjCLifetime();
1970
1971 // MRR, is more ad hoc.
1972 if (FQT.isObjCGCStrong())
1973 return Qualifiers::OCL_Strong;
1974 if (FQT.isObjCGCWeak())
1975 return Qualifiers::OCL_Weak;
1976
1977 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1978 return Qualifiers::OCL_Strong;
1979
1980 if (const PointerType *PT = FQT->getAs<PointerType>())
1981 return (GetObjCLifeTime(PT->getPointeeType()));
1982
1983 return Qualifiers::OCL_None;
1984}
1985
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00001986void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref,
1987 Qualifiers::ObjCLifetime LifeTime,
1988 unsigned FieldOffset,
1989 unsigned FieldSize) {
1990 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
1991 unsigned FieldSizeInBytes = FieldSize/ByteSizeInBits;
1992
1993 // __block variables are passed by their descriptor address.
1994 if (IsByref)
1995 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset,
1996 FieldSizeInBytes));
1997 else if (LifeTime == Qualifiers::OCL_Strong)
1998 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset,
1999 FieldSizeInBytes));
2000 else if (LifeTime == Qualifiers::OCL_Weak)
2001 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset,
2002 FieldSizeInBytes));
2003 else if (LifeTime == Qualifiers::OCL_ExplicitNone)
2004 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset,
2005 FieldSizeInBytes));
2006 else
2007 RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES,
2008 FieldOffset,
2009 FieldSizeInBytes));
2010}
2011
2012void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
2013 const RecordDecl *RD,
2014 ArrayRef<const FieldDecl*> RecFields,
2015 unsigned int BytePos, bool &HasUnion) {
2016 bool IsUnion = (RD && RD->isUnion());
2017 uint64_t MaxUnionSize = 0;
2018 const FieldDecl *MaxField = 0;
2019 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
2020 uint64_t MaxFieldOffset = 0;
2021 uint64_t LastBitfieldOrUnnamedOffset = 0;
2022
2023 if (RecFields.empty())
2024 return;
2025 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2026
2027 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
2028 const FieldDecl *Field = RecFields[i];
2029 uint64_t FieldOffset;
2030 // Note that 'i' here is actually the field index inside RD of Field,
2031 // although this dependency is hidden.
2032 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
2033 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits);
2034
2035 // Skip over unnamed or bitfields
2036 if (!Field->getIdentifier() || Field->isBitField()) {
2037 LastFieldBitfieldOrUnnamed = Field;
2038 LastBitfieldOrUnnamedOffset = FieldOffset;
2039 continue;
2040 }
2041
2042 LastFieldBitfieldOrUnnamed = 0;
2043 QualType FQT = Field->getType();
2044 if (FQT->isRecordType() || FQT->isUnionType()) {
2045 if (FQT->isUnionType())
2046 HasUnion = true;
2047
2048 BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(),
2049 BytePos + FieldOffset, HasUnion);
2050 continue;
2051 }
2052
2053 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2054 const ConstantArrayType *CArray =
2055 dyn_cast_or_null<ConstantArrayType>(Array);
2056 uint64_t ElCount = CArray->getSize().getZExtValue();
2057 assert(CArray && "only array with known element size is supported");
2058 FQT = CArray->getElementType();
2059 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
2060 const ConstantArrayType *CArray =
2061 dyn_cast_or_null<ConstantArrayType>(Array);
2062 ElCount *= CArray->getSize().getZExtValue();
2063 FQT = CArray->getElementType();
2064 }
2065
2066 assert(!FQT->isUnionType() &&
2067 "layout for array of unions not supported");
2068 if (FQT->isRecordType() && ElCount) {
2069 int OldIndex = RunSkipBlockVars.size() - 1;
2070 const RecordType *RT = FQT->getAs<RecordType>();
2071 BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset,
2072 HasUnion);
2073
2074 // Replicate layout information for each array element. Note that
2075 // one element is already done.
2076 uint64_t ElIx = 1;
2077 for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) {
2078 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
2079 for (int i = OldIndex+1; i <= FirstIndex; ++i)
2080 RunSkipBlockVars.push_back(
2081 RUN_SKIP(RunSkipBlockVars[i].opcode,
2082 RunSkipBlockVars[i].block_var_bytepos + Size*ElIx,
2083 RunSkipBlockVars[i].block_var_size));
2084 }
2085 continue;
2086 }
2087 }
2088 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
2089 if (IsUnion) {
2090 uint64_t UnionIvarSize = FieldSize;
2091 if (UnionIvarSize > MaxUnionSize) {
2092 MaxUnionSize = UnionIvarSize;
2093 MaxField = Field;
2094 MaxFieldOffset = FieldOffset;
2095 }
2096 } else {
2097 UpdateRunSkipBlockVars(false,
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002098 GetObjCLifeTime(FQT),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002099 BytePos + FieldOffset,
2100 FieldSize);
2101 }
2102 }
2103
2104 if (LastFieldBitfieldOrUnnamed) {
2105 if (LastFieldBitfieldOrUnnamed->isBitField()) {
2106 // Last field was a bitfield. Must update the info.
2107 uint64_t BitFieldSize
2108 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
2109 unsigned Size = (BitFieldSize / ByteSizeInBits) +
2110 ((BitFieldSize % ByteSizeInBits) != 0);
2111 Size += LastBitfieldOrUnnamedOffset;
2112 UpdateRunSkipBlockVars(false,
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002113 GetObjCLifeTime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002114 BytePos + LastBitfieldOrUnnamedOffset,
2115 Size*ByteSizeInBits);
2116 } else {
2117 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
2118 // Last field was unnamed. Must update skip info.
2119 unsigned FieldSize
2120 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
2121 UpdateRunSkipBlockVars(false,
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002122 GetObjCLifeTime(LastFieldBitfieldOrUnnamed->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002123 BytePos + LastBitfieldOrUnnamedOffset,
2124 FieldSize);
2125 }
2126 }
2127
2128 if (MaxField)
2129 UpdateRunSkipBlockVars(false,
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002130 GetObjCLifeTime(MaxField->getType()),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002131 BytePos + MaxFieldOffset,
2132 MaxUnionSize);
2133}
2134
2135void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT,
2136 unsigned int BytePos,
2137 bool &HasUnion) {
2138 const RecordDecl *RD = RT->getDecl();
2139 SmallVector<const FieldDecl*, 16> Fields;
2140 for (RecordDecl::field_iterator i = RD->field_begin(),
2141 e = RD->field_end(); i != e; ++i)
2142 Fields.push_back(*i);
2143 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2144 const llvm::StructLayout *RecLayout =
2145 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
2146
2147 BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion);
2148}
2149
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002150/// InlineLayoutInstruction - This routine produce an inline instruction for the
2151/// block variable layout if it can. If not, it returns 0. Rules are as follow:
2152/// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world,
2153/// an inline layout of value 0x0000000000000xyz is interpreted as follows:
2154/// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by
2155/// y captured object of BLOCK_LAYOUT_BYREF. Followed by
2156/// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero
2157/// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no
2158/// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured.
2159uint64_t CGObjCCommonMac::InlineLayoutInstruction(
2160 SmallVectorImpl<unsigned char> &Layout) {
2161 uint64_t Result = 0;
2162 if (Layout.size() <= 3) {
2163 unsigned size = Layout.size();
2164 unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0;
2165 unsigned char inst;
2166 enum BLOCK_LAYOUT_OPCODE opcode ;
2167 switch (size) {
2168 case 3:
2169 inst = Layout[0];
2170 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2171 if (opcode == BLOCK_LAYOUT_STRONG)
2172 strong_word_count = (inst & 0xF)+1;
2173 else
2174 return 0;
2175 inst = Layout[1];
2176 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2177 if (opcode == BLOCK_LAYOUT_BYREF)
2178 byref_word_count = (inst & 0xF)+1;
2179 else
2180 return 0;
2181 inst = Layout[2];
2182 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2183 if (opcode == BLOCK_LAYOUT_WEAK)
2184 weak_word_count = (inst & 0xF)+1;
2185 else
2186 return 0;
2187 break;
2188
2189 case 2:
2190 inst = Layout[0];
2191 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2192 if (opcode == BLOCK_LAYOUT_STRONG) {
2193 strong_word_count = (inst & 0xF)+1;
2194 inst = Layout[1];
2195 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2196 if (opcode == BLOCK_LAYOUT_BYREF)
2197 byref_word_count = (inst & 0xF)+1;
2198 else if (opcode == BLOCK_LAYOUT_WEAK)
2199 weak_word_count = (inst & 0xF)+1;
2200 else
2201 return 0;
2202 }
2203 else if (opcode == BLOCK_LAYOUT_BYREF) {
2204 byref_word_count = (inst & 0xF)+1;
2205 inst = Layout[1];
2206 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2207 if (opcode == BLOCK_LAYOUT_WEAK)
2208 weak_word_count = (inst & 0xF)+1;
2209 else
2210 return 0;
2211 }
2212 else
2213 return 0;
2214 break;
2215
2216 case 1:
2217 inst = Layout[0];
2218 opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2219 if (opcode == BLOCK_LAYOUT_STRONG)
2220 strong_word_count = (inst & 0xF)+1;
2221 else if (opcode == BLOCK_LAYOUT_BYREF)
2222 byref_word_count = (inst & 0xF)+1;
2223 else if (opcode == BLOCK_LAYOUT_WEAK)
2224 weak_word_count = (inst & 0xF)+1;
2225 else
2226 return 0;
2227 break;
2228
2229 default:
2230 return 0;
2231 }
2232
2233 // Cannot inline when any of the word counts is 15. Because this is one less
2234 // than the actual work count (so 15 means 16 actual word counts),
2235 // and we can only display 0 thru 15 word counts.
2236 if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16)
2237 return 0;
2238
2239 unsigned count =
2240 (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0);
2241
2242 if (size == count) {
2243 if (strong_word_count)
2244 Result = strong_word_count;
2245 Result <<= 4;
2246 if (byref_word_count)
2247 Result += byref_word_count;
2248 Result <<= 4;
2249 if (weak_word_count)
2250 Result += weak_word_count;
2251 }
2252 }
2253 return Result;
2254}
2255
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002256llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM,
2257 const CGBlockInfo &blockInfo) {
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002258 assert(CGM.getLangOpts().getGC() == LangOptions::NonGC);
2259
2260 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
2261
2262 RunSkipBlockVars.clear();
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002263 bool hasUnion = false;
2264
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002265 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
2266 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
2267 unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits;
2268
2269 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
2270
2271 // Calculate the basic layout of the block structure.
2272 const llvm::StructLayout *layout =
2273 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
2274
2275 // Ignore the optional 'this' capture: C++ objects are not assumed
2276 // to be GC'ed.
2277
2278 // Walk the captured variables.
2279 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
2280 ce = blockDecl->capture_end(); ci != ce; ++ci) {
2281 const VarDecl *variable = ci->getVariable();
2282 QualType type = variable->getType();
2283
2284 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
2285
2286 // Ignore constant captures.
2287 if (capture.isConstant()) continue;
2288
2289 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
2290
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002291 assert(!type->isArrayType() && "array variable should not be caught");
2292 if (const RecordType *record = type->getAs<RecordType>()) {
2293 BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002294 continue;
2295 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002296 unsigned fieldSize = ci->isByRef() ? WordSizeInBits
2297 : CGM.getContext().getTypeSize(type);
Fariborz Jahanian44fcff92012-11-02 22:51:18 +00002298 UpdateRunSkipBlockVars(ci->isByRef(), GetObjCLifeTime(type),
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002299 fieldOffset, fieldSize);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002300 }
2301
2302 if (RunSkipBlockVars.empty())
2303 return nullPtr;
2304
2305 // Sort on byte position; captures might not be allocated in order,
2306 // and unions can do funny things.
2307 llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end());
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002308 SmallVector<unsigned char, 16> Layout;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002309
2310 unsigned size = RunSkipBlockVars.size();
2311 unsigned int shift = (WordSizeInBytes == 8) ? 3 : 2;
2312 unsigned int mask = (WordSizeInBytes == 8) ? 0x7 : 0x3;
2313 for (unsigned i = 0; i < size; i++) {
2314 enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode;
2315 unsigned start_byte_pos = RunSkipBlockVars[i].block_var_bytepos;
2316 unsigned end_byte_pos = start_byte_pos;
2317 unsigned j = i+1;
2318 while (j < size) {
2319 if (opcode == RunSkipBlockVars[j].opcode) {
2320 end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos;
2321 i++;
2322 }
2323 else
2324 break;
2325 }
2326 unsigned size_in_bytes =
2327 end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size;
Fariborz Jahanianc6abe9e2012-10-30 20:05:29 +00002328 if (j < size) {
2329 unsigned gap =
2330 RunSkipBlockVars[j].block_var_bytepos -
2331 RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size;
2332 size_in_bytes += gap;
2333 }
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002334 unsigned residue_in_bytes = 0;
2335 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) {
2336 residue_in_bytes = size_in_bytes & mask;
2337 size_in_bytes -= residue_in_bytes;
2338 opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS;
2339 }
2340
2341 unsigned size_in_words = size_in_bytes >> shift;
2342 while (size_in_words >= 16) {
2343 // Note that value in imm. is one less that the actual
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002344 // value. So, 0xf means 16 words follow!
2345 unsigned char inst = (opcode << 4) | 0xf;
2346 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002347 size_in_words -= 16;
2348 }
2349 if (size_in_words > 0) {
2350 // Note that value in imm. is one less that the actual
2351 // value. So, we subtract 1 away!
2352 unsigned char inst = (opcode << 4) | (size_in_words-1);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002353 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002354 }
2355 if (residue_in_bytes > 0) {
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002356 unsigned char inst =
2357 (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes-1);
2358 Layout.push_back(inst);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002359 }
2360 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002361
2362 int e = Layout.size()-1;
2363 while (e >= 0) {
2364 unsigned char inst = Layout[e--];
2365 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2366 if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS)
2367 Layout.pop_back();
2368 else
2369 break;
2370 }
2371
2372 uint64_t Result = InlineLayoutInstruction(Layout);
2373 if (Result != 0) {
2374 // Block variable layout instruction has been inlined.
2375 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2376 printf("\n Inline instruction for block variable layout: ");
Matt Beaumont-Gay00a85412012-11-01 20:26:42 +00002377 printf("0x0%llx\n", (unsigned long long)Result);
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002378 }
2379 if (WordSizeInBytes == 8) {
2380 const llvm::APInt Instruction(64, Result);
2381 return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction);
2382 }
2383 else {
2384 const llvm::APInt Instruction(32, Result);
2385 return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction);
2386 }
2387 }
2388
2389 unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0;
2390 Layout.push_back(inst);
2391 std::string BitMap;
2392 for (unsigned i = 0, e = Layout.size(); i != e; i++)
2393 BitMap += Layout[i];
2394
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002395 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
2396 printf("\n block variable layout: ");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002397 for (unsigned i = 0, e = BitMap.size(); i != e; i++) {
2398 unsigned char inst = BitMap[i];
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002399 enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4);
2400 unsigned delta = 1;
2401 switch (opcode) {
2402 case BLOCK_LAYOUT_OPERATOR:
2403 printf("BL_OPERATOR:");
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002404 delta = 0;
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002405 break;
2406 case BLOCK_LAYOUT_NON_OBJECT_BYTES:
2407 printf("BL_NON_OBJECT_BYTES:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002408 break;
2409 case BLOCK_LAYOUT_NON_OBJECT_WORDS:
2410 printf("BL_NON_OBJECT_WORD:");
2411 break;
2412 case BLOCK_LAYOUT_STRONG:
2413 printf("BL_STRONG:");
2414 break;
2415 case BLOCK_LAYOUT_BYREF:
2416 printf("BL_BYREF:");
2417 break;
2418 case BLOCK_LAYOUT_WEAK:
2419 printf("BL_WEAK:");
2420 break;
2421 case BLOCK_LAYOUT_UNRETAINED:
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002422 printf("BL_UNRETAINED:");
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002423 break;
2424 }
2425 // Actual value of word count is one more that what is in the imm.
2426 // field of the instruction
2427 printf("%d", (inst & 0xf) + delta);
2428 if (i < e-1)
2429 printf(", ");
2430 else
2431 printf("\n");
2432 }
2433 }
Fariborz Jahanianf22ae652012-11-01 18:32:55 +00002434
2435 llvm::GlobalVariable * Entry =
2436 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2437 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
2438 "__TEXT,__objc_classname,cstring_literals", 1, true);
2439 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc46b4352012-10-27 21:10:38 +00002440}
2441
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002442llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00002443 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002444 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002445 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00002446 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2447
Owen Anderson3c4972d2009-07-29 18:54:39 +00002448 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00002449 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002450}
2451
Fariborz Jahanianda320092009-01-29 19:24:30 +00002452void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00002453 // FIXME: We shouldn't need this, the protocol decl should contain enough
2454 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002455 DefinedProtocols.insert(PD->getIdentifier());
2456
2457 // If we have generated a forward reference to this protocol, emit
2458 // it now. Otherwise do nothing, the protocol objects are lazily
2459 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002460 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002461 GetOrEmitProtocol(PD);
2462}
2463
Fariborz Jahanianda320092009-01-29 19:24:30 +00002464llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002465 if (DefinedProtocols.count(PD->getIdentifier()))
2466 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002467
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002468 return GetOrEmitProtocolRef(PD);
2469}
2470
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002471/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002472// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
2473struct _objc_protocol {
2474struct _objc_protocol_extension *isa;
2475char *protocol_name;
2476struct _objc_protocol_list *protocol_list;
2477struct _objc__method_prototype_list *instance_methods;
2478struct _objc__method_prototype_list *class_methods
2479};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002480
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002481See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002482*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002483llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00002484 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002485
2486 // Early exit if a defining object has already been generated.
2487 if (Entry && Entry->hasInitializer())
2488 return Entry;
2489
Douglas Gregor1d784b22012-01-01 19:51:50 +00002490 // Use the protocol definition, if there is one.
2491 if (const ObjCProtocolDecl *Def = PD->getDefinition())
2492 PD = Def;
2493
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002494 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00002495 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002496 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
2497
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002498 // Construct method lists.
2499 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2500 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002501 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002502 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002503 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002504 ObjCMethodDecl *MD = *i;
2505 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002506 if (!C)
2507 return GetOrEmitProtocolRef(PD);
2508
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002509 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2510 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002511 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002512 } else {
2513 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002514 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002515 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002516 }
2517
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002518 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002519 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002520 ObjCMethodDecl *MD = *i;
2521 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00002522 if (!C)
2523 return GetOrEmitProtocolRef(PD);
2524
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002525 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
2526 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002527 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002528 } else {
2529 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00002530 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002531 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002532 }
2533
Bob Wilsondc8dab62011-11-30 01:57:58 +00002534 MethodTypesExt.insert(MethodTypesExt.end(),
2535 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
2536
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002537 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002538 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
2539 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002540 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002541 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00002542 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002543 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002544 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002545 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002546 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002547 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002548 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002549 ClassMethods)
2550 };
Owen Anderson08e25242009-07-27 22:29:56 +00002551 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002552 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002553
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002554 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002555 // Already created, fix the linkage and update the initializer.
2556 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002557 Entry->setInitializer(Init);
2558 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002559 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002560 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002561 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002562 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002563 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002564 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002565 // FIXME: Is this necessary? Why only for protocol?
2566 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002567
2568 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002569 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002570 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002571
2572 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002573}
2574
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002575llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002576 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2577
2578 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002579 // We use the initializer as a marker of whether this is a forward
2580 // reference or not. At module finalization we add the empty
2581 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002582 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002583 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002584 llvm::GlobalValue::ExternalLinkage,
2585 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002586 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002587 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002588 // FIXME: Is this necessary? Why only for protocol?
2589 Entry->setAlignment(4);
2590 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002591
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002592 return Entry;
2593}
2594
2595/*
2596 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002597 uint32_t size;
2598 struct objc_method_description_list *optional_instance_methods;
2599 struct objc_method_description_list *optional_class_methods;
2600 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002601 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002602 };
2603*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002604llvm::Constant *
2605CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002606 ArrayRef<llvm::Constant*> OptInstanceMethods,
2607 ArrayRef<llvm::Constant*> OptClassMethods,
2608 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002609 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002610 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002611 llvm::Constant *Values[] = {
2612 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002613 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002614 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002615 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002616 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002617 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002618 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002619 OptClassMethods),
2620 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002621 ObjCTypes),
2622 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2623 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002624 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002625
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002626 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002627 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002628 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002629 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002630
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002631 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002632 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002634 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002635 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002636 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002637 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002638}
2639
2640/*
2641 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002642 struct objc_protocol_list *next;
2643 long count;
2644 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002645 };
2646*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00002647llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002648CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00002649 ObjCProtocolDecl::protocol_iterator begin,
2650 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002651 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002652
Daniel Dunbardbc933702008-08-21 21:57:41 +00002653 for (; begin != end; ++begin)
2654 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002655
2656 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002657 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002658 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002659
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002660 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002661 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002662
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002663 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002664 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002665 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002666 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002667 ProtocolRefs.size() - 1);
2668 Values[2] =
2669 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2670 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002671 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002672
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002673 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002674 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002675 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002676 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002677 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002678}
2679
Bill Wendlingbb028552012-02-07 09:25:09 +00002680void CGObjCCommonMac::
2681PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002682 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002683 const Decl *Container,
2684 const ObjCProtocolDecl *PROTO,
2685 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002686 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2687 E = PROTO->protocol_end(); P != E; ++P)
2688 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2689 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2690 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002691 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002692 if (!PropertySet.insert(PD->getIdentifier()))
2693 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002694 llvm::Constant *Prop[] = {
2695 GetPropertyName(PD->getIdentifier()),
2696 GetPropertyTypeString(PD, Container)
2697 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002698 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2699 }
2700}
2701
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002702/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002703 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002704 const char * const name;
2705 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002706 };
2707
2708 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002709 uint32_t entsize; // sizeof (struct _objc_property)
2710 uint32_t prop_count;
2711 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002712 };
2713*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002714llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002715 const Decl *Container,
2716 const ObjCContainerDecl *OCD,
2717 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002718 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002719 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002720 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2721 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002722 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002723 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002724 llvm::Constant *Prop[] = {
2725 GetPropertyName(PD->getIdentifier()),
2726 GetPropertyTypeString(PD, Container)
2727 };
Owen Anderson08e25242009-07-27 22:29:56 +00002728 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002729 Prop));
2730 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002731 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002732 for (ObjCInterfaceDecl::all_protocol_iterator
2733 P = OID->all_referenced_protocol_begin(),
2734 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002735 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2736 ObjCTypes);
2737 }
2738 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2739 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2740 E = CD->protocol_end(); P != E; ++P)
2741 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2742 ObjCTypes);
2743 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002744
2745 // Return null for empty list.
2746 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002747 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002748
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002749 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002750 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002751 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002752 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2753 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002754 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002755 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002756 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002757 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002758
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002759 llvm::GlobalVariable *GV =
2760 CreateMetadataVar(Name, Init,
2761 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002762 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002763 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002764 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002765 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002766}
2767
Bill Wendlingbb028552012-02-07 09:25:09 +00002768llvm::Constant *
2769CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2770 ArrayRef<llvm::Constant*> MethodTypes,
2771 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002772 // Return null for empty list.
2773 if (MethodTypes.empty())
2774 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2775
2776 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2777 MethodTypes.size());
2778 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2779
2780 llvm::GlobalVariable *GV =
2781 CreateMetadataVar(Name, Init,
2782 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2783 (ObjCABI == 2) ? 8 : 4,
2784 true);
2785 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2786}
2787
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002788/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002789 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002790 int count;
2791 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002792 };
2793*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002794llvm::Constant *
2795CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002796 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002797 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002798 ObjCTypes.SelectorPtrTy),
2799 GetMethodVarType(MD)
2800 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002801 if (!Desc[1])
2802 return 0;
2803
Owen Anderson08e25242009-07-27 22:29:56 +00002804 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002805 Desc);
2806}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002807
Bill Wendlingbb028552012-02-07 09:25:09 +00002808llvm::Constant *
2809CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2810 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002811 // Return null for empty list.
2812 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002813 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002814
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002815 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002816 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002817 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002818 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002819 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002820 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002821
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002822 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002823 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002824 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002825}
2826
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002827/*
2828 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002829 char *category_name;
2830 char *class_name;
2831 struct _objc_method_list *instance_methods;
2832 struct _objc_method_list *class_methods;
2833 struct _objc_protocol_list *protocols;
2834 uint32_t size; // <rdar://4585769>
2835 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002836 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002837*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002838void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002839 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002840
Mike Stumpf5408fe2009-05-16 07:57:57 +00002841 // FIXME: This is poor design, the OCD should have a pointer to the category
2842 // decl. Additionally, note that Category can be null for the @implementation
2843 // w/o an @interface case. Sema should just create one for us as it does for
2844 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002845 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002846 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002847 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002848
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002849 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002850 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2851 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002852
Bill Wendling3964e622012-02-09 22:16:49 +00002853 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002854 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002855 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002856 // Instance methods should always be defined.
2857 InstanceMethods.push_back(GetMethodConstant(*i));
2858 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002859 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002860 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002861 // Class methods should always be defined.
2862 ClassMethods.push_back(GetMethodConstant(*i));
2863 }
2864
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002865 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002866 Values[0] = GetClassName(OCD->getIdentifier());
2867 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002868 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002869 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002870 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002871 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002872 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002873 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002874 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002875 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002876 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002877 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002878 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002879 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002880 Category->protocol_begin(),
2881 Category->protocol_end());
2882 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002883 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002884 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002885 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002886
2887 // If there is no category @interface then there can be no properties.
2888 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002889 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002890 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002891 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002892 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002893 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002894
Owen Anderson08e25242009-07-27 22:29:56 +00002895 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002896 Values);
2897
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002898 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002899 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002900 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002901 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002902 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002903 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002904 // method definition entries must be clear for next implementation.
2905 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002906}
2907
John McCall621915c2012-10-17 04:53:23 +00002908enum FragileClassFlags {
2909 FragileABI_Class_Factory = 0x00001,
2910 FragileABI_Class_Meta = 0x00002,
2911 FragileABI_Class_HasCXXStructors = 0x02000,
2912 FragileABI_Class_Hidden = 0x20000
2913};
2914
2915enum NonFragileClassFlags {
2916 /// Is a meta-class.
2917 NonFragileABI_Class_Meta = 0x00001,
2918
2919 /// Is a root class.
2920 NonFragileABI_Class_Root = 0x00002,
2921
2922 /// Has a C++ constructor and destructor.
2923 NonFragileABI_Class_HasCXXStructors = 0x00004,
2924
2925 /// Has hidden visibility.
2926 NonFragileABI_Class_Hidden = 0x00010,
2927
2928 /// Has the exception attribute.
2929 NonFragileABI_Class_Exception = 0x00020,
2930
2931 /// (Obsolete) ARC-specific: this class has a .release_ivars method
2932 NonFragileABI_Class_HasIvarReleaser = 0x00040,
2933
2934 /// Class implementation was compiled under ARC.
John McCallb03527a2012-10-17 04:53:31 +00002935 NonFragileABI_Class_CompiledByARC = 0x00080,
2936
2937 /// Class has non-trivial destructors, but zero-initialization is okay.
2938 NonFragileABI_Class_HasCXXDestructorOnly = 0x00100
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002939};
2940
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002941/*
2942 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002943 Class isa;
2944 Class super_class;
2945 const char *name;
2946 long version;
2947 long info;
2948 long instance_size;
2949 struct _objc_ivar_list *ivars;
2950 struct _objc_method_list *methods;
2951 struct _objc_cache *cache;
2952 struct _objc_protocol_list *protocols;
2953 // Objective-C 1.0 extensions (<rdr://4585769>)
2954 const char *ivar_layout;
2955 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002956 };
2957
2958 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002959*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002960void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002961 DefinedSymbols.insert(ID->getIdentifier());
2962
Chris Lattner8ec03f52008-11-24 03:54:41 +00002963 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002964 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002965 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002966 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002967 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002968 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002969 Interface->all_referenced_protocol_begin(),
2970 Interface->all_referenced_protocol_end());
John McCall621915c2012-10-17 04:53:23 +00002971 unsigned Flags = FragileABI_Class_Factory;
John McCallb03527a2012-10-17 04:53:31 +00002972 if (ID->hasNonZeroConstructors() || ID->hasDestructors())
John McCall621915c2012-10-17 04:53:23 +00002973 Flags |= FragileABI_Class_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002974 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002975 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002976
2977 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002978 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00002979 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002980
Bill Wendling3964e622012-02-09 22:16:49 +00002981 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002982 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002983 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002984 // Instance methods should always be defined.
2985 InstanceMethods.push_back(GetMethodConstant(*i));
2986 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002987 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002988 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002989 // Class methods should always be defined.
2990 ClassMethods.push_back(GetMethodConstant(*i));
2991 }
2992
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002993 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002994 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002995 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002996
2997 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2998 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2999
3000 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3001 if (llvm::Constant *C = GetMethodConstant(MD))
3002 InstanceMethods.push_back(C);
3003 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3004 if (llvm::Constant *C = GetMethodConstant(MD))
3005 InstanceMethods.push_back(C);
3006 }
3007 }
3008
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003009 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00003010 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003011 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003012 // Record a reference to the super class.
3013 LazySymbols.insert(Super->getIdentifier());
3014
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003015 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003016 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003017 ObjCTypes.ClassPtrTy);
3018 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003019 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003020 }
3021 Values[ 2] = GetClassName(ID->getIdentifier());
3022 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003023 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3024 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3025 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003026 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003027 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003028 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003029 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003030 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003031 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003032 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003033 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003034 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003035 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00003036 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003037 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003038 std::string Name("\01L_OBJC_CLASS_");
3039 Name += ClassName;
3040 const char *Section = "__OBJC,__class,regular,no_dead_strip";
3041 // Check for a forward reference.
3042 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3043 if (GV) {
3044 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3045 "Forward metaclass reference has incorrect type.");
3046 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3047 GV->setInitializer(Init);
3048 GV->setSection(Section);
3049 GV->setAlignment(4);
3050 CGM.AddUsedGlobal(GV);
3051 }
3052 else
3053 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003054 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00003055 // method definition entries must be clear for next implementation.
3056 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003057}
3058
3059llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
3060 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00003061 ArrayRef<llvm::Constant*> Methods) {
John McCall621915c2012-10-17 04:53:23 +00003062 unsigned Flags = FragileABI_Class_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00003063 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003064
John McCall1fb0caa2010-10-22 21:05:15 +00003065 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
John McCall621915c2012-10-17 04:53:23 +00003066 Flags |= FragileABI_Class_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003067
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003068 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003069 // The isa for the metaclass is the root of the hierarchy.
3070 const ObjCInterfaceDecl *Root = ID->getClassInterface();
3071 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
3072 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003073 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003074 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003075 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003076 // The super class for the metaclass is emitted as the name of the
3077 // super class. The runtime fixes this up to point to the
3078 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003079 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003080 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003081 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003082 ObjCTypes.ClassPtrTy);
3083 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003084 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003085 }
3086 Values[ 2] = GetClassName(ID->getIdentifier());
3087 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003088 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3089 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
3090 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003091 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003092 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003093 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003094 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003095 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003096 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003097 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003098 Values[ 9] = Protocols;
3099 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003100 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003101 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00003102 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00003103 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003104 Values);
3105
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003106 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00003107 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003108
3109 // Check for a forward reference.
3110 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
3111 if (GV) {
3112 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3113 "Forward metaclass reference has incorrect type.");
3114 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
3115 GV->setInitializer(Init);
3116 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00003117 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003118 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00003119 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003120 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003121 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00003122 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00003123 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003124
3125 return GV;
3126}
3127
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003128llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00003129 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003130
Mike Stumpf5408fe2009-05-16 07:57:57 +00003131 // FIXME: Should we look these up somewhere other than the module. Its a bit
3132 // silly since we only generate these while processing an implementation, so
3133 // exactly one pointer would work if know when we entered/exitted an
3134 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003135
3136 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00003137 // Previously, metaclass with internal linkage may have been defined.
3138 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003139 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3140 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003141 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3142 "Forward metaclass reference has incorrect type.");
3143 return GV;
3144 } else {
3145 // Generate as an external reference to keep a consistent
3146 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00003147 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003148 llvm::GlobalValue::ExternalLinkage,
3149 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00003150 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00003151 }
3152}
3153
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00003154llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
3155 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
3156
3157 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
3158 true)) {
3159 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
3160 "Forward class metadata reference has incorrect type.");
3161 return GV;
3162 } else {
3163 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
3164 llvm::GlobalValue::ExternalLinkage,
3165 0,
3166 Name);
3167 }
3168}
3169
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003170/*
3171 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003172 uint32_t size;
3173 const char *weak_ivar_layout;
3174 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003175 };
3176*/
3177llvm::Constant *
3178CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003179 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00003180 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003181
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003182 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003183 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00003184 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003185 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003186 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003187
3188 // Return null if no extension bits are used.
3189 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003190 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003191
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003192 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00003193 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003194 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003195 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003196 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003197}
3198
3199/*
3200 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00003201 char *ivar_name;
3202 char *ivar_type;
3203 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003204 };
3205
3206 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00003207 int ivar_count;
3208 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003209 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003210*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003211llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003212 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003213 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003214
3215 // When emitting the root class GCC emits ivar entries for the
3216 // actual class structure. It is not clear if we need to follow this
3217 // behavior; for now lets try and get away with not doing it. If so,
3218 // the cleanest solution would be to make up an ObjCInterfaceDecl
3219 // for the class.
3220 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003221 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003222
Jordy Rosedb8264e2011-07-22 02:08:32 +00003223 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003224
Jordy Rosedb8264e2011-07-22 02:08:32 +00003225 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003226 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00003227 // Ignore unnamed bit-fields.
3228 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003229 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003230 llvm::Constant *Ivar[] = {
3231 GetMethodVarName(IVD->getIdentifier()),
3232 GetMethodVarType(IVD),
3233 llvm::ConstantInt::get(ObjCTypes.IntTy,
3234 ComputeIvarBaseOffset(CGM, OID, IVD))
3235 };
Owen Anderson08e25242009-07-27 22:29:56 +00003236 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003237 }
3238
3239 // Return null for empty list.
3240 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003241 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003242
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003243 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003244 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003245 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003246 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003247 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003248 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003249
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003250 llvm::GlobalVariable *GV;
3251 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003252 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003253 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003254 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003255 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003256 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003257 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003258 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003259 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003260}
3261
3262/*
3263 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003264 SEL method_name;
3265 char *method_types;
3266 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003267 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003268
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003269 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003270 struct objc_method_list *obsolete;
3271 int count;
3272 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003273 };
3274*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003275
3276/// GetMethodConstant - Return a struct objc_method constant for the
3277/// given method if it has been defined. The result is null if the
3278/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00003279llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003280 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003281 if (!Fn)
3282 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003283
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003284 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00003285 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003286 ObjCTypes.SelectorPtrTy),
3287 GetMethodVarType(MD),
3288 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
3289 };
Owen Anderson08e25242009-07-27 22:29:56 +00003290 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003291}
3292
Chris Lattner5f9e2722011-07-23 10:55:15 +00003293llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003294 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00003295 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003296 // Return null for empty list.
3297 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003298 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003299
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003300 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00003301 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003302 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003303 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003304 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00003305 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003306 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003307
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003308 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003309 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003310}
3311
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003312llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003313 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003314 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003315 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003316
Daniel Dunbar541b63b2009-02-02 23:23:47 +00003317 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00003318 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00003319 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003320 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00003321 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003322 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003323 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003324 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003325 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003326
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003327 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003328}
3329
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003330llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00003331CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003332 llvm::Constant *Init,
3333 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003334 unsigned Align,
3335 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003336 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003337 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00003338 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00003339 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003340 if (Section)
3341 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00003342 if (Align)
3343 GV->setAlignment(Align);
3344 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00003345 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00003346 return GV;
3347}
3348
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003349llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003350 // Abuse this interface function as a place to finalize.
3351 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003352 return NULL;
3353}
3354
Chris Lattner74391b42009-03-22 21:03:39 +00003355llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003356 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003357}
3358
Chris Lattner74391b42009-03-22 21:03:39 +00003359llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003360 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00003361}
3362
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003363llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
3364 bool copy) {
3365 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
3366}
3367
David Chisnall8fac25d2010-12-26 22:13:16 +00003368llvm::Constant *CGObjCMac::GetGetStructFunction() {
3369 return ObjCTypes.getCopyStructFn();
3370}
3371llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00003372 return ObjCTypes.getCopyStructFn();
3373}
3374
Fariborz Jahaniane3173022012-01-06 18:07:23 +00003375llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
3376 return ObjCTypes.getCppAtomicObjectFunction();
3377}
3378
Chris Lattner74391b42009-03-22 21:03:39 +00003379llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00003380 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003381}
3382
John McCallf1549f62010-07-06 01:34:17 +00003383void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
3384 return EmitTryOrSynchronizedStmt(CGF, S);
3385}
3386
3387void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
3388 const ObjCAtSynchronizedStmt &S) {
3389 return EmitTryOrSynchronizedStmt(CGF, S);
3390}
3391
John McCallcc505292010-07-21 06:59:36 +00003392namespace {
John McCall1f0fca52010-07-21 07:22:38 +00003393 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00003394 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00003395 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00003396 llvm::Value *CallTryExitVar;
3397 llvm::Value *ExceptionData;
3398 ObjCTypesHelper &ObjCTypes;
3399 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00003400 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00003401 llvm::Value *CallTryExitVar,
3402 llvm::Value *ExceptionData,
3403 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00003404 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00003405 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
3406
John McCallad346f42011-07-12 20:27:29 +00003407 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00003408 // Check whether we need to call objc_exception_try_exit.
3409 // In optimized code, this branch will always be folded.
3410 llvm::BasicBlock *FinallyCallExit =
3411 CGF.createBasicBlock("finally.call_exit");
3412 llvm::BasicBlock *FinallyNoCallExit =
3413 CGF.createBasicBlock("finally.no_call_exit");
3414 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
3415 FinallyCallExit, FinallyNoCallExit);
3416
3417 CGF.EmitBlock(FinallyCallExit);
3418 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
3419 ->setDoesNotThrow();
3420
3421 CGF.EmitBlock(FinallyNoCallExit);
3422
3423 if (isa<ObjCAtTryStmt>(S)) {
3424 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00003425 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
3426 // Save the current cleanup destination in case there's
3427 // control flow inside the finally statement.
3428 llvm::Value *CurCleanupDest =
3429 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
3430
John McCallcc505292010-07-21 06:59:36 +00003431 CGF.EmitStmt(FinallyStmt->getFinallyBody());
3432
John McCalld96a8e72010-08-11 00:16:14 +00003433 if (CGF.HaveInsertPoint()) {
3434 CGF.Builder.CreateStore(CurCleanupDest,
3435 CGF.getNormalCleanupDestSlot());
3436 } else {
3437 // Currently, the end of the cleanup must always exist.
3438 CGF.EnsureInsertPoint();
3439 }
3440 }
John McCallcc505292010-07-21 06:59:36 +00003441 } else {
3442 // Emit objc_sync_exit(expr); as finally's sole statement for
3443 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00003444 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00003445 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
3446 ->setDoesNotThrow();
3447 }
3448 }
3449 };
John McCall87bb5822010-07-31 23:20:56 +00003450
3451 class FragileHazards {
3452 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003453 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00003454 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
3455
3456 llvm::InlineAsm *ReadHazard;
3457 llvm::InlineAsm *WriteHazard;
3458
3459 llvm::FunctionType *GetAsmFnType();
3460
3461 void collectLocals();
3462 void emitReadHazard(CGBuilderTy &Builder);
3463
3464 public:
3465 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00003466
John McCall87bb5822010-07-31 23:20:56 +00003467 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00003468 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003469 };
3470}
3471
3472/// Create the fragile-ABI read and write hazards based on the current
3473/// state of the function, which is presumed to be immediately prior
3474/// to a @try block. These hazards are used to maintain correct
3475/// semantics in the face of optimization and the fragile ABI's
3476/// cavalier use of setjmp/longjmp.
3477FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
3478 collectLocals();
3479
3480 if (Locals.empty()) return;
3481
3482 // Collect all the blocks in the function.
3483 for (llvm::Function::iterator
3484 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
3485 BlocksBeforeTry.insert(&*I);
3486
3487 llvm::FunctionType *AsmFnTy = GetAsmFnType();
3488
3489 // Create a read hazard for the allocas. This inhibits dead-store
3490 // optimizations and forces the values to memory. This hazard is
3491 // inserted before any 'throwing' calls in the protected scope to
3492 // reflect the possibility that the variables might be read from the
3493 // catch block if the call throws.
3494 {
3495 std::string Constraint;
3496 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3497 if (I) Constraint += ',';
3498 Constraint += "*m";
3499 }
3500
3501 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3502 }
3503
3504 // Create a write hazard for the allocas. This inhibits folding
3505 // loads across the hazard. This hazard is inserted at the
3506 // beginning of the catch path to reflect the possibility that the
3507 // variables might have been written within the protected scope.
3508 {
3509 std::string Constraint;
3510 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
3511 if (I) Constraint += ',';
3512 Constraint += "=*m";
3513 }
3514
3515 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
3516 }
3517}
3518
3519/// Emit a write hazard at the current location.
3520void FragileHazards::emitWriteHazard() {
3521 if (Locals.empty()) return;
3522
Jay Foad4c7d9f12011-07-15 08:37:34 +00003523 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003524}
3525
John McCall87bb5822010-07-31 23:20:56 +00003526void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
3527 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00003528 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00003529}
3530
3531/// Emit read hazards in all the protected blocks, i.e. all the blocks
3532/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00003533void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00003534 if (Locals.empty()) return;
3535
3536 CGBuilderTy Builder(CGF.getLLVMContext());
3537
3538 // Iterate through all blocks, skipping those prior to the try.
3539 for (llvm::Function::iterator
3540 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
3541 llvm::BasicBlock &BB = *FI;
3542 if (BlocksBeforeTry.count(&BB)) continue;
3543
3544 // Walk through all the calls in the block.
3545 for (llvm::BasicBlock::iterator
3546 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
3547 llvm::Instruction &I = *BI;
3548
3549 // Ignore instructions that aren't non-intrinsic calls.
3550 // These are the only calls that can possibly call longjmp.
3551 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
3552 if (isa<llvm::IntrinsicInst>(I))
3553 continue;
3554
3555 // Ignore call sites marked nounwind. This may be questionable,
3556 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
3557 llvm::CallSite CS(&I);
3558 if (CS.doesNotThrow()) continue;
3559
John McCall0b251722010-08-04 05:59:32 +00003560 // Insert a read hazard before the call. This will ensure that
3561 // any writes to the locals are performed before making the
3562 // call. If the call throws, then this is sufficient to
3563 // guarantee correctness as long as it doesn't also write to any
3564 // locals.
John McCall87bb5822010-07-31 23:20:56 +00003565 Builder.SetInsertPoint(&BB, BI);
3566 emitReadHazard(Builder);
3567 }
3568 }
3569}
3570
3571static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
3572 if (V) S.insert(V);
3573}
3574
3575void FragileHazards::collectLocals() {
3576 // Compute a set of allocas to ignore.
3577 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
3578 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
3579 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00003580
3581 // Collect all the allocas currently in the function. This is
3582 // probably way too aggressive.
3583 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3584 for (llvm::BasicBlock::iterator
3585 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3586 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3587 Locals.push_back(&*I);
3588}
3589
3590llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003591 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003592 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3593 tys[i] = Locals[i]->getType();
3594 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003595}
3596
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003597/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003598
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599 Objective-C setjmp-longjmp (sjlj) Exception Handling
3600 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003601
John McCallf1549f62010-07-06 01:34:17 +00003602 A catch buffer is a setjmp buffer plus:
3603 - a pointer to the exception that was caught
3604 - a pointer to the previous exception data buffer
3605 - two pointers of reserved storage
3606 Therefore catch buffers form a stack, with a pointer to the top
3607 of the stack kept in thread-local storage.
3608
3609 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3610 objc_exception_try_exit pops the given catch buffer, which is
3611 required to be the top of the EH stack.
3612 objc_exception_throw pops the top of the EH stack, writes the
3613 thrown exception into the appropriate field, and longjmps
3614 to the setjmp buffer. It crashes the process (with a printf
3615 and an abort()) if there are no catch buffers on the stack.
3616 objc_exception_extract just reads the exception pointer out of the
3617 catch buffer.
3618
3619 There's no reason an implementation couldn't use a light-weight
3620 setjmp here --- something like __builtin_setjmp, but API-compatible
3621 with the heavyweight setjmp. This will be more important if we ever
3622 want to implement correct ObjC/C++ exception interactions for the
3623 fragile ABI.
3624
3625 Note that for this use of setjmp/longjmp to be correct, we may need
3626 to mark some local variables volatile: if a non-volatile local
3627 variable is modified between the setjmp and the longjmp, it has
3628 indeterminate value. For the purposes of LLVM IR, it may be
3629 sufficient to make loads and stores within the @try (to variables
3630 declared outside the @try) volatile. This is necessary for
3631 optimized correctness, but is not currently being done; this is
3632 being tracked as rdar://problem/8160285
3633
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003634 The basic framework for a @try-catch-finally is as follows:
3635 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003636 objc_exception_data d;
3637 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003638 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003639
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003640 objc_exception_try_enter(&d);
3641 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003642 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003643 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003644 // exception path
3645 id _caught = objc_exception_extract(&d);
3646
3647 // enter new try scope for handlers
3648 if (!setjmp(d.jmp_buf)) {
3649 ... match exception and execute catch blocks ...
3650
3651 // fell off end, rethrow.
3652 _rethrow = _caught;
3653 ... jump-through-finally to finally_rethrow ...
3654 } else {
3655 // exception in catch block
3656 _rethrow = objc_exception_extract(&d);
3657 _call_try_exit = false;
3658 ... jump-through-finally to finally_rethrow ...
3659 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003660 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003661 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003662
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003663 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003664 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003665 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003666
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003667 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003668 ... dispatch to finally destination ...
3669
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003670 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003671 objc_exception_throw(_rethrow);
3672
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003673 finally_end:
3674 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003675
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 This framework differs slightly from the one gcc uses, in that gcc
3677 uses _rethrow to determine if objc_exception_try_exit should be called
3678 and if the object should be rethrown. This breaks in the face of
3679 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003680
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003682
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003683 - If there are no catch blocks, then we avoid emitting the second
3684 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003685
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003686 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3687 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003688
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003689 - FIXME: If there is no @finally block we can do a few more
3690 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003691
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003692 Rethrows and Jumps-Through-Finally
3693 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003694
John McCallf1549f62010-07-06 01:34:17 +00003695 '@throw;' is supported by pushing the currently-caught exception
3696 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003697
John McCallf1549f62010-07-06 01:34:17 +00003698 Branches through the @finally block are handled with an ordinary
3699 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3700 exceptions are not compatible with C++ exceptions, and this is
3701 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003702
John McCallf1549f62010-07-06 01:34:17 +00003703 @synchronized(expr) { stmt; } is emitted as if it were:
3704 id synch_value = expr;
3705 objc_sync_enter(synch_value);
3706 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003707*/
3708
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003709void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3710 const Stmt &S) {
3711 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003712
3713 // A destination for the fall-through edges of the catch handlers to
3714 // jump to.
3715 CodeGenFunction::JumpDest FinallyEnd =
3716 CGF.getJumpDestInCurrentScope("finally.end");
3717
3718 // A destination for the rethrow edge of the catch handlers to jump
3719 // to.
3720 CodeGenFunction::JumpDest FinallyRethrow =
3721 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003722
Daniel Dunbar1c566672009-02-24 01:43:46 +00003723 // For @synchronized, call objc_sync_enter(sync.expr). The
3724 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003725 // @synchronized. We can't avoid a temp here because we need the
3726 // value to be preserved. If the backend ever does liveness
3727 // correctly after setjmp, this will be unnecessary.
3728 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003729 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003730 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003731 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3732 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003733 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3734 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003735
3736 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3737 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003738 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003739
John McCall0b251722010-08-04 05:59:32 +00003740 // Allocate memory for the setjmp buffer. This needs to be kept
3741 // live throughout the try and catch blocks.
3742 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3743 "exceptiondata.ptr");
3744
John McCall87bb5822010-07-31 23:20:56 +00003745 // Create the fragile hazards. Note that this will not capture any
3746 // of the allocas required for exception processing, but will
3747 // capture the current basic block (which extends all the way to the
3748 // setjmp call) as "before the @try".
3749 FragileHazards Hazards(CGF);
3750
John McCallf1549f62010-07-06 01:34:17 +00003751 // Create a flag indicating whether the cleanup needs to call
3752 // objc_exception_try_exit. This is true except when
3753 // - no catches match and we're branching through the cleanup
3754 // just to rethrow the exception, or
3755 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003756 // The setjmp-safety rule here is that we should always store to this
3757 // variable in a place that dominates the branch through the cleanup
3758 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003759 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003760 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003761
John McCall9e2213d2010-10-04 23:42:51 +00003762 // A slot containing the exception to rethrow. Only needed when we
3763 // have both a @catch and a @finally.
3764 llvm::Value *PropagatingExnVar = 0;
3765
John McCallf1549f62010-07-06 01:34:17 +00003766 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003767 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003768 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003769 CallTryExitVar,
3770 ExceptionData,
3771 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003772
3773 // Enter a try block:
3774 // - Call objc_exception_try_enter to push ExceptionData on top of
3775 // the EH stack.
3776 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3777 ->setDoesNotThrow();
3778
3779 // - Call setjmp on the exception data buffer.
3780 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3781 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3782 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003783 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003784 llvm::CallInst *SetJmpResult =
3785 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3786 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003787 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003788
3789 // If setjmp returned 0, enter the protected block; otherwise,
3790 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003791 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3792 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003793 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003794 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3795 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003796
John McCallf1549f62010-07-06 01:34:17 +00003797 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003798 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003799 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003800 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003801 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003802
3803 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003804
John McCallf1549f62010-07-06 01:34:17 +00003805 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003806 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003807
John McCall87bb5822010-07-31 23:20:56 +00003808 // Don't optimize loads of the in-scope locals across this point.
3809 Hazards.emitWriteHazard();
3810
John McCallf1549f62010-07-06 01:34:17 +00003811 // For a @synchronized (or a @try with no catches), just branch
3812 // through the cleanup to the rethrow block.
3813 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3814 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003815 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003816 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003817
3818 // Otherwise, we have to match against the caught exceptions.
3819 } else {
John McCall0b251722010-08-04 05:59:32 +00003820 // Retrieve the exception object. We may emit multiple blocks but
3821 // nothing can cross this so the value is already in SSA form.
3822 llvm::CallInst *Caught =
3823 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3824 ExceptionData, "caught");
3825 Caught->setDoesNotThrow();
3826
John McCallf1549f62010-07-06 01:34:17 +00003827 // Push the exception to rethrow onto the EH value stack for the
3828 // benefit of any @throws in the handlers.
3829 CGF.ObjCEHValueStack.push_back(Caught);
3830
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003831 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003832
John McCall0b251722010-08-04 05:59:32 +00003833 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003834
John McCall0b251722010-08-04 05:59:32 +00003835 llvm::BasicBlock *CatchBlock = 0;
3836 llvm::BasicBlock *CatchHandler = 0;
3837 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003838 // Save the currently-propagating exception before
3839 // objc_exception_try_enter clears the exception slot.
3840 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3841 "propagating_exception");
3842 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3843
John McCall0b251722010-08-04 05:59:32 +00003844 // Enter a new exception try block (in case a @catch block
3845 // throws an exception).
3846 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3847 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003848
John McCall0b251722010-08-04 05:59:32 +00003849 llvm::CallInst *SetJmpResult =
3850 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3851 "setjmp.result");
3852 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003853 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003854
John McCall0b251722010-08-04 05:59:32 +00003855 llvm::Value *Threw =
3856 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3857
3858 CatchBlock = CGF.createBasicBlock("catch");
3859 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3860 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3861
3862 CGF.EmitBlock(CatchBlock);
3863 }
3864
3865 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003866
Daniel Dunbar55e40722008-09-27 07:03:52 +00003867 // Handle catch list. As a special case we check if everything is
3868 // matched and avoid generating code for falling off the end if
3869 // so.
3870 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003871 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3872 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003873
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003874 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003875 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003876
Anders Carlsson80f25672008-09-09 17:59:25 +00003877 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003878 if (!CatchParam) {
3879 AllMatched = true;
3880 } else {
John McCall183700f2009-09-21 23:43:11 +00003881 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003882
John McCallf1549f62010-07-06 01:34:17 +00003883 // catch(id e) always matches under this ABI, since only
3884 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003885 // FIXME: For the time being we also match id<X>; this should
3886 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003887 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003888 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003889 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003890
John McCallf1549f62010-07-06 01:34:17 +00003891 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003892 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003893 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3894
Anders Carlssondde0a942008-09-11 09:15:33 +00003895 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003896 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003897 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003898
3899 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003900 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003901 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003902
Anders Carlssondde0a942008-09-11 09:15:33 +00003903 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003904
3905 // The scope of the catch variable ends right here.
3906 CatchVarCleanups.ForceCleanup();
3907
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003908 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003909 break;
3910 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003911
Steve Naroff14108da2009-07-10 23:34:53 +00003912 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003913 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003914
3915 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003916 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3917 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003918
3919 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003920 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003921
John McCallf1549f62010-07-06 01:34:17 +00003922 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003923 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3924 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003925 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003926
John McCallf1549f62010-07-06 01:34:17 +00003927 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3928 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003929
3930 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003931 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003932
Anders Carlsson80f25672008-09-09 17:59:25 +00003933 // Emit the @catch block.
3934 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003935
3936 // Collect any cleanups for the catch variable. The scope lasts until
3937 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003938 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003939
John McCallb6bbcc92010-10-15 04:57:14 +00003940 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003941 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003942
John McCallf1549f62010-07-06 01:34:17 +00003943 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003944 llvm::Value *Tmp =
3945 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003946 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003947 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003948
Anders Carlssondde0a942008-09-11 09:15:33 +00003949 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003950
3951 // We're done with the catch variable.
3952 CatchVarCleanups.ForceCleanup();
3953
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003954 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003955
Anders Carlsson80f25672008-09-09 17:59:25 +00003956 CGF.EmitBlock(NextCatchBlock);
3957 }
3958
John McCallf1549f62010-07-06 01:34:17 +00003959 CGF.ObjCEHValueStack.pop_back();
3960
John McCall0b251722010-08-04 05:59:32 +00003961 // If nothing wanted anything to do with the caught exception,
3962 // kill the extract call.
3963 if (Caught->use_empty())
3964 Caught->eraseFromParent();
3965
3966 if (!AllMatched)
3967 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3968
3969 if (HasFinally) {
3970 // Emit the exception handler for the @catch blocks.
3971 CGF.EmitBlock(CatchHandler);
3972
3973 // In theory we might now need a write hazard, but actually it's
3974 // unnecessary because there's no local-accessing code between
3975 // the try's write hazard and here.
3976 //Hazards.emitWriteHazard();
3977
John McCall9e2213d2010-10-04 23:42:51 +00003978 // Extract the new exception and save it to the
3979 // propagating-exception slot.
3980 assert(PropagatingExnVar);
3981 llvm::CallInst *NewCaught =
3982 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3983 ExceptionData, "caught");
3984 NewCaught->setDoesNotThrow();
3985 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3986
John McCall0b251722010-08-04 05:59:32 +00003987 // Don't pop the catch handler; the throw already did.
3988 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003989 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003990 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003991 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003992
John McCall87bb5822010-07-31 23:20:56 +00003993 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003994 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003995
John McCallf1549f62010-07-06 01:34:17 +00003996 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003997 CGF.Builder.restoreIP(TryFallthroughIP);
3998 if (CGF.HaveInsertPoint())
3999 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00004000 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00004001 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00004002
John McCallf1549f62010-07-06 01:34:17 +00004003 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00004004 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00004005 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00004006 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00004007 // If we have a propagating-exception variable, check it.
4008 llvm::Value *PropagatingExn;
4009 if (PropagatingExnVar) {
4010 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00004011
John McCall9e2213d2010-10-04 23:42:51 +00004012 // Otherwise, just look in the buffer for the exception to throw.
4013 } else {
4014 llvm::CallInst *Caught =
4015 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
4016 ExceptionData);
4017 Caught->setDoesNotThrow();
4018 PropagatingExn = Caught;
4019 }
4020
4021 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00004022 ->setDoesNotThrow();
4023 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00004024 }
Anders Carlsson80f25672008-09-09 17:59:25 +00004025
John McCall87bb5822010-07-31 23:20:56 +00004026 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004027}
4028
4029void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00004030 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004031 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004032
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004033 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00004034 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004035 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00004036 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004037 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004038 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00004039 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00004040 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00004041 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004042
John McCallf1549f62010-07-06 01:34:17 +00004043 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
4044 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00004045 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00004046
4047 // Clear the insertion point to indicate we are in unreachable code.
4048 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00004049}
4050
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004051/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004052/// object: objc_read_weak (id *src)
4053///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004054llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004055 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004056 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004057 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
4058 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
4059 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00004060 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004061 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00004062 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00004063 return read_weak;
4064}
4065
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004066/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
4067/// objc_assign_weak (id src, id *dst)
4068///
4069void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004070 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004071 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004072 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004073 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004074 assert(Size <= 8 && "does not support size > 8");
4075 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004076 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004077 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4078 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004079 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4080 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00004081 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00004082 src, dst, "weakassign");
4083 return;
4084}
4085
Fariborz Jahanian58626502008-11-19 00:59:10 +00004086/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
4087/// objc_assign_global (id src, id *dst)
4088///
4089void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004090 llvm::Value *src, llvm::Value *dst,
4091 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004092 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004093 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004094 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004095 assert(Size <= 8 && "does not support size > 8");
4096 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004097 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004098 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4099 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004100 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4101 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00004102 if (!threadlocal)
4103 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
4104 src, dst, "globalassign");
4105 else
4106 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
4107 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00004108 return;
4109}
4110
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004111/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004112/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004113///
4114void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004115 llvm::Value *src, llvm::Value *dst,
4116 llvm::Value *ivarOffset) {
4117 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00004118 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004119 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004120 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004121 assert(Size <= 8 && "does not support size > 8");
4122 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004123 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004124 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4125 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004126 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4127 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00004128 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
4129 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00004130 return;
4131}
4132
Fariborz Jahanian58626502008-11-19 00:59:10 +00004133/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
4134/// objc_assign_strongCast (id src, id *dst)
4135///
4136void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00004137 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00004138 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004139 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00004140 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004141 assert(Size <= 8 && "does not support size > 8");
4142 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004143 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00004144 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
4145 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00004146 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
4147 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00004148 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00004149 src, dst, "weakassign");
4150 return;
4151}
4152
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004153void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004154 llvm::Value *DestPtr,
4155 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004156 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004157 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
4158 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004159 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00004160 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00004161 return;
4162}
4163
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004164/// EmitObjCValueForIvar - Code Gen for ivar reference.
4165///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004166LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
4167 QualType ObjectTy,
4168 llvm::Value *BaseValue,
4169 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004170 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00004171 const ObjCInterfaceDecl *ID =
4172 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00004173 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4174 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00004175}
4176
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004177llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004178 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004179 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00004180 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004181 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
4183 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004184}
4185
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004186/* *** Private Interface *** */
4187
4188/// EmitImageInfo - Emit the image info marker used to encode some module
4189/// level information.
4190///
4191/// See: <rdr://4810609&4810587&4810587>
4192/// struct IMAGE_INFO {
4193/// unsigned version;
4194/// unsigned flags;
4195/// };
4196enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004197 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004198 eImageInfo_GarbageCollected = (1 << 1),
4199 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00004200 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
4201
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004202 // A flag indicating that the module has no instances of a @synthesize of a
4203 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00004204 eImageInfo_CorrectedSynthesize = (1 << 4),
4205 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004206};
4207
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004208void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004209 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00004210 const char *Section = (ObjCABI == 1) ?
4211 "__OBJC, __image_info,regular" :
4212 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004213
Bill Wendling3973acc2012-02-16 01:13:30 +00004214 // Generate module-level named metadata to convey this information to the
4215 // linker and code-gen.
4216 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004217
Bill Wendling3973acc2012-02-16 01:13:30 +00004218 // Add the ObjC ABI version to the module flags.
4219 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
4220 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
4221 version);
4222 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
4223 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004224
David Blaikie4e4d0842012-03-11 07:00:24 +00004225 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004226 // Non-GC overrides those files which specify GC.
4227 Mod.addModuleFlag(llvm::Module::Override,
4228 "Objective-C Garbage Collection", (uint32_t)0);
4229 } else {
4230 // Add the ObjC garbage collection value.
4231 Mod.addModuleFlag(llvm::Module::Error,
4232 "Objective-C Garbage Collection",
4233 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004234
David Blaikie4e4d0842012-03-11 07:00:24 +00004235 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00004236 // Add the ObjC GC Only value.
4237 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
4238 eImageInfo_GCOnly);
4239
4240 // Require that GC be specified and set to eImageInfo_GarbageCollected.
4241 llvm::Value *Ops[2] = {
4242 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
4243 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
4244 eImageInfo_GarbageCollected)
4245 };
4246 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
4247 llvm::MDNode::get(VMContext, Ops));
4248 }
4249 }
Bill Wendling09822512012-04-24 11:04:57 +00004250
4251 // Indicate whether we're compiling this to run on a simulator.
4252 const llvm::Triple &Triple = CGM.getTarget().getTriple();
4253 if (Triple.getOS() == llvm::Triple::IOS &&
4254 (Triple.getArch() == llvm::Triple::x86 ||
4255 Triple.getArch() == llvm::Triple::x86_64))
4256 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
4257 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004258}
4259
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004260// struct objc_module {
4261// unsigned long version;
4262// unsigned long size;
4263// const char *name;
4264// Symtab symtab;
4265// };
4266
4267// FIXME: Get from somewhere
4268static const int ModuleVersion = 7;
4269
4270void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00004271 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004272
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004273 llvm::Constant *Values[] = {
4274 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
4275 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
4276 // This used to be the filename, now it is unused. <rdr://4327263>
4277 GetClassName(&CGM.getContext().Idents.get("")),
4278 EmitModuleSymbols()
4279 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004280 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00004281 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004282 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00004283 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004284}
4285
4286llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004287 unsigned NumClasses = DefinedClasses.size();
4288 unsigned NumCategories = DefinedCategories.size();
4289
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004290 // Return null if no symbols were defined.
4291 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004292 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004293
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004294 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004295 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004296 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004297 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
4298 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004299
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004300 // The runtime expects exactly the list of defined classes followed
4301 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00004302 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004303 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004304 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004305 ObjCTypes.Int8PtrTy);
4306 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004307 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004308 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004309 ObjCTypes.Int8PtrTy);
4310
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004311 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004312 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004313 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004314 Symbols);
4315
Chris Lattnerc5cbb902011-06-20 04:01:35 +00004316 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004317
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004318 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004319 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
4320 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004321 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004322 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323}
4324
John McCallf85e1932011-06-15 23:02:42 +00004325llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
4326 IdentifierInfo *II) {
4327 LazySymbols.insert(II);
4328
4329 llvm::GlobalVariable *&Entry = ClassReferences[II];
4330
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004331 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004332 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00004333 llvm::ConstantExpr::getBitCast(GetClassName(II),
4334 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004335 Entry =
John McCallf85e1932011-06-15 23:02:42 +00004336 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
4337 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
4338 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004339 }
John McCallf85e1932011-06-15 23:02:42 +00004340
Benjamin Kramer578faa82011-09-27 21:06:10 +00004341 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004342}
4343
John McCallf85e1932011-06-15 23:02:42 +00004344llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
4345 const ObjCInterfaceDecl *ID) {
4346 return EmitClassRefFromId(Builder, ID->getIdentifier());
4347}
4348
4349llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
4350 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
4351 return EmitClassRefFromId(Builder, II);
4352}
4353
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004354llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
4355 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004356 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004358 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004359 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004360 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004361 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004362 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004363 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
4364 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004365 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004366 }
4367
Fariborz Jahanian03b29602010-06-17 19:56:20 +00004368 if (lvalue)
4369 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00004370 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004371}
4372
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004373llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004374 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004375
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004376 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004377 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004378 llvm::ConstantDataArray::getString(VMContext,
4379 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004380 ((ObjCABI == 2) ?
4381 "__TEXT,__objc_classname,cstring_literals" :
4382 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004383 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004384
Owen Andersona1cf15f2009-07-14 23:10:40 +00004385 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004386}
4387
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004388llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
4389 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
4390 I = MethodDefinitions.find(MD);
4391 if (I != MethodDefinitions.end())
4392 return I->second;
4393
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00004394 return NULL;
4395}
4396
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004397/// GetIvarLayoutName - Returns a unique constant for the given
4398/// ivar layout bitmap.
4399llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004400 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004401 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00004402}
4403
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004404void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004405 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004406 bool ForStrongLayout,
4407 bool &HasUnion) {
4408 const RecordDecl *RD = RT->getDecl();
4409 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00004410 SmallVector<const FieldDecl*, 16> Fields;
4411 for (RecordDecl::field_iterator i = RD->field_begin(),
4412 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00004413 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00004414 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004415 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00004416 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004417
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004418 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
4419 ForStrongLayout, HasUnion);
4420}
4421
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00004422void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004423 const llvm::StructLayout *Layout,
4424 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00004425 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004426 unsigned int BytePos, bool ForStrongLayout,
4427 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004428 bool IsUnion = (RD && RD->isUnion());
4429 uint64_t MaxUnionIvarSize = 0;
4430 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00004431 const FieldDecl *MaxField = 0;
4432 const FieldDecl *MaxSkippedField = 0;
4433 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004434 uint64_t MaxFieldOffset = 0;
4435 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004436 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00004437 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004439 if (RecFields.empty())
4440 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00004441 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
4442 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00004443 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004444 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00004445 FirstFieldDelta =
4446 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
4447 }
4448
Chris Lattnerf1690852009-03-31 08:48:01 +00004449 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004450 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00004451 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004452 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00004453 // Note that 'i' here is actually the field index inside RD of Field,
4454 // although this dependency is hidden.
4455 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00004456 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00004457 } else
John McCallf85e1932011-06-15 23:02:42 +00004458 FieldOffset =
4459 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004460
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004461 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004462 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004463 LastFieldBitfieldOrUnnamed = Field;
4464 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004465 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004466 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004467
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004468 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004469 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004470 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004471 if (FQT->isUnionType())
4472 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004473
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004474 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004475 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004476 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004477 continue;
4478 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004479
Chris Lattnerf1690852009-03-31 08:48:01 +00004480 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004481 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004482 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004483 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004484 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004485 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004486 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
4487 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004488 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004489 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004490 FQT = CArray->getElementType();
4491 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
4493 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004494 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00004495 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004496 int OldIndex = IvarsInfo.size() - 1;
4497 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498
Ted Kremenek6217b802009-07-29 21:53:49 +00004499 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004500 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004501 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004502
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004503 // Replicate layout information for each array element. Note that
4504 // one element is already done.
4505 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004506 for (int FirstIndex = IvarsInfo.size() - 1,
4507 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004508 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004509 for (int i = OldIndex+1; i <= FirstIndex; ++i)
4510 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
4511 IvarsInfo[i].ivar_size));
4512 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
4513 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
4514 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004515 }
4516 continue;
4517 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00004518 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004519 // At this point, we are done with Record/Union and array there of.
4520 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00004521 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00004522
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004523 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00004524 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
4525 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004526 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004527 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004528 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004529 MaxUnionIvarSize = UnionIvarSize;
4530 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004531 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004532 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004533 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004534 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004535 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004536 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004537 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00004538 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
4539 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00004540 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004541 // FIXME: Why the asymmetry? We divide by word size in bits on other
4542 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004543 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00004544 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004545 MaxSkippedUnionIvarSize = UnionIvarSize;
4546 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00004547 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004548 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00004549 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004550 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00004551 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004552 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00004553 }
4554 }
4555 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00004556
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004557 if (LastFieldBitfieldOrUnnamed) {
4558 if (LastFieldBitfieldOrUnnamed->isBitField()) {
4559 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00004560 uint64_t BitFieldSize
4561 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00004562 GC_IVAR skivar;
4563 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
4564 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
4565 + ((BitFieldSize % ByteSizeInBits) != 0);
4566 SkipIvars.push_back(skivar);
4567 } else {
4568 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
4569 // Last field was unnamed. Must update skip info.
4570 unsigned FieldSize
4571 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
4572 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
4573 FieldSize / ByteSizeInBits));
4574 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00004575 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004576
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004577 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004578 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004579 MaxUnionIvarSize));
4580 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00004581 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00004582 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004583}
4584
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004585/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4586/// the computations and returning the layout bitmap (for ivar or blocks) in
4587/// the given argument BitMap string container. Routine reads
4588/// two containers, IvarsInfo and SkipIvars which are assumed to be
4589/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004590llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004591 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004592 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004593
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004594 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004595 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596 unsigned int WordSize =
Micah Villmow25a6a842012-10-08 16:25:52 +00004597 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004598 if (IvarsInfo[0].ivar_bytepos == 0) {
4599 WordsToSkip = 0;
4600 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004601 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004602 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4603 WordsToScan = IvarsInfo[0].ivar_size;
4604 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004605 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004606 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004607 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004608 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004609 // consecutive 'scanned' object pointers.
4610 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004611 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004612 // Skip over 'gc'able object pointer which lay over each other.
4613 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4614 continue;
4615 // Must skip over 1 or more words. We save current skip/scan values
4616 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004617 SKIP_SCAN SkScan;
4618 SkScan.skip = WordsToSkip;
4619 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004620 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004621
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004622 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004623 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4624 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004625 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004626 WordsToSkip = 0;
4627 WordsToScan = IvarsInfo[i].ivar_size;
4628 }
4629 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004630 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004631 SKIP_SCAN SkScan;
4632 SkScan.skip = WordsToSkip;
4633 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004634 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004635 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004636
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004637 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004638 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004639 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004640 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004641 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004642 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004643 IvarsInfo[LastIndex].ivar_bytepos +
4644 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004645 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004646 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004647 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004648 SKIP_SCAN SkScan;
4649 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4650 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004651 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004652 }
4653 }
4654 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4655 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004656 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004657 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004658 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4659 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4660 // 0xM0 followed by 0x0N detected.
4661 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4662 for (int j = i+1; j < SkipScan; j++)
4663 SkipScanIvars[j] = SkipScanIvars[j+1];
4664 --SkipScan;
4665 }
4666 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004667
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004668 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004669 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004670 unsigned char byte;
4671 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4672 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4673 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4674 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004675
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004676 // first skip big.
4677 for (unsigned int ix = 0; ix < skip_big; ix++)
4678 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004679
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004680 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004681 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004682 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004683 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004684 byte |= 0xf;
4685 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004686 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004687 byte |= scan_small;
4688 scan_small = 0;
4689 }
4690 BitMap += byte;
4691 }
4692 // next scan big
4693 for (unsigned int ix = 0; ix < scan_big; ix++)
4694 BitMap += (unsigned char)(0x0f);
4695 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004696 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004697 byte = scan_small;
4698 BitMap += byte;
4699 }
4700 }
4701 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004702 unsigned char zero = 0;
4703 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004704
4705 llvm::GlobalVariable * Entry =
4706 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004707 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004708 ((ObjCABI == 2) ?
4709 "__TEXT,__objc_classname,cstring_literals" :
4710 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004711 1, true);
4712 return getConstantGEP(VMContext, Entry, 0, 0);
4713}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004714
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004715/// BuildIvarLayout - Builds ivar layout bitmap for the class
4716/// implementation for the __strong or __weak case.
4717/// The layout map displays which words in ivar list must be skipped
4718/// and which must be scanned by GC (see below). String is built of bytes.
4719/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4720/// of words to skip and right nibble is count of words to scan. So, each
4721/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4722/// represented by a 0x00 byte which also ends the string.
4723/// 1. when ForStrongLayout is true, following ivars are scanned:
4724/// - id, Class
4725/// - object *
4726/// - __strong anything
4727///
4728/// 2. When ForStrongLayout is false, following ivars are scanned:
4729/// - __weak anything
4730///
4731llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4732 const ObjCImplementationDecl *OMD,
4733 bool ForStrongLayout) {
4734 bool hasUnion = false;
4735
Chris Lattner8b418682012-02-07 00:39:47 +00004736 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004737 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4738 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004739 return llvm::Constant::getNullValue(PtrTy);
4740
Jordy Rosedb8264e2011-07-22 02:08:32 +00004741 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4742 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004743 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004744 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004745 IVD; IVD = IVD->getNextIvar())
4746 RecFields.push_back(cast<FieldDecl>(IVD));
4747 }
4748 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004749 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004750 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004751
Jordy Rosedb8264e2011-07-22 02:08:32 +00004752 // FIXME: This is not ideal; we shouldn't have to do this copy.
4753 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004754 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004755
4756 if (RecFields.empty())
4757 return llvm::Constant::getNullValue(PtrTy);
4758
4759 SkipIvars.clear();
4760 IvarsInfo.clear();
4761
4762 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4763 if (IvarsInfo.empty())
4764 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004765 // Sort on byte position in case we encounterred a union nested in
4766 // the ivar list.
4767 if (hasUnion && !IvarsInfo.empty())
4768 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4769 if (hasUnion && !SkipIvars.empty())
4770 std::sort(SkipIvars.begin(), SkipIvars.end());
4771
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004772 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004773 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004774
David Blaikie4e4d0842012-03-11 07:00:24 +00004775 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004776 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004777 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004778 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004779 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004780 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004781 if (!(s[i] & 0xf0))
4782 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4783 else
4784 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4785 printf("\n");
4786 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004787 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004788}
4789
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004790llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004791 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4792
Chris Lattner0b239712012-02-06 22:16:34 +00004793 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004794 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004795 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004796 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004797 ((ObjCABI == 2) ?
4798 "__TEXT,__objc_methname,cstring_literals" :
4799 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004800 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004801
Owen Andersona1cf15f2009-07-14 23:10:40 +00004802 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004803}
4804
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004805// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004806llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004807 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4808}
4809
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004810llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004811 std::string TypeStr;
4812 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4813
4814 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004815
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004816 if (!Entry)
4817 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004818 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004819 ((ObjCABI == 2) ?
4820 "__TEXT,__objc_methtype,cstring_literals" :
4821 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004822 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004823
Owen Andersona1cf15f2009-07-14 23:10:40 +00004824 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004825}
4826
Bob Wilsondc8dab62011-11-30 01:57:58 +00004827llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4828 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004829 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004830 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004831 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004832
4833 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4834
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004835 if (!Entry)
4836 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004837 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004838 ((ObjCABI == 2) ?
4839 "__TEXT,__objc_methtype,cstring_literals" :
4840 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004841 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004842
Owen Andersona1cf15f2009-07-14 23:10:40 +00004843 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004844}
4845
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004846// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004847llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004848 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004849
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004850 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004852 llvm::ConstantDataArray::getString(VMContext,
4853 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004854 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004855 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004856
Owen Andersona1cf15f2009-07-14 23:10:40 +00004857 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004858}
4859
4860// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004861// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004862llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004863CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4864 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004865 std::string TypeStr;
4866 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004867 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4868}
4869
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004870void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004871 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004872 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004873 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004874 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004875 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4876 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004878 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004879 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004880 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004881}
4882
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004883void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004884 EmitModuleInfo();
4885
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004886 // Emit the dummy bodies for any protocols which were referenced but
4887 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004888 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004889 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4890 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004891 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004892
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004893 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004894 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004895 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004896 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004897 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004898 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004899 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004900 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004901 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004902 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004903 }
4904
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004905 // Add assembler directives to add lazy undefined symbol references
4906 // for classes which are referenced but not defined. This is
4907 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004908 //
4909 // FIXME: It would be nice if we had an LLVM construct for this.
4910 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004911 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004912 Asm += CGM.getModule().getModuleInlineAsm();
4913 if (!Asm.empty() && Asm.back() != '\n')
4914 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004915
Daniel Dunbar33063492009-09-07 00:20:42 +00004916 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004917 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4918 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004919 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4920 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004921 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004922 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004923 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004924 }
4925
Bill Wendling13562a12012-02-07 09:06:01 +00004926 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004927 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4928 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4929 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004930
Daniel Dunbar33063492009-09-07 00:20:42 +00004931 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004932 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004933}
4934
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004935CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004936 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004937 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004938 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004939 ObjCABI = 2;
4940}
4941
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004942/* *** */
4943
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004944ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004945 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4946{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004947 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4948 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004949
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004950 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004951 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004952 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004953 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004954 Int8PtrTy = CGM.Int8PtrTy;
4955 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004957 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004958 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004959 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004960
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004961 // I'm not sure I like this. The implicit coordination is a bit
4962 // gross. We should solve this in a reasonable fashion because this
4963 // is a pretty common task (match some runtime data structure with
4964 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004965
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004966 // FIXME: This is leaked.
4967 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004969 // struct _objc_super {
4970 // id self;
4971 // Class cls;
4972 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004973 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004974 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004975 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004976 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004977 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004978 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004979 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004980 Ctx.getObjCClassType(), 0, 0, false,
4981 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004982 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004983
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004984 SuperCTy = Ctx.getTagDeclType(RD);
4985 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004987 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004988 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4989
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004990 // struct _prop_t {
4991 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004992 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004993 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004994 PropertyTy = llvm::StructType::create("struct._prop_t",
4995 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004996
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004997 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004998 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004999 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005000 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005001 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005002 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005003 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
5004 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005005 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005006 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005007
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005008 // struct _objc_method {
5009 // SEL _cmd;
5010 // char *method_type;
5011 // char *_imp;
5012 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005013 MethodTy = llvm::StructType::create("struct._objc_method",
5014 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
5015 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005016
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005017 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00005018 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005019 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005020
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005021}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00005022
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005023ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005024 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005025 // struct _objc_method_description {
5026 // SEL name;
5027 // char *types;
5028 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005030 llvm::StructType::create("struct._objc_method_description",
5031 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005032
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005033 // struct _objc_method_description_list {
5034 // int count;
5035 // struct _objc_method_description[1];
5036 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005037 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005038 llvm::StructType::create("struct._objc_method_description_list",
5039 IntTy,
5040 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005041
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005042 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005043 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005044 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005045
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005046 // Protocol description structures
5047
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005048 // struct _objc_protocol_extension {
5049 // uint32_t size; // sizeof(struct _objc_protocol_extension)
5050 // struct _objc_method_description_list *optional_instance_methods;
5051 // struct _objc_method_description_list *optional_class_methods;
5052 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005053 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005054 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005055 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005056 llvm::StructType::create("struct._objc_protocol_extension",
5057 IntTy, MethodDescriptionListPtrTy,
5058 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005059 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005060
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005061 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005062 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005063
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00005064 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005065
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005066 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005067 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005068
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005069 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005070 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005071 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005072 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005073 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005074 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005075
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005076 // struct _objc_protocol {
5077 // struct _objc_protocol_extension *isa;
5078 // char *protocol_name;
5079 // struct _objc_protocol **_objc_protocol_list;
5080 // struct _objc_method_description_list *instance_methods;
5081 // struct _objc_method_description_list *class_methods;
5082 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005083 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
5084 llvm::PointerType::getUnqual(ProtocolListTy),
5085 MethodDescriptionListPtrTy,
5086 MethodDescriptionListPtrTy,
5087 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005088
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005089 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005090 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005091
Owen Anderson96e0fc72009-07-29 22:16:19 +00005092 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005093
5094 // Class description structures
5095
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005096 // struct _objc_ivar {
5097 // char *ivar_name;
5098 // char *ivar_type;
5099 // int ivar_offset;
5100 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005101 IvarTy = llvm::StructType::create("struct._objc_ivar",
5102 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005103
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005104 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005105 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005106 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005107 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005108
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005109 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005110 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005111 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00005112 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005113
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005114 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005115 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005116 llvm::StructType::create("struct._objc_class_extension",
5117 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005118 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005119
Chris Lattnerc1c20112011-08-12 17:43:31 +00005120 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005121
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005122 // struct _objc_class {
5123 // Class isa;
5124 // Class super_class;
5125 // char *name;
5126 // long version;
5127 // long info;
5128 // long instance_size;
5129 // struct _objc_ivar_list *ivars;
5130 // struct _objc_method_list *methods;
5131 // struct _objc_cache *cache;
5132 // struct _objc_protocol_list *protocols;
5133 // char *ivar_layout;
5134 // struct _objc_class_ext *ext;
5135 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005136 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
5137 llvm::PointerType::getUnqual(ClassTy),
5138 Int8PtrTy,
5139 LongTy,
5140 LongTy,
5141 LongTy,
5142 IvarListPtrTy,
5143 MethodListPtrTy,
5144 CachePtrTy,
5145 ProtocolListPtrTy,
5146 Int8PtrTy,
5147 ClassExtensionPtrTy,
5148 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005149
Owen Anderson96e0fc72009-07-29 22:16:19 +00005150 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005151
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005152 // struct _objc_category {
5153 // char *category_name;
5154 // char *class_name;
5155 // struct _objc_method_list *instance_method;
5156 // struct _objc_method_list *class_method;
5157 // uint32_t size; // sizeof(struct _objc_category)
5158 // struct _objc_property_list *instance_properties;// category's @property
5159 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005160 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005161 llvm::StructType::create("struct._objc_category",
5162 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
5163 MethodListPtrTy, ProtocolListPtrTy,
5164 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00005165
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005166 // Global metadata structures
5167
Fariborz Jahanian10a42312009-01-21 00:39:53 +00005168 // struct _objc_symtab {
5169 // long sel_ref_cnt;
5170 // SEL *refs;
5171 // short cls_def_cnt;
5172 // short cat_def_cnt;
5173 // char *defs[cls_def_cnt + cat_def_cnt];
5174 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005175 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005176 llvm::StructType::create("struct._objc_symtab",
5177 LongTy, SelectorPtrTy, ShortTy, ShortTy,
5178 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005179 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00005180
Fariborz Jahaniandb286862009-01-22 00:37:21 +00005181 // struct _objc_module {
5182 // long version;
5183 // long size; // sizeof(struct _objc_module)
5184 // char *name;
5185 // struct _objc_symtab* symtab;
5186 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005187 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005188 llvm::StructType::create("struct._objc_module",
5189 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00005190
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005191
Mike Stumpf5408fe2009-05-16 07:57:57 +00005192 // FIXME: This is the size of the setjmp buffer and should be target
5193 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00005194 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005195
Anders Carlsson124526b2008-09-09 10:10:21 +00005196 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00005197 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005198
5199 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005200 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00005201 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
5202 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00005203
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005204}
5205
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005206ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00005207 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005208 // struct _method_list_t {
5209 // uint32_t entsize; // sizeof(struct _objc_method)
5210 // uint32_t method_count;
5211 // struct _objc_method method_list[method_count];
5212 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005213 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005214 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
5215 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005216 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005217 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005218
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005219 // struct _protocol_t {
5220 // id isa; // NULL
5221 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005222 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005223 // const struct method_list_t * const instance_methods;
5224 // const struct method_list_t * const class_methods;
5225 // const struct method_list_t *optionalInstanceMethods;
5226 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005227 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005228 // const uint32_t size; // sizeof(struct _protocol_t)
5229 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005230 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005231 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005232
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005233 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005234 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005235 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005236
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005237 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005238 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
5239 llvm::PointerType::getUnqual(ProtocolListnfABITy),
5240 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
5241 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00005242 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
5243 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00005244
5245 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005246 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247
Fariborz Jahanianda320092009-01-29 19:24:30 +00005248 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005249 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00005250 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005251 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005252 ProtocolListnfABITy->setBody(LongTy,
5253 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
5254 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005255
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005256 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005257 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005258
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005259 // struct _ivar_t {
5260 // unsigned long int *offset; // pointer to ivar offset location
5261 // char *name;
5262 // char *type;
5263 // uint32_t alignment;
5264 // uint32_t size;
5265 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005266 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005267 llvm::StructType::create("struct._ivar_t",
5268 llvm::PointerType::getUnqual(LongTy),
5269 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005270
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005271 // struct _ivar_list_t {
5272 // uint32 entsize; // sizeof(struct _ivar_t)
5273 // uint32 count;
5274 // struct _iver_t list[count];
5275 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005276 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005277 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
5278 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005279
Owen Anderson96e0fc72009-07-29 22:16:19 +00005280 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005281
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005282 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005283 // uint32_t const flags;
5284 // uint32_t const instanceStart;
5285 // uint32_t const instanceSize;
5286 // uint32_t const reserved; // only when building for 64bit targets
5287 // const uint8_t * const ivarLayout;
5288 // const char *const name;
5289 // const struct _method_list_t * const baseMethods;
5290 // const struct _objc_protocol_list *const baseProtocols;
5291 // const struct _ivar_list_t *const ivars;
5292 // const uint8_t * const weakIvarLayout;
5293 // const struct _prop_list_t * const properties;
5294 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005295
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005296 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00005297 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
5298 IntTy, IntTy, IntTy, Int8PtrTy,
5299 Int8PtrTy, MethodListnfABIPtrTy,
5300 ProtocolListnfABIPtrTy,
5301 IvarListnfABIPtrTy,
5302 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005304 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005305 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00005306 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
5307 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005308
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005309 // struct _class_t {
5310 // struct _class_t *isa;
5311 // struct _class_t * const superclass;
5312 // void *cache;
5313 // IMP *vtable;
5314 // struct class_ro_t *ro;
5315 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316
Chris Lattnerc1c20112011-08-12 17:43:31 +00005317 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005318 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
5319 llvm::PointerType::getUnqual(ClassnfABITy),
5320 CachePtrTy,
5321 llvm::PointerType::getUnqual(ImpnfABITy),
5322 llvm::PointerType::getUnqual(ClassRonfABITy),
5323 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005324
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005325 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00005326 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005327
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00005328 // struct _category_t {
5329 // const char * const name;
5330 // struct _class_t *const cls;
5331 // const struct _method_list_t * const instance_methods;
5332 // const struct _method_list_t * const class_methods;
5333 // const struct _protocol_list_t * const protocols;
5334 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00005335 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00005336 CategorynfABITy = llvm::StructType::create("struct._category_t",
5337 Int8PtrTy, ClassnfABIPtrTy,
5338 MethodListnfABIPtrTy,
5339 MethodListnfABIPtrTy,
5340 ProtocolListnfABIPtrTy,
5341 PropertyListPtrTy,
5342 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005344 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005345 CodeGen::CodeGenTypes &Types = CGM.getTypes();
5346 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005348 // MessageRefTy - LLVM for:
5349 // struct _message_ref_t {
5350 // IMP messenger;
5351 // SEL name;
5352 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005353
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005354 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005355 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00005356 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005357 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005358 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005359 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005360 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00005361 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00005362 Ctx.getObjCSelType(), 0, 0, false,
5363 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00005364 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005366 MessageRefCTy = Ctx.getTagDeclType(RD);
5367 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
5368 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005370 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00005371 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005373 // SuperMessageRefTy - LLVM for:
5374 // struct _super_message_ref_t {
5375 // SUPER_IMP messenger;
5376 // SEL name;
5377 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005378 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005379 llvm::StructType::create("struct._super_message_ref_t",
5380 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005381
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00005382 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005383 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00005384
Daniel Dunbare588b992009-03-01 04:46:24 +00005385
5386 // struct objc_typeinfo {
5387 // const void** vtable; // objc_ehtype_vtable + 2
5388 // const char* name; // c++ typeinfo string
5389 // Class cls;
5390 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00005391 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00005392 llvm::StructType::create("struct._objc_typeinfo",
5393 llvm::PointerType::getUnqual(Int8PtrTy),
5394 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00005395 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005396}
5397
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005398llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005399 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005400
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005401 return NULL;
5402}
5403
Bill Wendlingbb028552012-02-07 09:25:09 +00005404void CGObjCNonFragileABIMac::
5405AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
5406 const char *SymbolName,
5407 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00005408 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409
Daniel Dunbar463b8762009-05-15 21:48:48 +00005410 if (!NumClasses)
5411 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005412
Chris Lattner0b239712012-02-06 22:16:34 +00005413 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005414 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00005415 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00005416 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00005417 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00005418 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00005419 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00005420 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005421
Daniel Dunbar463b8762009-05-15 21:48:48 +00005422 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005423 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005424 llvm::GlobalValue::InternalLinkage,
5425 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005426 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00005427 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00005428 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00005429 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00005430}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005431
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005432void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
5433 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005434
Daniel Dunbar463b8762009-05-15 21:48:48 +00005435 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005436 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005437 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005438 "\01L_OBJC_LABEL_CLASS_$",
5439 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005440
Bill Wendling13562a12012-02-07 09:06:01 +00005441 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005442 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
5443 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5444 continue;
5445 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005446 }
5447
Bill Wendling13562a12012-02-07 09:06:01 +00005448 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005449 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
5450 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
5451 continue;
5452 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
5453 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005454
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005456 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
5457 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005458
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005459 // Build list of all implemented category addresses in array
5460 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00005462 "\01L_OBJC_LABEL_CATEGORY_$",
5463 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005464 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005465 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
5466 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467
Daniel Dunbarfce176b2010-04-25 20:39:01 +00005468 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005469}
5470
John McCall944c8432011-05-14 03:10:52 +00005471/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
5472/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005474/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00005475bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
5476 // At various points we've experimented with using vtable-based
5477 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005478 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005479 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00005480 return false;
John McCall944c8432011-05-14 03:10:52 +00005481 case CodeGenOptions::NonLegacy:
5482 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005483 case CodeGenOptions::Mixed:
5484 break;
5485 }
5486
5487 // If so, see whether this selector is in the white-list of things which must
5488 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00005489 if (VTableDispatchMethods.empty()) {
5490 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
5491 VTableDispatchMethods.insert(GetNullarySelector("class"));
5492 VTableDispatchMethods.insert(GetNullarySelector("self"));
5493 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
5494 VTableDispatchMethods.insert(GetNullarySelector("length"));
5495 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005496
John McCall944c8432011-05-14 03:10:52 +00005497 // These are vtable-based if GC is disabled.
5498 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005499 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00005500 VTableDispatchMethods.insert(GetNullarySelector("retain"));
5501 VTableDispatchMethods.insert(GetNullarySelector("release"));
5502 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
5503 }
5504
5505 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
5506 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
5507 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
5508 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
5509 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
5510 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
5511 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
5512
5513 // These are vtable-based if GC is enabled.
5514 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00005515 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00005516 VTableDispatchMethods.insert(GetNullarySelector("hash"));
5517 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
5518
5519 // "countByEnumeratingWithState:objects:count"
5520 IdentifierInfo *KeyIdents[] = {
5521 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
5522 &CGM.getContext().Idents.get("objects"),
5523 &CGM.getContext().Idents.get("count")
5524 };
5525 VTableDispatchMethods.insert(
5526 CGM.getContext().Selectors.getSelector(3, KeyIdents));
5527 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005528 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00005529
John McCall944c8432011-05-14 03:10:52 +00005530 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005531}
5532
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005533/// BuildClassRoTInitializer - generate meta-data for:
5534/// struct _class_ro_t {
5535/// uint32_t const flags;
5536/// uint32_t const instanceStart;
5537/// uint32_t const instanceSize;
5538/// uint32_t const reserved; // only when building for 64bit targets
5539/// const uint8_t * const ivarLayout;
5540/// const char *const name;
5541/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005542/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005543/// const struct _ivar_list_t *const ivars;
5544/// const uint8_t * const weakIvarLayout;
5545/// const struct _prop_list_t * const properties;
5546/// }
5547///
5548llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005549 unsigned flags,
5550 unsigned InstanceStart,
5551 unsigned InstanceSize,
5552 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005553 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005554 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00005555
David Blaikie4e4d0842012-03-11 07:00:24 +00005556 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall621915c2012-10-17 04:53:23 +00005557 flags |= NonFragileABI_Class_CompiledByARC;
John McCallf85e1932011-06-15 23:02:42 +00005558
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005559 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
5560 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
5561 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005562 // FIXME. For 64bit targets add 0 here.
John McCall621915c2012-10-17 04:53:23 +00005563 Values[ 3] = (flags & NonFragileABI_Class_Meta)
5564 ? GetIvarLayoutName(0, ObjCTypes)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005565 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005566 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005567 // const struct _method_list_t * const baseMethods;
5568 std::vector<llvm::Constant*> Methods;
5569 std::string MethodListName("\01l_OBJC_$_");
John McCall621915c2012-10-17 04:53:23 +00005570 if (flags & NonFragileABI_Class_Meta) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005571 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005572 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005573 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005574 // Class methods should always be defined.
5575 Methods.push_back(GetMethodConstant(*i));
5576 }
5577 } else {
5578 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005579 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005580 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005581 // Instance methods should always be defined.
5582 Methods.push_back(GetMethodConstant(*i));
5583 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005584 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005585 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005586 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005587
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005588 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5589 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005590
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005591 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5592 if (llvm::Constant *C = GetMethodConstant(MD))
5593 Methods.push_back(C);
5594 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5595 if (llvm::Constant *C = GetMethodConstant(MD))
5596 Methods.push_back(C);
5597 }
5598 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005599 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600 Values[ 5] = EmitMethodList(MethodListName,
5601 "__DATA, __objc_const", Methods);
5602
Fariborz Jahanianda320092009-01-29 19:24:30 +00005603 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5604 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005605 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005606 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005607 OID->all_referenced_protocol_begin(),
5608 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005609
John McCall621915c2012-10-17 04:53:23 +00005610 if (flags & NonFragileABI_Class_Meta) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005611 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005612 Values[ 8] = GetIvarLayoutName(0, ObjCTypes);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005613 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
John McCall621915c2012-10-17 04:53:23 +00005614 } else {
5615 Values[ 7] = EmitIvarList(ID);
5616 Values[ 8] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005617 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5618 ID, ID->getClassInterface(), ObjCTypes);
John McCall621915c2012-10-17 04:53:23 +00005619 }
Owen Anderson08e25242009-07-27 22:29:56 +00005620 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005621 Values);
5622 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005623 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5624 llvm::GlobalValue::InternalLinkage,
5625 Init,
John McCall621915c2012-10-17 04:53:23 +00005626 (flags & NonFragileABI_Class_Meta) ?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005627 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5628 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005629 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005630 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005631 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005632 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005633
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005634}
5635
5636/// BuildClassMetaData - This routine defines that to-level meta-data
5637/// for the given ClassName for:
5638/// struct _class_t {
5639/// struct _class_t *isa;
5640/// struct _class_t * const superclass;
5641/// void *cache;
5642/// IMP *vtable;
5643/// struct class_ro_t *ro;
5644/// }
5645///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005646llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005647 std::string &ClassName,
5648 llvm::Constant *IsAGV,
5649 llvm::Constant *SuperClassGV,
5650 llvm::Constant *ClassRoGV,
5651 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005652 llvm::Constant *Values[] = {
5653 IsAGV,
5654 SuperClassGV,
5655 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5656 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5657 ClassRoGV // &CLASS_RO_GV
5658 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659 if (!Values[1])
5660 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005661 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005662 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005663 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5664 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005665 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005666 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005667 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005668 if (HiddenVisibility)
5669 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005670 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005671}
5672
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005673bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005674CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005675 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005676}
5677
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005678void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005679 uint32_t &InstanceStart,
5680 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005681 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005682 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005683
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005684 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005685 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005686
5687 // If there are no fields, the start is the same as the end.
5688 if (!RL.getFieldCount())
5689 InstanceStart = InstanceSize;
5690 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005691 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005692}
5693
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005694void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5695 std::string ClassName = ID->getNameAsString();
5696 if (!ObjCEmptyCacheVar) {
5697 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005698 CGM.getModule(),
5699 ObjCTypes.CacheTy,
5700 false,
5701 llvm::GlobalValue::ExternalLinkage,
5702 0,
5703 "_objc_empty_cache");
5704
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005705 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706 CGM.getModule(),
5707 ObjCTypes.ImpnfABITy,
5708 false,
5709 llvm::GlobalValue::ExternalLinkage,
5710 0,
5711 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005712 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005713 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005714 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005715 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005717 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005718 uint32_t InstanceSize = InstanceStart;
John McCall621915c2012-10-17 04:53:23 +00005719 uint32_t flags = NonFragileABI_Class_Meta;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005720 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5721 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005722
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005723 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005724
John McCallb03527a2012-10-17 04:53:31 +00005725 // Build the flags for the metaclass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005727 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005728 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005729 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005730
5731 // FIXME: why is this flag set on the metaclass?
5732 // ObjC metaclasses have no fields and don't really get constructed.
5733 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005734 flags |= NonFragileABI_Class_HasCXXStructors;
John McCallb03527a2012-10-17 04:53:31 +00005735 if (!ID->hasNonZeroConstructors())
5736 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5737 }
5738
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005739 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005740 // class is root
John McCall621915c2012-10-17 04:53:23 +00005741 flags |= NonFragileABI_Class_Root;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005742 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005743 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005744 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005745 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005746 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5747 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5748 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005749 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005750 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005751 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005752 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005753 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005754 ObjCMetaClassName +
5755 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005756 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005757 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005758 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005759 }
5760 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5761 InstanceStart,
5762 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005763 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005764 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005765 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5766 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005767 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005768
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005769 // Metadata for the class
John McCall621915c2012-10-17 04:53:23 +00005770 flags = 0;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005771 if (classIsHidden)
John McCall621915c2012-10-17 04:53:23 +00005772 flags |= NonFragileABI_Class_Hidden;
John McCallb03527a2012-10-17 04:53:31 +00005773
5774 if (ID->hasNonZeroConstructors() || ID->hasDestructors()) {
John McCall621915c2012-10-17 04:53:23 +00005775 flags |= NonFragileABI_Class_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005776
John McCallb03527a2012-10-17 04:53:31 +00005777 // Set a flag to enable a runtime optimization when a class has
5778 // fields that require destruction but which don't require
5779 // anything except zero-initialization during construction. This
5780 // is most notably true of __strong and __weak types, but you can
5781 // also imagine there being C++ types with non-trivial default
5782 // constructors that merely set all fields to null.
5783 if (!ID->hasNonZeroConstructors())
5784 flags |= NonFragileABI_Class_HasCXXDestructorOnly;
5785 }
5786
Douglas Gregor68584ed2009-06-18 16:11:24 +00005787 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
John McCall621915c2012-10-17 04:53:23 +00005788 flags |= NonFragileABI_Class_Exception;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005789
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005790 if (!ID->getClassInterface()->getSuperClass()) {
John McCall621915c2012-10-17 04:53:23 +00005791 flags |= NonFragileABI_Class_Root;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005792 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005793 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005794 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005795 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005796 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005797 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005798 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005799 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005800 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005801 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005802 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005803 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005804 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005805 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005806
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005807 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005808 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005809 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5810 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005811 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005812
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005813 // Determine if this class is also "non-lazy".
5814 if (ImplementationIsNonLazy(ID))
5815 DefinedNonLazyClasses.push_back(ClassMD);
5816
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005817 // Force the definition of the EHType if necessary.
John McCall621915c2012-10-17 04:53:23 +00005818 if (flags & NonFragileABI_Class_Exception)
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005819 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005820 // Make sure method definition entries are all clear for next implementation.
5821 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005822}
5823
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005824/// GenerateProtocolRef - This routine is called to generate code for
5825/// a protocol reference expression; as in:
5826/// @code
5827/// @protocol(Proto1);
5828/// @endcode
5829/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5830/// which will hold address of the protocol meta-data.
5831///
5832llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005833 const ObjCProtocolDecl *PD) {
5834
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005835 // This routine is called for @protocol only. So, we must build definition
5836 // of protocol's meta-data (not a reference to it!)
5837 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005838 llvm::Constant *Init =
5839 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005840 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005841
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005842 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005843 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005845 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5846 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005847 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005848 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849 CGM.getModule(),
5850 Init->getType(), false,
5851 llvm::GlobalValue::WeakAnyLinkage,
5852 Init,
5853 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005854 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5855 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005856 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005857 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005858}
5859
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005860/// GenerateCategory - Build metadata for a category implementation.
5861/// struct _category_t {
5862/// const char * const name;
5863/// struct _class_t *const cls;
5864/// const struct _method_list_t * const instance_methods;
5865/// const struct _method_list_t * const class_methods;
5866/// const struct _protocol_list_t * const protocols;
5867/// const struct _prop_list_t * const properties;
5868/// }
5869///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005870void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005871 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005872 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005873 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5874 "_$_" + OCD->getNameAsString());
5875 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005876 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005877
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005878 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005879 Values[0] = GetClassName(OCD->getIdentifier());
5880 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005881 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005882 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005883 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5884
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005885 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005886 std::vector<llvm::Constant*> Methods;
5887 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005888 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005889 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005890
5891 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005892 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005893 // Instance methods should always be defined.
5894 Methods.push_back(GetMethodConstant(*i));
5895 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005896
5897 Values[2] = EmitMethodList(MethodListName,
5898 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005899 Methods);
5900
5901 MethodListName = Prefix;
5902 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5903 OCD->getNameAsString();
5904 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005906 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005907 // Class methods should always be defined.
5908 Methods.push_back(GetMethodConstant(*i));
5909 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005910
5911 Values[3] = EmitMethodList(MethodListName,
5912 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005913 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005914 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005915 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005916 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005917 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005918 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5919 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005920 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005921 + Interface->getName() + "_$_"
5922 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005923 Category->protocol_begin(),
5924 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005925 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5926 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005927 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005928 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5929 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005930 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005931
5932 llvm::Constant *Init =
5933 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005934 Values);
5935 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005936 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005937 false,
5938 llvm::GlobalValue::InternalLinkage,
5939 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005940 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005941 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005942 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005943 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005944 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005945 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005946
5947 // Determine if this category is also "non-lazy".
5948 if (ImplementationIsNonLazy(OCD))
5949 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005950 // method definition entries must be clear for next implementation.
5951 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005952}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005953
5954/// GetMethodConstant - Return a struct objc_method constant for the
5955/// given method if it has been defined. The result is null if the
5956/// method has not been defined. The return value has type MethodPtrTy.
5957llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005958 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005959 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005960 if (!Fn)
5961 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005962
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005963 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005964 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005965 ObjCTypes.SelectorPtrTy),
5966 GetMethodVarType(MD),
5967 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5968 };
Owen Anderson08e25242009-07-27 22:29:56 +00005969 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005970}
5971
5972/// EmitMethodList - Build meta-data for method declarations
5973/// struct _method_list_t {
5974/// uint32_t entsize; // sizeof(struct _objc_method)
5975/// uint32_t method_count;
5976/// struct _objc_method method_list[method_count];
5977/// }
5978///
Bill Wendlingbb028552012-02-07 09:25:09 +00005979llvm::Constant *
5980CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5981 const char *Section,
5982 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005983 // Return null for empty list.
5984 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005985 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005986
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005987 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005988 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005989 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005990 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005991 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005992 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005993 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005994 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005995 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005996 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005997
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005998 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005999 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006000 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00006001 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006002 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00006003 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006004 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00006005}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006006
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006007/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
6008/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00006009llvm::GlobalVariable *
6010CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
6011 const ObjCIvarDecl *Ivar) {
6012 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00006013 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00006014 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006015 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006016 CGM.getModule().getGlobalVariable(Name);
6017 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006018 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006019 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006020 false,
6021 llvm::GlobalValue::ExternalLinkage,
6022 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00006023 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00006024 return IvarOffsetGV;
6025}
6026
Daniel Dunbare83be122010-04-02 21:14:02 +00006027llvm::Constant *
6028CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
6029 const ObjCIvarDecl *Ivar,
6030 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00006031 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006032 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00006033 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006034 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006035 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00006036
Mike Stumpf5408fe2009-05-16 07:57:57 +00006037 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
6038 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00006039 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
6040 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00006041 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00006042 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00006043 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00006044 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00006045 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006046 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006047}
6048
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006049/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00006050/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006051/// IvarListnfABIPtrTy.
6052/// struct _ivar_t {
6053/// unsigned long int *offset; // pointer to ivar offset location
6054/// char *name;
6055/// char *type;
6056/// uint32_t alignment;
6057/// uint32_t size;
6058/// }
6059/// struct _ivar_list_t {
6060/// uint32 entsize; // sizeof(struct _ivar_t)
6061/// uint32 count;
6062/// struct _iver_t list[count];
6063/// }
6064///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00006065
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006066llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006067 const ObjCImplementationDecl *ID) {
6068
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006069 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006070
Jordy Rosedb8264e2011-07-22 02:08:32 +00006071 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006072 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006073
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006074 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006075
Jordy Rosedb8264e2011-07-22 02:08:32 +00006076 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00006077 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00006078 // Ignore unnamed bit-fields.
6079 if (!IVD->getDeclName())
6080 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006081 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006082 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00006083 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006084 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
6085 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00006086 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00006087 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00006088 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006089 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006090 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006091 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006092 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00006093 // NOTE. Size of a bitfield does not match gcc's, because of the
6094 // way bitfields are treated special in each. But I am told that
6095 // 'size' for bitfield ivars is ignored by the runtime so it does
6096 // not matter. If it matters, there is enough info to get the
6097 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006098 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00006099 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006100 }
6101 // Return null for empty list.
6102 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00006103 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006104
6105 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00006106 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006107 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
6108 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00006109 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006110 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00006111 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006112 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006113 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
6114 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00006115 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00006116 llvm::GlobalValue::InternalLinkage,
6117 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006118 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006119 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006120 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00006121 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006122
Chris Lattnerad64e022009-07-17 23:57:13 +00006123 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006124 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006125}
6126
6127llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006128 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006129 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006130
Fariborz Jahanianda320092009-01-29 19:24:30 +00006131 if (!Entry) {
6132 // We use the initializer as a marker of whether this is a forward
6133 // reference or not. At module finalization we add the empty
6134 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006135 Entry =
6136 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
6137 llvm::GlobalValue::ExternalLinkage,
6138 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006139 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00006140 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00006141 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006142
Fariborz Jahanianda320092009-01-29 19:24:30 +00006143 return Entry;
6144}
6145
6146/// GetOrEmitProtocol - Generate the protocol meta-data:
6147/// @code
6148/// struct _protocol_t {
6149/// id isa; // NULL
6150/// const char * const protocol_name;
6151/// const struct _protocol_list_t * protocol_list; // super protocols
6152/// const struct method_list_t * const instance_methods;
6153/// const struct method_list_t * const class_methods;
6154/// const struct method_list_t *optionalInstanceMethods;
6155/// const struct method_list_t *optionalClassMethods;
6156/// const struct _prop_list_t * properties;
6157/// const uint32_t size; // sizeof(struct _protocol_t)
6158/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00006159/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006160/// }
6161/// @endcode
6162///
6163
6164llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006165 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00006166 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006167
Fariborz Jahanianda320092009-01-29 19:24:30 +00006168 // Early exit if a defining object has already been generated.
6169 if (Entry && Entry->hasInitializer())
6170 return Entry;
6171
Douglas Gregor1d784b22012-01-01 19:51:50 +00006172 // Use the protocol definition, if there is one.
6173 if (const ObjCProtocolDecl *Def = PD->getDefinition())
6174 PD = Def;
6175
Fariborz Jahanianda320092009-01-29 19:24:30 +00006176 // Construct method lists.
6177 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
6178 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00006179 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006180 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006181 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006182 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006183 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006184 if (!C)
6185 return GetOrEmitProtocolRef(PD);
6186
Fariborz Jahanianda320092009-01-29 19:24:30 +00006187 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6188 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006189 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006190 } else {
6191 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006192 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006193 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006194 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006195
6196 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00006197 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00006198 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006199 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006200 if (!C)
6201 return GetOrEmitProtocolRef(PD);
6202
Fariborz Jahanianda320092009-01-29 19:24:30 +00006203 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
6204 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006205 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006206 } else {
6207 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006208 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006209 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00006210 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006211
Bob Wilsondc8dab62011-11-30 01:57:58 +00006212 MethodTypesExt.insert(MethodTypesExt.end(),
6213 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
6214
6215 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00006216 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00006217 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006218 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006219 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
6220 PD->protocol_begin(),
6221 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006222
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006223 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006224 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006225 "__DATA, __objc_const",
6226 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006227 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006228 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006229 "__DATA, __objc_const",
6230 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006231 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006232 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006233 "__DATA, __objc_const",
6234 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006235 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006236 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006237 "__DATA, __objc_const",
6238 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006239 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00006240 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006241 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00006242 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006243 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00006244 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00006245 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
6246 + PD->getName(),
6247 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00006248 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006249 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006250
Fariborz Jahanianda320092009-01-29 19:24:30 +00006251 if (Entry) {
6252 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00006253 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006254 Entry->setInitializer(Init);
6255 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006256 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006257 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
6258 false, llvm::GlobalValue::WeakAnyLinkage, Init,
6259 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006260 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006261 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00006262 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00006263
6264 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00006265 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006266 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006267 CGM.AddUsedGlobal(Entry);
6268
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006269 // Use this protocol meta-data to build protocol list table in section
6270 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006271 llvm::GlobalVariable *PTGV =
6272 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
6273 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
6274 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006275 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006276 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00006277 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00006278 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00006279 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006280 return Entry;
6281}
6282
6283/// EmitProtocolList - Generate protocol list meta-data:
6284/// @code
6285/// struct _protocol_list_t {
6286/// long protocol_count; // Note, this is 32/64 bit
6287/// struct _protocol_t[protocol_count];
6288/// }
6289/// @endcode
6290///
6291llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00006292CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006293 ObjCProtocolDecl::protocol_iterator begin,
6294 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00006295 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006296
Fariborz Jahanianda320092009-01-29 19:24:30 +00006297 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006298 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00006299 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006300
Daniel Dunbar948e2582009-02-15 07:36:20 +00006301 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006302 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006303 Name.toVector(TmpName);
6304 llvm::GlobalVariable *GV =
6305 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006306 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006307 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006308
Daniel Dunbar948e2582009-02-15 07:36:20 +00006309 for (; begin != end; ++begin)
6310 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
6311
Fariborz Jahanianda320092009-01-29 19:24:30 +00006312 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006313 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006314 ObjCTypes.ProtocolnfABIPtrTy));
6315
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006316 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006317 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00006318 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006319 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00006320 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
6321 ProtocolRefs.size()),
6322 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006323
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006324 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00006325 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00006326 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006327 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006328 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00006329 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006330 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00006331 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006332 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00006333 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00006334}
6335
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006336/// GetMethodDescriptionConstant - This routine build following meta-data:
6337/// struct _objc_method {
6338/// SEL _cmd;
6339/// char *method_type;
6340/// char *_imp;
6341/// }
6342
6343llvm::Constant *
6344CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006345 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00006346 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006347 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
6348 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006349 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00006350 if (!Desc[1])
6351 return 0;
6352
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00006353 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00006354 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00006355 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00006356}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006357
6358/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
6359/// This code gen. amounts to generating code for:
6360/// @code
6361/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
6362/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006363///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00006364LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00006365 CodeGen::CodeGenFunction &CGF,
6366 QualType ObjectTy,
6367 llvm::Value *BaseValue,
6368 const ObjCIvarDecl *Ivar,
6369 unsigned CVRQualifiers) {
6370 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006371 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
6372 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
6373 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6374 llvm::MDNode::get(VMContext,
6375 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00006376 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00006377 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00006378}
6379
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006380llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006381 CodeGen::CodeGenFunction &CGF,
6382 const ObjCInterfaceDecl *Interface,
6383 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00006384 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00006385}
6386
John McCallb1e81442011-05-13 23:16:18 +00006387static void appendSelectorForMessageRefTable(std::string &buffer,
6388 Selector selector) {
6389 if (selector.isUnarySelector()) {
6390 buffer += selector.getNameForSlot(0);
6391 return;
6392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006393
John McCallb1e81442011-05-13 23:16:18 +00006394 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
6395 buffer += selector.getNameForSlot(i);
6396 buffer += '_';
6397 }
6398}
6399
John McCall944c8432011-05-14 03:10:52 +00006400/// Emit a "v-table" message send. We emit a weak hidden-visibility
6401/// struct, initially containing the selector pointer and a pointer to
6402/// a "fixup" variant of the appropriate objc_msgSend. To call, we
6403/// load and call the function pointer, passing the address of the
6404/// struct as the second parameter. The runtime determines whether
6405/// the selector is currently emitted using vtable dispatch; if so, it
6406/// substitutes a stub function which simply tail-calls through the
6407/// appropriate vtable slot, and if not, it substitues a stub function
6408/// which tail-calls objc_msgSend. Both stubs adjust the selector
6409/// argument to correctly point to the selector.
6410RValue
6411CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
6412 ReturnValueSlot returnSlot,
6413 QualType resultType,
6414 Selector selector,
6415 llvm::Value *arg0,
6416 QualType arg0Type,
6417 bool isSuper,
6418 const CallArgList &formalArgs,
6419 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00006420 // Compute the actual arguments.
6421 CallArgList args;
6422
John McCall944c8432011-05-14 03:10:52 +00006423 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00006424 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00006425 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
6426 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00006427
John McCall944c8432011-05-14 03:10:52 +00006428 // Second argument: a pointer to the message ref structure. Leave
6429 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00006430 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
6431
6432 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
6433
John McCallde5d3c72012-02-17 03:33:10 +00006434 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00006435
John McCallcba681a2011-05-14 21:12:11 +00006436 NullReturnState nullReturn;
6437
John McCall944c8432011-05-14 03:10:52 +00006438 // Find the function to call and the mangled name for the message
6439 // ref structure. Using a different mangled name wouldn't actually
6440 // be a problem; it would just be a waste.
6441 //
6442 // The runtime currently never uses vtable dispatch for anything
6443 // except normal, non-super message-sends.
6444 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00006445 llvm::Constant *fn = 0;
6446 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00006447 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00006448 if (isSuper) {
6449 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
6450 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006451 } else {
John McCallcba681a2011-05-14 21:12:11 +00006452 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00006453 fn = ObjCTypes.getMessageSendStretFixupFn();
6454 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006455 }
John McCallb1e81442011-05-13 23:16:18 +00006456 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
6457 fn = ObjCTypes.getMessageSendFpretFixupFn();
6458 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00006459 } else {
John McCallb1e81442011-05-13 23:16:18 +00006460 if (isSuper) {
6461 fn = ObjCTypes.getMessageSendSuper2FixupFn();
6462 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006463 } else {
John McCallb1e81442011-05-13 23:16:18 +00006464 fn = ObjCTypes.getMessageSendFixupFn();
6465 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00006466 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00006467 }
John McCallb1e81442011-05-13 23:16:18 +00006468 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
6469 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006470
John McCallb1e81442011-05-13 23:16:18 +00006471 // Append the selector name, except use underscores anywhere we
6472 // would have used colons.
6473 appendSelectorForMessageRefTable(messageRefName, selector);
6474
6475 llvm::GlobalVariable *messageRef
6476 = CGM.getModule().getGlobalVariable(messageRefName);
6477 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00006478 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00006479 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00006480 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00006481 messageRef = new llvm::GlobalVariable(CGM.getModule(),
6482 init->getType(),
6483 /*constant*/ false,
6484 llvm::GlobalValue::WeakAnyLinkage,
6485 init,
6486 messageRefName);
6487 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
6488 messageRef->setAlignment(16);
6489 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
6490 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006491
6492 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00006493 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006494 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
6495 e = method->param_end(); i != e; ++i) {
6496 const ParmVarDecl *ParamDecl = (*i);
6497 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
6498 if (!nullReturn.NullBB)
6499 nullReturn.init(CGF, arg0);
6500 requiresnullCheck = true;
6501 break;
6502 }
6503 }
6504
John McCallb1e81442011-05-13 23:16:18 +00006505 llvm::Value *mref =
6506 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
6507
John McCall944c8432011-05-14 03:10:52 +00006508 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00006509 args[1].RV = RValue::get(mref);
6510
6511 // Load the function to call from the message ref table.
6512 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
6513 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
6514
John McCallde5d3c72012-02-17 03:33:10 +00006515 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00006516
John McCallde5d3c72012-02-17 03:33:10 +00006517 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00006518 return nullReturn.complete(CGF, result, resultType, formalArgs,
6519 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006520}
6521
6522/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006523CodeGen::RValue
6524CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006525 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006526 QualType ResultType,
6527 Selector Sel,
6528 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006529 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00006530 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006531 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00006532 return isVTableDispatchedSelector(Sel)
6533 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006534 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006535 false, CallArgs, Method)
6536 : EmitMessageSend(CGF, Return, ResultType,
6537 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006538 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00006539 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00006540}
6541
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006542llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006543CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006544 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
6545
Daniel Dunbardfff2302009-03-02 05:18:14 +00006546 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006547 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006548 false, llvm::GlobalValue::ExternalLinkage,
6549 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006550 }
6551
6552 return GV;
6553}
6554
John McCallf85e1932011-06-15 23:02:42 +00006555llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
6556 IdentifierInfo *II) {
6557 llvm::GlobalVariable *&Entry = ClassReferences[II];
6558
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006559 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00006560 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00006561 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006562 Entry =
John McCallf85e1932011-06-15 23:02:42 +00006563 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
6564 false, llvm::GlobalValue::InternalLinkage,
6565 ClassGV,
6566 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006567 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006568 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00006569 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006570 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006571 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006572 }
John McCallf85e1932011-06-15 23:02:42 +00006573
Benjamin Kramer578faa82011-09-27 21:06:10 +00006574 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00006575}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006576
John McCallf85e1932011-06-15 23:02:42 +00006577llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
6578 const ObjCInterfaceDecl *ID) {
6579 return EmitClassRefFromId(Builder, ID->getIdentifier());
6580}
6581
6582llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
6583 CGBuilderTy &Builder) {
6584 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
6585 return EmitClassRefFromId(Builder, II);
6586}
6587
Daniel Dunbar11394522009-04-18 08:51:00 +00006588llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006589CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006590 const ObjCInterfaceDecl *ID) {
6591 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006592
Daniel Dunbar11394522009-04-18 08:51:00 +00006593 if (!Entry) {
6594 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6595 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006596 Entry =
6597 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006598 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006599 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006600 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006601 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006602 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006603 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006604 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006605 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006606 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006607
Benjamin Kramer578faa82011-09-27 21:06:10 +00006608 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006609}
6610
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006611/// EmitMetaClassRef - Return a Value * of the address of _class_t
6612/// meta-data
6613///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006614llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6615 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006616 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6617 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006618 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006619
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006620 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006621 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006622 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006623 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006624 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006625 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006626 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006627 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006628 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006629 ObjCTypes.ClassnfABIPtrTy));
6630
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006631 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006632 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006633
Benjamin Kramer578faa82011-09-27 21:06:10 +00006634 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006635}
6636
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006637/// GetClass - Return a reference to the class for the given interface
6638/// decl.
6639llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6640 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006641 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006642 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6643 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6644 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6645 }
6646
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006647 return EmitClassRef(Builder, ID);
6648}
6649
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006650/// Generates a message send where the super is the receiver. This is
6651/// a message send to self with special delivery semantics indicating
6652/// which class's method should be called.
6653CodeGen::RValue
6654CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006655 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006656 QualType ResultType,
6657 Selector Sel,
6658 const ObjCInterfaceDecl *Class,
6659 bool isCategoryImpl,
6660 llvm::Value *Receiver,
6661 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006662 const CodeGen::CallArgList &CallArgs,
6663 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006664 // ...
6665 // Create and init a super structure; this is a (receiver, class)
6666 // pair we will pass to objc_msgSendSuper.
6667 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006668 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006669
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006670 llvm::Value *ReceiverAsObject =
6671 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6672 CGF.Builder.CreateStore(ReceiverAsObject,
6673 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006674
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006675 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006676 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006677 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006678 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006679 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006680 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006681
Mike Stumpf5408fe2009-05-16 07:57:57 +00006682 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6683 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006684 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006685 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6686 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6687 CGF.Builder.CreateStore(Target,
6688 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006689
John McCall944c8432011-05-14 03:10:52 +00006690 return (isVTableDispatchedSelector(Sel))
6691 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006692 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006693 true, CallArgs, Method)
6694 : EmitMessageSend(CGF, Return, ResultType,
6695 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006696 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006697 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006698}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006699
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006700llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006701 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006702 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006703
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006704 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006705 llvm::Constant *Casted =
6706 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6707 ObjCTypes.SelectorPtrTy);
6708 Entry =
6709 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6710 llvm::GlobalValue::InternalLinkage,
6711 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006712 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006713 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006714 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006715
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006716 if (lval)
6717 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006718 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6719
6720 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6721 llvm::MDNode::get(VMContext,
6722 ArrayRef<llvm::Value*>()));
6723 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006724}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006725/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006726/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006727///
6728void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006729 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006730 llvm::Value *dst,
6731 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006732 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006733 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006734 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006735 assert(Size <= 8 && "does not support size > 8");
6736 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6737 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006738 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6739 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006740 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6741 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006742 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6743 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006744 return;
6745}
6746
6747/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6748/// objc_assign_strongCast (id src, id *dst)
6749///
6750void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006751 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006752 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006753 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006754 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006755 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006756 assert(Size <= 8 && "does not support size > 8");
6757 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006758 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006759 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6760 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006761 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6762 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006763 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006764 src, dst, "weakassign");
6765 return;
6766}
6767
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006768void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006769 CodeGen::CodeGenFunction &CGF,
6770 llvm::Value *DestPtr,
6771 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006772 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006773 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6774 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006775 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006776 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006777 return;
6778}
6779
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006780/// EmitObjCWeakRead - Code gen for loading value of a __weak
6781/// object: objc_read_weak (id *src)
6782///
6783llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006784 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006785 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006786 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006787 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6788 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006789 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006790 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006791 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006792 return read_weak;
6793}
6794
6795/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6796/// objc_assign_weak (id src, id *dst)
6797///
6798void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006799 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006800 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006801 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006802 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006803 assert(Size <= 8 && "does not support size > 8");
6804 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6805 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006806 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6807 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006808 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6809 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006810 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006811 src, dst, "weakassign");
6812 return;
6813}
6814
6815/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6816/// objc_assign_global (id src, id *dst)
6817///
6818void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006819 llvm::Value *src, llvm::Value *dst,
6820 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006821 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006822 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006823 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006824 assert(Size <= 8 && "does not support size > 8");
6825 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6826 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006827 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6828 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006829 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6830 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006831 if (!threadlocal)
6832 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6833 src, dst, "globalassign");
6834 else
6835 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6836 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006837 return;
6838}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006839
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006840void
John McCallf1549f62010-07-06 01:34:17 +00006841CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6842 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006843 EmitAtSynchronizedStmt(CGF, S,
6844 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6845 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006846}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006847
John McCall5a180392010-07-24 00:37:23 +00006848llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006849CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006850 // There's a particular fixed type info for 'id'.
6851 if (T->isObjCIdType() ||
6852 T->isObjCQualifiedIdType()) {
6853 llvm::Constant *IDEHType =
6854 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6855 if (!IDEHType)
6856 IDEHType =
6857 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6858 false,
6859 llvm::GlobalValue::ExternalLinkage,
6860 0, "OBJC_EHTYPE_id");
6861 return IDEHType;
6862 }
6863
6864 // All other types should be Objective-C interface pointer types.
6865 const ObjCObjectPointerType *PT =
6866 T->getAs<ObjCObjectPointerType>();
6867 assert(PT && "Invalid @catch type.");
6868 const ObjCInterfaceType *IT = PT->getInterfaceType();
6869 assert(IT && "Invalid @catch type.");
6870 return GetInterfaceEHType(IT->getDecl(), false);
6871}
6872
John McCallf1549f62010-07-06 01:34:17 +00006873void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6874 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006875 EmitTryCatchStmt(CGF, S,
6876 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6877 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6878 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006879}
6880
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006881/// EmitThrowStmt - Generate code for a throw statement.
6882void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6883 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006884 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006885 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006886 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006887 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006888 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006889 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006890 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006891 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006892 }
6893
John McCall7ec404c2010-10-16 08:21:07 +00006894 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006895 CGF.Builder.ClearInsertionPoint();
6896}
Daniel Dunbare588b992009-03-01 04:46:24 +00006897
John McCall5a180392010-07-24 00:37:23 +00006898llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006899CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006900 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006901 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006902
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006903 // If we don't need a definition, return the entry if found or check
6904 // if we use an external reference.
6905 if (!ForDefinition) {
6906 if (Entry)
6907 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006908
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006909 // If this type (or a super class) has the __objc_exception__
6910 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006911 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006912 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006913 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006914 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006915 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006916 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006917 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006918 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006919
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006920 // Otherwise we need to either make a new entry or fill in the
6921 // initializer.
6922 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006923 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006924 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006925 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006926 CGM.getModule().getGlobalVariable(VTableName);
6927 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006928 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6929 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006930 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006931 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006932
Chris Lattner8b418682012-02-07 00:39:47 +00006933 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006934
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006935 llvm::Constant *Values[] = {
6936 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6937 GetClassName(ID->getIdentifier()),
6938 GetClassGlobal(ClassName)
6939 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006940 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006941 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006942
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006943 if (Entry) {
6944 Entry->setInitializer(Init);
6945 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006946 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006947 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006948 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006949 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006950 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006951 }
6952
David Blaikie4e4d0842012-03-11 07:00:24 +00006953 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006954 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006955 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006956 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006957
6958 if (ForDefinition) {
6959 Entry->setSection("__DATA,__objc_const");
6960 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6961 } else {
6962 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6963 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006964
6965 return Entry;
6966}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006967
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006968/* *** */
6969
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006970CodeGen::CGObjCRuntime *
6971CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006972 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6973 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006974 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006975
6976 case ObjCRuntime::MacOSX:
6977 case ObjCRuntime::iOS:
6978 return new CGObjCNonFragileABIMac(CGM);
6979
David Chisnall11d3f4c2012-07-03 20:49:52 +00006980 case ObjCRuntime::GNUstep:
6981 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006982 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006983 llvm_unreachable("these runtimes are not Mac runtimes");
6984 }
6985 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006986}