blob: 4a165ce46e0b01c71d13042dda1353dcc71985b6 [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 Jahanianee0af742009-01-21 22:04:16 +0000758protected:
Owen Anderson69243822009-07-13 04:10:07 +0000759 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000760 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000761 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000763 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000764 SmallVector<GC_IVAR, 16> SkipIvars;
765 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000767 /// LazySymbols - Symbols to generate a lazy reference for. See
768 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000769 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000771 /// DefinedSymbols - External symbols which are defined by this
772 /// module. The symbols in this list and LazySymbols are used to add
773 /// special linker symbols which ensure that Objective-C modules are
774 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000775 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000776
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000777 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000778 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000780 /// MethodVarNames - uniqued method variable names.
781 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000782
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000783 /// DefinedCategoryNames - list of category names in form Class_Category.
784 llvm::SetVector<std::string> DefinedCategoryNames;
785
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000786 /// MethodVarTypes - uniqued method type signatures. We have to use
787 /// a StringMap here because have no other unique reference.
788 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000789
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000790 /// MethodDefinitions - map of methods which have been defined in
791 /// this translation unit.
792 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000794 /// PropertyNames - uniqued method variable names.
795 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000796
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000797 /// ClassReferences - uniqued class references.
798 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000800 /// SelectorReferences - uniqued selector references.
801 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000803 /// Protocols - Protocols for which an objc_protocol structure has
804 /// been emitted. Forward declarations are handled by creating an
805 /// empty structure whose initializer is filled in when/if defined.
806 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000807
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000808 /// DefinedProtocols - Protocols which have actually been
809 /// defined. We should not need this, see FIXME in GenerateProtocol.
810 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000811
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000812 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000813 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000814
815 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000816 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000817
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000818 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000819 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000820
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000821 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000822 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000823
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000824 /// GetNameForMethod - Return a name for the given method.
825 /// \param[out] NameOut - The return value.
826 void GetNameForMethod(const ObjCMethodDecl *OMD,
827 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000828 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000829
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000830 /// GetMethodVarName - Return a unique constant for the given
831 /// selector's name. The return value has type char *.
832 llvm::Constant *GetMethodVarName(Selector Sel);
833 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000834
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000835 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000836 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000837
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000838 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000839 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
840 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000841 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000842
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000843 /// GetPropertyName - Return a unique constant for the given
844 /// name. The return value has type char *.
845 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000847 // FIXME: This can be dropped once string functions are unified.
848 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
849 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000851 /// GetClassName - Return a unique constant for the given selector's
852 /// name. The return value has type char *.
853 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000855 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
856
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000857 /// BuildIvarLayout - Builds ivar layout bitmap for the class
858 /// implementation for the __strong or __weak case.
859 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000860 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
861 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000862
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000863 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000864
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000865 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866 unsigned int BytePos, bool ForStrongLayout,
867 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000868 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000869 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000870 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000871 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000872 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000873 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000874
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000875 /// GetIvarLayoutName - Returns a unique constant for the given
876 /// ivar layout bitmap.
877 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
878 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000879
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000880 /// EmitPropertyList - Emit the given property list. The return
881 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000882 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000884 const ObjCContainerDecl *OCD,
885 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Bob Wilsondc8dab62011-11-30 01:57:58 +0000887 /// EmitProtocolMethodTypes - Generate the array of extended method type
888 /// strings. The return value has type Int8PtrPtrTy.
889 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000890 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000891 const ObjCCommonTypesHelper &ObjCTypes);
892
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000893 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000894 void PushProtocolProperties(
895 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
896 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
897 const Decl *Container,
898 const ObjCProtocolDecl *PROTO,
899 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000900
Fariborz Jahanianda320092009-01-29 19:24:30 +0000901 /// GetProtocolRef - Return a reference to the internal protocol
902 /// description, creating an empty one if it has not been
903 /// defined. The return value has type ProtocolPtrTy.
904 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000905
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000906 /// CreateMetadataVar - Create a global variable with internal
907 /// linkage for use by the Objective-C runtime.
908 ///
909 /// This is a convenience wrapper which not only creates the
910 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000911 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000912 ///
913 /// \param Name - The variable name.
914 /// \param Init - The variable initializer; this is also used to
915 /// define the type of the variable.
916 /// \param Section - The section the variable should go into, or 0.
917 /// \param Align - The alignment for the variable, or 0.
918 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000919 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000920 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000921 llvm::Constant *Init,
922 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000923 unsigned Align,
924 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000925
John McCall944c8432011-05-14 03:10:52 +0000926 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
927 ReturnValueSlot Return,
928 QualType ResultType,
929 llvm::Value *Sel,
930 llvm::Value *Arg0,
931 QualType Arg0Ty,
932 bool IsSuper,
933 const CallArgList &CallArgs,
934 const ObjCMethodDecl *OMD,
935 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000936
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000937 /// EmitImageInfo - Emit the image info marker used to encode some module
938 /// level information.
939 void EmitImageInfo();
940
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000941public:
Owen Anderson69243822009-07-13 04:10:07 +0000942 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +0000943 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000944
David Chisnall0d13f6f2010-01-23 02:40:42 +0000945 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000946
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000947 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
948 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000949
Fariborz Jahanianda320092009-01-29 19:24:30 +0000950 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000951
Fariborz Jahanianda320092009-01-29 19:24:30 +0000952 /// GetOrEmitProtocol - Get the protocol object for the given
953 /// declaration, emitting it if necessary. The return value has type
954 /// ProtocolPtrTy.
955 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000956
Fariborz Jahanianda320092009-01-29 19:24:30 +0000957 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
958 /// object for the given declaration, emitting it if needed. These
959 /// forward references will be filled in with empty bodies if no
960 /// definition is seen. The return value has type ProtocolPtrTy.
961 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000962 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
963 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000964
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000965};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000966
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000967class CGObjCMac : public CGObjCCommonMac {
968private:
969 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000970
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000971 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000972 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000973 void EmitModuleInfo();
974
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000975 /// EmitModuleSymols - Emit module symbols, the list of defined
976 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000977 llvm::Constant *EmitModuleSymbols();
978
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000979 /// FinishModule - Write out global data structures at the end of
980 /// processing a translation unit.
981 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000982
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000983 /// EmitClassExtension - Generate the class extension structure used
984 /// to store the weak ivar layout and properties. The return value
985 /// has type ClassExtensionPtrTy.
986 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
987
988 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
989 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000990 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000991 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000992
John McCallf85e1932011-06-15 23:02:42 +0000993 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
994 IdentifierInfo *II);
995
996 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
997
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000998 /// EmitSuperClassRef - Emits reference to class's main metadata class.
999 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001000
1001 /// EmitIvarList - Emit the ivar list for the given
1002 /// implementation. If ForClass is true the list of class ivars
1003 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1004 /// interface ivars will be emitted. The return value has type
1005 /// IvarListPtrTy.
1006 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001007 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001008
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001009 /// EmitMetaClass - Emit a forward reference to the class structure
1010 /// for the metaclass of the given interface. The return value has
1011 /// type ClassPtrTy.
1012 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1013
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001014 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001015 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001016 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1017 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001018 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001019
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001020 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001021
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001022 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001023
1024 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001025 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001026 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001027 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001028 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001029
1030 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001031 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001033 /// - IsProtocol: True iff these methods are for a protocol.
1034 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001035 /// - Required: When true, only "required" methods are
1036 /// listed. Similarly, when false only "optional" methods are
1037 /// listed. For classes this should always be true.
1038 /// - begin, end: The method list to output.
1039 ///
1040 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001041 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001042 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001043 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001044
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001045 /// GetOrEmitProtocol - Get the protocol object for the given
1046 /// declaration, emitting it if necessary. The return value has type
1047 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001048 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001049
1050 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1051 /// object for the given declaration, emitting it if needed. These
1052 /// forward references will be filled in with empty bodies if no
1053 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001054 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001055
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056 /// EmitProtocolExtension - Generate the protocol extension
1057 /// structure used to store optional instance and class methods, and
1058 /// protocol properties. The return value has type
1059 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001060 llvm::Constant *
1061 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001062 ArrayRef<llvm::Constant*> OptInstanceMethods,
1063 ArrayRef<llvm::Constant*> OptClassMethods,
1064 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001065
1066 /// EmitProtocolList - Generate the list of referenced
1067 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001068 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001069 ObjCProtocolDecl::protocol_iterator begin,
1070 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001071
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001072 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1073 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001074 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1075 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001076
1077public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001078 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001079
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001080 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001081
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001082 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001083 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001084 QualType ResultType,
1085 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001086 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001087 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001088 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001089 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001090
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001091 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001092 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001093 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001094 QualType ResultType,
1095 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001096 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001097 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001098 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001099 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001100 const CallArgList &CallArgs,
1101 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001102
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001103 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001104 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001105
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001106 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1107 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001108
1109 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1110 /// untyped one.
1111 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1112 const ObjCMethodDecl *Method);
1113
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001114 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001115
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001116 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001117
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001118 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001119
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001120 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001121
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001122 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001123 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001124
Chris Lattner74391b42009-03-22 21:03:39 +00001125 virtual llvm::Constant *GetPropertyGetFunction();
1126 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001127 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1128 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001129 virtual llvm::Constant *GetGetStructFunction();
1130 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001131 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001132 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001133
John McCallf1549f62010-07-06 01:34:17 +00001134 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1135 const ObjCAtTryStmt &S);
1136 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1137 const ObjCAtSynchronizedStmt &S);
1138 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001139 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1140 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001141 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001142 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001143 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001144 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001145 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001146 llvm::Value *src, llvm::Value *dest,
1147 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001148 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001149 llvm::Value *src, llvm::Value *dest,
1150 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001151 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1152 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001153 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1154 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001155 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001156
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001157 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1158 QualType ObjectTy,
1159 llvm::Value *BaseValue,
1160 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001161 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001162 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001163 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001164 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001165
1166 /// GetClassGlobal - Return the global variable for the Objective-C
1167 /// class of the given name.
1168 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001169 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001170 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001171};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001172
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001173class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001174private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001175 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001176 llvm::GlobalVariable* ObjCEmptyCacheVar;
1177 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001178
Daniel Dunbar11394522009-04-18 08:51:00 +00001179 /// SuperClassReferences - uniqued super class references.
1180 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001182 /// MetaClassReferences - uniqued meta class references.
1183 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001184
1185 /// EHTypeReferences - uniqued class ehtype references.
1186 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001187
John McCall944c8432011-05-14 03:10:52 +00001188 /// VTableDispatchMethods - List of methods for which we generate
1189 /// vtable-based message dispatch.
1190 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001191
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001192 /// DefinedMetaClasses - List of defined meta-classes.
1193 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1194
John McCall944c8432011-05-14 03:10:52 +00001195 /// isVTableDispatchedSelector - Returns true if SEL is a
1196 /// vtable-based selector.
1197 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001198
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001199 /// FinishNonFragileABIModule - Write out global data structures at the end of
1200 /// processing a translation unit.
1201 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001202
Daniel Dunbar463b8762009-05-15 21:48:48 +00001203 /// AddModuleClassList - Add the given list of class pointers to the
1204 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001205 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001206 const char *SymbolName,
1207 const char *SectionName);
1208
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001209 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1210 unsigned InstanceStart,
1211 unsigned InstanceSize,
1212 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001213 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001215 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001216 llvm::Constant *ClassRoGV,
1217 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001219 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001220
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001221 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001222
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001223 /// EmitMethodList - Emit the method list for the given
1224 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001225 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001226 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001227 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001228 /// EmitIvarList - Emit the ivar list for the given
1229 /// implementation. If ForClass is true the list of class ivars
1230 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1231 /// interface ivars will be emitted. The return value has type
1232 /// IvarListnfABIPtrTy.
1233 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001235 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001236 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001237 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238
Fariborz Jahanianda320092009-01-29 19:24:30 +00001239 /// GetOrEmitProtocol - Get the protocol object for the given
1240 /// declaration, emitting it if necessary. The return value has type
1241 /// ProtocolPtrTy.
1242 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001243
Fariborz Jahanianda320092009-01-29 19:24:30 +00001244 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1245 /// object for the given declaration, emitting it if needed. These
1246 /// forward references will be filled in with empty bodies if no
1247 /// definition is seen. The return value has type ProtocolPtrTy.
1248 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001249
Fariborz Jahanianda320092009-01-29 19:24:30 +00001250 /// EmitProtocolList - Generate the list of referenced
1251 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001252 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001253 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001254 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001255
John McCall944c8432011-05-14 03:10:52 +00001256 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1257 ReturnValueSlot Return,
1258 QualType ResultType,
1259 Selector Sel,
1260 llvm::Value *Receiver,
1261 QualType Arg0Ty,
1262 bool IsSuper,
1263 const CallArgList &CallArgs,
1264 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001265
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001266 /// GetClassGlobal - Return the global variable for the Objective-C
1267 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001268 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001269
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001270 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001271 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001272 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001273 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001274
1275 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1276 IdentifierInfo *II);
1277
1278 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Daniel Dunbar11394522009-04-18 08:51:00 +00001280 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1281 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001282 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1283 const ObjCInterfaceDecl *ID);
1284
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001285 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1286 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001287 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001288 const ObjCInterfaceDecl *ID);
1289
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001290 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1291 /// the given ivar.
1292 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001293 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294 const ObjCInterfaceDecl *ID,
1295 const ObjCIvarDecl *Ivar);
1296
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001297 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1298 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001299 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1300 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001301
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001302 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001303 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001304 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001305 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001306
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001307 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001308 return "OBJC_METACLASS_$_";
1309 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001310
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001311 const char *getClassSymbolPrefix() const {
1312 return "OBJC_CLASS_$_";
1313 }
1314
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001315 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001316 uint32_t &InstanceStart,
1317 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001318
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001319 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001320 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001321 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1322 return CGM.getContext().Selectors.getSelector(0, &II);
1323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001324
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001325 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001326 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1327 return CGM.getContext().Selectors.getSelector(1, &II);
1328 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001329
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001330 /// ImplementationIsNonLazy - Check whether the given category or
1331 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001332 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001333
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001334public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001335 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001336 // FIXME. All stubs for now!
1337 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001338
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001339 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001340 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001341 QualType ResultType,
1342 Selector Sel,
1343 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001344 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001345 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001346 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001347
1348 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001349 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001350 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001351 QualType ResultType,
1352 Selector Sel,
1353 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001354 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 llvm::Value *Receiver,
1356 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001357 const CallArgList &CallArgs,
1358 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001359
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001360 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001361 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001362
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001363 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1364 bool lvalue = false)
1365 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001366
1367 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1368 /// untyped one.
1369 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1370 const ObjCMethodDecl *Method)
1371 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001372
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001373 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001374
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001375 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001376
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001377 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001378
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001379 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001380 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001381
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001382 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001383
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001384 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001385 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001386 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001387 virtual llvm::Constant *GetPropertySetFunction() {
1388 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001389 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001390
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001391 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1392 bool copy) {
1393 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1394 }
1395
David Chisnall8fac25d2010-12-26 22:13:16 +00001396 virtual llvm::Constant *GetSetStructFunction() {
1397 return ObjCTypes.getCopyStructFn();
1398 }
1399 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001400 return ObjCTypes.getCopyStructFn();
1401 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001402 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1403 return ObjCTypes.getCppAtomicObjectFunction();
1404 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001405
Chris Lattner74391b42009-03-22 21:03:39 +00001406 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001407 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001409
John McCallf1549f62010-07-06 01:34:17 +00001410 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1411 const ObjCAtTryStmt &S);
1412 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1413 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001414 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001415 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001416 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001417 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001419 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001420 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001421 llvm::Value *src, llvm::Value *dest,
1422 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001423 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001424 llvm::Value *src, llvm::Value *dest,
1425 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001426 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001427 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001428 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1429 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001430 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001431 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1432 QualType ObjectTy,
1433 llvm::Value *BaseValue,
1434 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001435 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001436 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001437 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001438 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001439};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001440
1441/// A helper class for performing the null-initialization of a return
1442/// value.
1443struct NullReturnState {
1444 llvm::BasicBlock *NullBB;
1445 llvm::BasicBlock *callBB;
1446 NullReturnState() : NullBB(0), callBB(0) {}
1447
1448 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1449 // Make blocks for the null-init and call edges.
1450 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1451 callBB = CGF.createBasicBlock("msgSend.call");
1452
1453 // Check for a null receiver and, if there is one, jump to the
1454 // null-init test.
1455 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1456 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1457
1458 // Otherwise, start performing the call.
1459 CGF.EmitBlock(callBB);
1460 }
1461
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001462 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1463 const CallArgList &CallArgs,
1464 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001465 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001466
1467 llvm::Value *NullInitPtr = 0;
1468 if (result.isScalar() && !resultType->isVoidType()) {
1469 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1470 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1471 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001472
1473 // Finish the call path.
1474 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1475 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1476
1477 // Emit the null-init block and perform the null-initialization there.
1478 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001479
1480 // Release consumed arguments along the null-receiver path.
1481 if (Method) {
1482 CallArgList::const_iterator I = CallArgs.begin();
1483 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1484 e = Method->param_end(); i != e; ++i, ++I) {
1485 const ParmVarDecl *ParamDecl = (*i);
1486 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1487 RValue RV = I->RV;
1488 assert(RV.isScalar() &&
1489 "NullReturnState::complete - arg not on object");
1490 CGF.EmitARCRelease(RV.getScalarVal(), true);
1491 }
1492 }
1493 }
1494
1495 if (result.isScalar()) {
1496 if (NullInitPtr)
1497 CGF.EmitNullInitialization(NullInitPtr, resultType);
1498 // Jump to the continuation block.
1499 CGF.EmitBlock(contBB);
1500 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1501 : result;
1502 }
1503
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001504 if (!resultType->isAnyComplexType()) {
1505 assert(result.isAggregate() && "null init of non-aggregate result?");
1506 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1507 // Jump to the continuation block.
1508 CGF.EmitBlock(contBB);
1509 return result;
1510 }
1511
1512 // _Complex type
1513 // FIXME. Now easy to handle any other scalar type whose result is returned
1514 // in memory due to ABI limitations.
1515 CGF.EmitBlock(contBB);
1516 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1517 llvm::Type *MemberType = CallCV.first->getType();
1518 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1519 // Create phi instruction for scalar complex value.
1520 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1521 PHIReal->addIncoming(ZeroCV, NullBB);
1522 PHIReal->addIncoming(CallCV.first, callBB);
1523 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1524 PHIImag->addIncoming(ZeroCV, NullBB);
1525 PHIImag->addIncoming(CallCV.second, callBB);
1526 return RValue::getComplex(PHIReal, PHIImag);
1527 }
1528};
1529
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001530} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001531
1532/* *** Helper Functions *** */
1533
1534/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001535static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001536 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001537 unsigned idx0,
1538 unsigned idx1) {
1539 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001540 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1541 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001542 };
Jay Foada5c04342011-07-21 14:31:17 +00001543 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001544}
1545
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001546/// hasObjCExceptionAttribute - Return true if this class or any super
1547/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001548static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001549 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001550 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001551 return true;
1552 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001553 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001554 return false;
1555}
1556
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001557/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001558
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001559CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001560 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001561 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001562 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001563}
1564
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001565/// GetClass - Return a reference to the class for the given interface
1566/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001567llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001568 const ObjCInterfaceDecl *ID) {
1569 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001570}
1571
1572/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001573llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1574 bool lval) {
1575 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001576}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001577llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001578 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001579 return EmitSelector(Builder, Method->getSelector());
1580}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001581
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001582llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001583 if (T->isObjCIdType() ||
1584 T->isObjCQualifiedIdType()) {
1585 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001586 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001587 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001588 if (T->isObjCClassType() ||
1589 T->isObjCQualifiedClassType()) {
1590 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001591 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001592 }
1593 if (T->isObjCObjectPointerType())
1594 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1595
John McCall5a180392010-07-24 00:37:23 +00001596 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001597}
1598
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001599/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001600/*
1601 struct __builtin_CFString {
1602 const int *isa; // point to __CFConstantStringClassReference
1603 int flags;
1604 const char *str;
1605 long length;
1606 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001607*/
1608
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001609/// or Generate a constant NSString object.
1610/*
1611 struct __builtin_NSString {
1612 const int *isa; // point to __NSConstantStringClassReference
1613 const char *str;
1614 unsigned int length;
1615 };
1616*/
1617
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001618llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001619 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001620 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001621 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001622 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001623}
1624
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001625enum {
1626 kCFTaggedObjectID_Integer = (1 << 1) + 1
1627};
1628
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001629/// Generates a message send where the super is the receiver. This is
1630/// a message send to self with special delivery semantics indicating
1631/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001632CodeGen::RValue
1633CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001634 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001635 QualType ResultType,
1636 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001637 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001638 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001639 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001640 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001641 const CodeGen::CallArgList &CallArgs,
1642 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001643 // Create and init a super structure; this is a (receiver, class)
1644 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001645 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001646 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001647 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001648 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001649 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001650 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001651
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001652 // If this is a class message the metaclass is passed as the target.
1653 llvm::Value *Target;
1654 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001655 if (isCategoryImpl) {
1656 // Message sent to 'super' in a class method defined in a category
1657 // implementation requires an odd treatment.
1658 // If we are in a class method, we must retrieve the
1659 // _metaclass_ for the current class, pointed at by
1660 // the class's "isa" pointer. The following assumes that
1661 // isa" is the first ivar in a class (which it must be).
1662 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1663 Target = CGF.Builder.CreateStructGEP(Target, 0);
1664 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001665 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001666 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1667 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1668 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1669 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001670 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001671 }
1672 else if (isCategoryImpl)
1673 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1674 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001675 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1676 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1677 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001678 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001679 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1680 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001681 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001682 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001683 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001684 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001685 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001686 return EmitMessageSend(CGF, Return, ResultType,
1687 EmitSelector(CGF.Builder, Sel),
1688 ObjCSuper, ObjCTypes.SuperPtrCTy,
1689 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001690}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001691
1692/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001693CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001694 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001695 QualType ResultType,
1696 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001697 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001698 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001699 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001700 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001701 return EmitMessageSend(CGF, Return, ResultType,
1702 EmitSelector(CGF.Builder, Sel),
1703 Receiver, CGF.getContext().getObjCIdType(),
1704 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001705}
1706
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001707CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001708CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1709 ReturnValueSlot Return,
1710 QualType ResultType,
1711 llvm::Value *Sel,
1712 llvm::Value *Arg0,
1713 QualType Arg0Ty,
1714 bool IsSuper,
1715 const CallArgList &CallArgs,
1716 const ObjCMethodDecl *Method,
1717 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001718 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001719 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001720 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001721 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1722 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001723 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001724
John McCallde5d3c72012-02-17 03:33:10 +00001725 // If we're calling a method, use the formal signature.
1726 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001727
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001728 if (Method)
1729 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1730 CGM.getContext().getCanonicalType(ResultType) &&
1731 "Result type mismatch!");
1732
John McCallcba681a2011-05-14 21:12:11 +00001733 NullReturnState nullReturn;
1734
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001735 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001736 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001737 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001738 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001739 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001740 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1741 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1742 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001743 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1744 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1745 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001746 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001747 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001748 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001749 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001750
1751 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001752 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001753 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1754 e = Method->param_end(); i != e; ++i) {
1755 const ParmVarDecl *ParamDecl = (*i);
1756 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1757 if (!nullReturn.NullBB)
1758 nullReturn.init(CGF, Arg0);
1759 requiresnullCheck = true;
1760 break;
1761 }
1762 }
1763
John McCallde5d3c72012-02-17 03:33:10 +00001764 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1765 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001766 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1767 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001768}
1769
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001770static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1771 if (FQT.isObjCGCStrong())
1772 return Qualifiers::Strong;
1773
John McCallf85e1932011-06-15 23:02:42 +00001774 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001775 return Qualifiers::Weak;
1776
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001777 // check for __unsafe_unretained
1778 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1779 return Qualifiers::GCNone;
1780
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001781 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1782 return Qualifiers::Strong;
1783
1784 if (const PointerType *PT = FQT->getAs<PointerType>())
1785 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1786
1787 return Qualifiers::GCNone;
1788}
1789
John McCall6b5a61b2011-02-07 10:33:21 +00001790llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1791 const CGBlockInfo &blockInfo) {
Chris Lattner8b418682012-02-07 00:39:47 +00001792 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall6b5a61b2011-02-07 10:33:21 +00001793
David Blaikie4e4d0842012-03-11 07:00:24 +00001794 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1795 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001796 return nullPtr;
1797
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001798 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001799 SkipIvars.clear();
1800 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001801 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1802 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001803
Fariborz Jahanian81979822010-09-09 00:21:45 +00001804 // __isa is the first field in block descriptor and must assume by runtime's
1805 // convention that it is GC'able.
1806 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001807
1808 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1809
1810 // Calculate the basic layout of the block structure.
1811 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001812 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001813
1814 // Ignore the optional 'this' capture: C++ objects are not assumed
1815 // to be GC'ed.
1816
1817 // Walk the captured variables.
1818 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1819 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1820 const VarDecl *variable = ci->getVariable();
1821 QualType type = variable->getType();
1822
1823 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1824
1825 // Ignore constant captures.
1826 if (capture.isConstant()) continue;
1827
1828 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1829
1830 // __block variables are passed by their descriptor address.
1831 if (ci->isByRef()) {
1832 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001833 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001834 }
1835
1836 assert(!type->isArrayType() && "array variable should not be caught");
1837 if (const RecordType *record = type->getAs<RecordType>()) {
1838 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001839 continue;
1840 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001841
John McCall6b5a61b2011-02-07 10:33:21 +00001842 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1843 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1844
1845 if (GCAttr == Qualifiers::Strong)
1846 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1847 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001848 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001849 SkipIvars.push_back(GC_IVAR(fieldOffset,
1850 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001851 }
1852
1853 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001854 return nullPtr;
1855
1856 // Sort on byte position; captures might not be allocated in order,
1857 // and unions can do funny things.
1858 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1859 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001860
1861 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001862 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001863 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001864 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001865 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001866 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001867 if (!(s[i] & 0xf0))
1868 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1869 else
1870 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1871 printf("\n");
1872 }
1873
1874 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001875}
1876
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001877llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001878 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001879 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001880 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001881 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1882
Owen Anderson3c4972d2009-07-29 18:54:39 +00001883 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001884 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001885}
1886
Fariborz Jahanianda320092009-01-29 19:24:30 +00001887void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001888 // FIXME: We shouldn't need this, the protocol decl should contain enough
1889 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001890 DefinedProtocols.insert(PD->getIdentifier());
1891
1892 // If we have generated a forward reference to this protocol, emit
1893 // it now. Otherwise do nothing, the protocol objects are lazily
1894 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001895 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001896 GetOrEmitProtocol(PD);
1897}
1898
Fariborz Jahanianda320092009-01-29 19:24:30 +00001899llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001900 if (DefinedProtocols.count(PD->getIdentifier()))
1901 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001902
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001903 return GetOrEmitProtocolRef(PD);
1904}
1905
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1908struct _objc_protocol {
1909struct _objc_protocol_extension *isa;
1910char *protocol_name;
1911struct _objc_protocol_list *protocol_list;
1912struct _objc__method_prototype_list *instance_methods;
1913struct _objc__method_prototype_list *class_methods
1914};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001915
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001916See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001917*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001918llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00001919 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001920
1921 // Early exit if a defining object has already been generated.
1922 if (Entry && Entry->hasInitializer())
1923 return Entry;
1924
Douglas Gregor1d784b22012-01-01 19:51:50 +00001925 // Use the protocol definition, if there is one.
1926 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1927 PD = Def;
1928
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001929 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001930 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001931 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1932
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001933 // Construct method lists.
1934 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1935 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001936 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001937 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001938 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001939 ObjCMethodDecl *MD = *i;
1940 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001941 if (!C)
1942 return GetOrEmitProtocolRef(PD);
1943
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001944 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1945 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001946 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001947 } else {
1948 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001949 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001951 }
1952
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001953 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001954 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001955 ObjCMethodDecl *MD = *i;
1956 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001957 if (!C)
1958 return GetOrEmitProtocolRef(PD);
1959
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001960 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1961 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001962 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001963 } else {
1964 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001965 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001966 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001967 }
1968
Bob Wilsondc8dab62011-11-30 01:57:58 +00001969 MethodTypesExt.insert(MethodTypesExt.end(),
1970 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1971
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001972 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001973 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1974 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001975 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001976 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001977 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001978 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001979 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001980 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001981 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001982 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001983 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001984 ClassMethods)
1985 };
Owen Anderson08e25242009-07-27 22:29:56 +00001986 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001987 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001988
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001989 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001990 // Already created, fix the linkage and update the initializer.
1991 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001992 Entry->setInitializer(Init);
1993 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001994 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001995 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001996 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001997 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001998 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001999 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002000 // FIXME: Is this necessary? Why only for protocol?
2001 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002002
2003 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002004 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002005 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002006
2007 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002008}
2009
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002010llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002011 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2012
2013 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002014 // We use the initializer as a marker of whether this is a forward
2015 // reference or not. At module finalization we add the empty
2016 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002017 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002018 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002019 llvm::GlobalValue::ExternalLinkage,
2020 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002021 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002023 // FIXME: Is this necessary? Why only for protocol?
2024 Entry->setAlignment(4);
2025 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002026
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002027 return Entry;
2028}
2029
2030/*
2031 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002032 uint32_t size;
2033 struct objc_method_description_list *optional_instance_methods;
2034 struct objc_method_description_list *optional_class_methods;
2035 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002036 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002037 };
2038*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002039llvm::Constant *
2040CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002041 ArrayRef<llvm::Constant*> OptInstanceMethods,
2042 ArrayRef<llvm::Constant*> OptClassMethods,
2043 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002044 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002045 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002046 llvm::Constant *Values[] = {
2047 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002048 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002049 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002050 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002051 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002052 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002053 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002054 OptClassMethods),
2055 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002056 ObjCTypes),
2057 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2058 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002059 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002060
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002061 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002062 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002063 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002064 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002065
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002066 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002067 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002068
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002069 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002070 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002071 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002072 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002073}
2074
2075/*
2076 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002077 struct objc_protocol_list *next;
2078 long count;
2079 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002080 };
2081*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002082llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002083CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002084 ObjCProtocolDecl::protocol_iterator begin,
2085 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002086 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002087
Daniel Dunbardbc93372008-08-21 21:57:41 +00002088 for (; begin != end; ++begin)
2089 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002090
2091 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002092 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002093 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002094
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002095 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002096 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002097
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002098 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002099 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002100 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002101 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002102 ProtocolRefs.size() - 1);
2103 Values[2] =
2104 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2105 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002106 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002107
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002108 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002110 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002111 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002112 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002113}
2114
Bill Wendlingbb028552012-02-07 09:25:09 +00002115void CGObjCCommonMac::
2116PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002117 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002118 const Decl *Container,
2119 const ObjCProtocolDecl *PROTO,
2120 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002121 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2122 E = PROTO->protocol_end(); P != E; ++P)
2123 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2124 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2125 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002126 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002127 if (!PropertySet.insert(PD->getIdentifier()))
2128 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002129 llvm::Constant *Prop[] = {
2130 GetPropertyName(PD->getIdentifier()),
2131 GetPropertyTypeString(PD, Container)
2132 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002133 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2134 }
2135}
2136
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002137/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002138 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002139 const char * const name;
2140 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002141 };
2142
2143 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002144 uint32_t entsize; // sizeof (struct _objc_property)
2145 uint32_t prop_count;
2146 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002147 };
2148*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002149llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002150 const Decl *Container,
2151 const ObjCContainerDecl *OCD,
2152 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002153 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002154 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002155 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2156 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002157 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002158 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002159 llvm::Constant *Prop[] = {
2160 GetPropertyName(PD->getIdentifier()),
2161 GetPropertyTypeString(PD, Container)
2162 };
Owen Anderson08e25242009-07-27 22:29:56 +00002163 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002164 Prop));
2165 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002166 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002167 for (ObjCInterfaceDecl::all_protocol_iterator
2168 P = OID->all_referenced_protocol_begin(),
2169 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002170 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2171 ObjCTypes);
2172 }
2173 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2174 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2175 E = CD->protocol_end(); P != E; ++P)
2176 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2177 ObjCTypes);
2178 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002179
2180 // Return null for empty list.
2181 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002182 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002183
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002184 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002185 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002186 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002187 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2188 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002189 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002190 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002191 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002192 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002193
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002194 llvm::GlobalVariable *GV =
2195 CreateMetadataVar(Name, Init,
2196 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002197 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002198 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002199 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002200 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002201}
2202
Bill Wendlingbb028552012-02-07 09:25:09 +00002203llvm::Constant *
2204CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2205 ArrayRef<llvm::Constant*> MethodTypes,
2206 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002207 // Return null for empty list.
2208 if (MethodTypes.empty())
2209 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2210
2211 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2212 MethodTypes.size());
2213 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2214
2215 llvm::GlobalVariable *GV =
2216 CreateMetadataVar(Name, Init,
2217 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2218 (ObjCABI == 2) ? 8 : 4,
2219 true);
2220 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2221}
2222
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002223/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002224 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002225 int count;
2226 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002227 };
2228*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002229llvm::Constant *
2230CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002231 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002232 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002233 ObjCTypes.SelectorPtrTy),
2234 GetMethodVarType(MD)
2235 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002236 if (!Desc[1])
2237 return 0;
2238
Owen Anderson08e25242009-07-27 22:29:56 +00002239 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002240 Desc);
2241}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002242
Bill Wendlingbb028552012-02-07 09:25:09 +00002243llvm::Constant *
2244CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2245 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002246 // Return null for empty list.
2247 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002248 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002249
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002250 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002251 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002252 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002253 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002254 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002255 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002256
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002257 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002258 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002259 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002260}
2261
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002262/*
2263 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002264 char *category_name;
2265 char *class_name;
2266 struct _objc_method_list *instance_methods;
2267 struct _objc_method_list *class_methods;
2268 struct _objc_protocol_list *protocols;
2269 uint32_t size; // <rdar://4585769>
2270 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002271 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002272*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002273void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002274 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002275
Mike Stumpf5408fe2009-05-16 07:57:57 +00002276 // FIXME: This is poor design, the OCD should have a pointer to the category
2277 // decl. Additionally, note that Category can be null for the @implementation
2278 // w/o an @interface case. Sema should just create one for us as it does for
2279 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002280 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002281 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002282 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002283
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002284 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002285 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2286 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002287
Bill Wendling3964e622012-02-09 22:16:49 +00002288 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002289 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002290 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002291 // Instance methods should always be defined.
2292 InstanceMethods.push_back(GetMethodConstant(*i));
2293 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002294 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002295 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002296 // Class methods should always be defined.
2297 ClassMethods.push_back(GetMethodConstant(*i));
2298 }
2299
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002300 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002301 Values[0] = GetClassName(OCD->getIdentifier());
2302 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002303 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002304 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002305 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002306 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002307 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002308 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002309 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002310 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002311 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002312 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002313 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002314 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002315 Category->protocol_begin(),
2316 Category->protocol_end());
2317 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002318 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002319 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002320 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002321
2322 // If there is no category @interface then there can be no properties.
2323 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002324 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002325 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002326 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002327 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002328 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002329
Owen Anderson08e25242009-07-27 22:29:56 +00002330 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002331 Values);
2332
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002333 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002334 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002335 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002336 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002337 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002338 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002339 // method definition entries must be clear for next implementation.
2340 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002341}
2342
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343// FIXME: Get from somewhere?
2344enum ClassFlags {
2345 eClassFlags_Factory = 0x00001,
2346 eClassFlags_Meta = 0x00002,
2347 // <rdr://5142207>
2348 eClassFlags_HasCXXStructors = 0x02000,
2349 eClassFlags_Hidden = 0x20000,
2350 eClassFlags_ABI2_Hidden = 0x00010,
2351 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2352};
2353
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354/*
2355 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002356 Class isa;
2357 Class super_class;
2358 const char *name;
2359 long version;
2360 long info;
2361 long instance_size;
2362 struct _objc_ivar_list *ivars;
2363 struct _objc_method_list *methods;
2364 struct _objc_cache *cache;
2365 struct _objc_protocol_list *protocols;
2366 // Objective-C 1.0 extensions (<rdr://4585769>)
2367 const char *ivar_layout;
2368 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002369 };
2370
2371 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002372*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002374 DefinedSymbols.insert(ID->getIdentifier());
2375
Chris Lattner8ec03f52008-11-24 03:54:41 +00002376 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002377 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002378 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002380 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002381 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002382 Interface->all_referenced_protocol_begin(),
2383 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002384 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002385 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002386 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002387 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002388 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002389
2390 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002391 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002392 Flags |= eClassFlags_Hidden;
2393
Bill Wendling3964e622012-02-09 22:16:49 +00002394 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002395 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002396 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002397 // Instance methods should always be defined.
2398 InstanceMethods.push_back(GetMethodConstant(*i));
2399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002400 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002401 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002402 // Class methods should always be defined.
2403 ClassMethods.push_back(GetMethodConstant(*i));
2404 }
2405
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002406 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002407 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002408 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002409
2410 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2411 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2412
2413 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2414 if (llvm::Constant *C = GetMethodConstant(MD))
2415 InstanceMethods.push_back(C);
2416 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2417 if (llvm::Constant *C = GetMethodConstant(MD))
2418 InstanceMethods.push_back(C);
2419 }
2420 }
2421
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002422 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002423 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002424 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002425 // Record a reference to the super class.
2426 LazySymbols.insert(Super->getIdentifier());
2427
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002428 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002429 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 ObjCTypes.ClassPtrTy);
2431 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002432 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433 }
2434 Values[ 2] = GetClassName(ID->getIdentifier());
2435 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002436 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2437 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2438 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002439 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002440 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002441 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002442 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002443 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002445 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002447 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002449 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002451 std::string Name("\01L_OBJC_CLASS_");
2452 Name += ClassName;
2453 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2454 // Check for a forward reference.
2455 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2456 if (GV) {
2457 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2458 "Forward metaclass reference has incorrect type.");
2459 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2460 GV->setInitializer(Init);
2461 GV->setSection(Section);
2462 GV->setAlignment(4);
2463 CGM.AddUsedGlobal(GV);
2464 }
2465 else
2466 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002467 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002468 // method definition entries must be clear for next implementation.
2469 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002470}
2471
2472llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2473 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00002474 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002475 unsigned Flags = eClassFlags_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00002476 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002477
John McCall1fb0caa2010-10-22 21:05:15 +00002478 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002480
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002481 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002482 // The isa for the metaclass is the root of the hierarchy.
2483 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2484 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2485 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002486 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002487 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002489 // The super class for the metaclass is emitted as the name of the
2490 // super class. The runtime fixes this up to point to the
2491 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002493 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002494 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002495 ObjCTypes.ClassPtrTy);
2496 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002497 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002498 }
2499 Values[ 2] = GetClassName(ID->getIdentifier());
2500 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002501 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2502 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2503 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002504 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002505 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002506 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002507 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002508 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002509 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002510 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002511 Values[ 9] = Protocols;
2512 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002513 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002514 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002515 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002516 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002517 Values);
2518
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002519 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00002520 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002521
2522 // Check for a forward reference.
2523 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2524 if (GV) {
2525 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2526 "Forward metaclass reference has incorrect type.");
2527 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2528 GV->setInitializer(Init);
2529 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002530 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002531 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002532 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002533 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002534 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002535 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002536 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002537
2538 return GV;
2539}
2540
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002541llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002542 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002543
Mike Stumpf5408fe2009-05-16 07:57:57 +00002544 // FIXME: Should we look these up somewhere other than the module. Its a bit
2545 // silly since we only generate these while processing an implementation, so
2546 // exactly one pointer would work if know when we entered/exitted an
2547 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002548
2549 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002550 // Previously, metaclass with internal linkage may have been defined.
2551 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002552 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2553 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002554 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2555 "Forward metaclass reference has incorrect type.");
2556 return GV;
2557 } else {
2558 // Generate as an external reference to keep a consistent
2559 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002560 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002561 llvm::GlobalValue::ExternalLinkage,
2562 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002563 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002564 }
2565}
2566
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002567llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2568 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2569
2570 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2571 true)) {
2572 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2573 "Forward class metadata reference has incorrect type.");
2574 return GV;
2575 } else {
2576 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2577 llvm::GlobalValue::ExternalLinkage,
2578 0,
2579 Name);
2580 }
2581}
2582
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002583/*
2584 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002585 uint32_t size;
2586 const char *weak_ivar_layout;
2587 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002588 };
2589*/
2590llvm::Constant *
2591CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002592 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002593 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002594
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002595 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002596 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002597 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002598 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002599 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002600
2601 // Return null if no extension bits are used.
2602 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002603 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002604
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002605 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002606 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002607 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002608 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002609 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002610}
2611
2612/*
2613 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00002614 char *ivar_name;
2615 char *ivar_type;
2616 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002617 };
2618
2619 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00002620 int ivar_count;
2621 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002622 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002623*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002624llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002625 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002626 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002627
2628 // When emitting the root class GCC emits ivar entries for the
2629 // actual class structure. It is not clear if we need to follow this
2630 // behavior; for now lets try and get away with not doing it. If so,
2631 // the cleanest solution would be to make up an ObjCInterfaceDecl
2632 // for the class.
2633 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002634 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002635
Jordy Rosedb8264e2011-07-22 02:08:32 +00002636 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002637
Jordy Rosedb8264e2011-07-22 02:08:32 +00002638 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002639 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002640 // Ignore unnamed bit-fields.
2641 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002642 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002643 llvm::Constant *Ivar[] = {
2644 GetMethodVarName(IVD->getIdentifier()),
2645 GetMethodVarType(IVD),
2646 llvm::ConstantInt::get(ObjCTypes.IntTy,
2647 ComputeIvarBaseOffset(CGM, OID, IVD))
2648 };
Owen Anderson08e25242009-07-27 22:29:56 +00002649 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002650 }
2651
2652 // Return null for empty list.
2653 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002654 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002655
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002656 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002657 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002658 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002659 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002660 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002661 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002662
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002663 llvm::GlobalVariable *GV;
2664 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002665 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002666 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002667 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002668 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002669 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002670 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002671 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002672 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002673}
2674
2675/*
2676 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002677 SEL method_name;
2678 char *method_types;
2679 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002680 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002681
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002682 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002683 struct objc_method_list *obsolete;
2684 int count;
2685 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002686 };
2687*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002688
2689/// GetMethodConstant - Return a struct objc_method constant for the
2690/// given method if it has been defined. The result is null if the
2691/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002692llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002693 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002694 if (!Fn)
2695 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002696
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002697 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002698 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002699 ObjCTypes.SelectorPtrTy),
2700 GetMethodVarType(MD),
2701 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2702 };
Owen Anderson08e25242009-07-27 22:29:56 +00002703 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002704}
2705
Chris Lattner5f9e2722011-07-23 10:55:15 +00002706llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002707 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00002708 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002709 // Return null for empty list.
2710 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002711 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002712
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002713 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002714 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002715 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002716 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002717 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002718 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002719 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002720
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002721 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002722 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002723}
2724
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002725llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002726 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002727 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002728 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002729
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002730 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002731 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00002732 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002733 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002734 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002735 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002736 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002737 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002738 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002739
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002740 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002741}
2742
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002743llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002745 llvm::Constant *Init,
2746 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002747 unsigned Align,
2748 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002749 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002750 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002751 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002752 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002753 if (Section)
2754 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002755 if (Align)
2756 GV->setAlignment(Align);
2757 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002758 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002759 return GV;
2760}
2761
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002762llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002763 // Abuse this interface function as a place to finalize.
2764 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002765 return NULL;
2766}
2767
Chris Lattner74391b42009-03-22 21:03:39 +00002768llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002769 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002770}
2771
Chris Lattner74391b42009-03-22 21:03:39 +00002772llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002773 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002774}
2775
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002776llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2777 bool copy) {
2778 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2779}
2780
David Chisnall8fac25d2010-12-26 22:13:16 +00002781llvm::Constant *CGObjCMac::GetGetStructFunction() {
2782 return ObjCTypes.getCopyStructFn();
2783}
2784llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002785 return ObjCTypes.getCopyStructFn();
2786}
2787
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002788llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2789 return ObjCTypes.getCppAtomicObjectFunction();
2790}
2791
Chris Lattner74391b42009-03-22 21:03:39 +00002792llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002793 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002794}
2795
John McCallf1549f62010-07-06 01:34:17 +00002796void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2797 return EmitTryOrSynchronizedStmt(CGF, S);
2798}
2799
2800void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2801 const ObjCAtSynchronizedStmt &S) {
2802 return EmitTryOrSynchronizedStmt(CGF, S);
2803}
2804
John McCallcc505292010-07-21 06:59:36 +00002805namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002806 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002807 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002808 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002809 llvm::Value *CallTryExitVar;
2810 llvm::Value *ExceptionData;
2811 ObjCTypesHelper &ObjCTypes;
2812 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002813 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002814 llvm::Value *CallTryExitVar,
2815 llvm::Value *ExceptionData,
2816 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002817 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002818 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2819
John McCallad346f42011-07-12 20:27:29 +00002820 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002821 // Check whether we need to call objc_exception_try_exit.
2822 // In optimized code, this branch will always be folded.
2823 llvm::BasicBlock *FinallyCallExit =
2824 CGF.createBasicBlock("finally.call_exit");
2825 llvm::BasicBlock *FinallyNoCallExit =
2826 CGF.createBasicBlock("finally.no_call_exit");
2827 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2828 FinallyCallExit, FinallyNoCallExit);
2829
2830 CGF.EmitBlock(FinallyCallExit);
2831 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2832 ->setDoesNotThrow();
2833
2834 CGF.EmitBlock(FinallyNoCallExit);
2835
2836 if (isa<ObjCAtTryStmt>(S)) {
2837 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002838 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2839 // Save the current cleanup destination in case there's
2840 // control flow inside the finally statement.
2841 llvm::Value *CurCleanupDest =
2842 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2843
John McCallcc505292010-07-21 06:59:36 +00002844 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2845
John McCalld96a8e72010-08-11 00:16:14 +00002846 if (CGF.HaveInsertPoint()) {
2847 CGF.Builder.CreateStore(CurCleanupDest,
2848 CGF.getNormalCleanupDestSlot());
2849 } else {
2850 // Currently, the end of the cleanup must always exist.
2851 CGF.EnsureInsertPoint();
2852 }
2853 }
John McCallcc505292010-07-21 06:59:36 +00002854 } else {
2855 // Emit objc_sync_exit(expr); as finally's sole statement for
2856 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002857 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002858 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2859 ->setDoesNotThrow();
2860 }
2861 }
2862 };
John McCall87bb5822010-07-31 23:20:56 +00002863
2864 class FragileHazards {
2865 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002866 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002867 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2868
2869 llvm::InlineAsm *ReadHazard;
2870 llvm::InlineAsm *WriteHazard;
2871
2872 llvm::FunctionType *GetAsmFnType();
2873
2874 void collectLocals();
2875 void emitReadHazard(CGBuilderTy &Builder);
2876
2877 public:
2878 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002879
John McCall87bb5822010-07-31 23:20:56 +00002880 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002881 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002882 };
2883}
2884
2885/// Create the fragile-ABI read and write hazards based on the current
2886/// state of the function, which is presumed to be immediately prior
2887/// to a @try block. These hazards are used to maintain correct
2888/// semantics in the face of optimization and the fragile ABI's
2889/// cavalier use of setjmp/longjmp.
2890FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2891 collectLocals();
2892
2893 if (Locals.empty()) return;
2894
2895 // Collect all the blocks in the function.
2896 for (llvm::Function::iterator
2897 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2898 BlocksBeforeTry.insert(&*I);
2899
2900 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2901
2902 // Create a read hazard for the allocas. This inhibits dead-store
2903 // optimizations and forces the values to memory. This hazard is
2904 // inserted before any 'throwing' calls in the protected scope to
2905 // reflect the possibility that the variables might be read from the
2906 // catch block if the call throws.
2907 {
2908 std::string Constraint;
2909 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2910 if (I) Constraint += ',';
2911 Constraint += "*m";
2912 }
2913
2914 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2915 }
2916
2917 // Create a write hazard for the allocas. This inhibits folding
2918 // loads across the hazard. This hazard is inserted at the
2919 // beginning of the catch path to reflect the possibility that the
2920 // variables might have been written within the protected scope.
2921 {
2922 std::string Constraint;
2923 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2924 if (I) Constraint += ',';
2925 Constraint += "=*m";
2926 }
2927
2928 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2929 }
2930}
2931
2932/// Emit a write hazard at the current location.
2933void FragileHazards::emitWriteHazard() {
2934 if (Locals.empty()) return;
2935
Jay Foad4c7d9f12011-07-15 08:37:34 +00002936 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002937}
2938
John McCall87bb5822010-07-31 23:20:56 +00002939void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2940 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002941 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002942}
2943
2944/// Emit read hazards in all the protected blocks, i.e. all the blocks
2945/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002946void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002947 if (Locals.empty()) return;
2948
2949 CGBuilderTy Builder(CGF.getLLVMContext());
2950
2951 // Iterate through all blocks, skipping those prior to the try.
2952 for (llvm::Function::iterator
2953 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2954 llvm::BasicBlock &BB = *FI;
2955 if (BlocksBeforeTry.count(&BB)) continue;
2956
2957 // Walk through all the calls in the block.
2958 for (llvm::BasicBlock::iterator
2959 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2960 llvm::Instruction &I = *BI;
2961
2962 // Ignore instructions that aren't non-intrinsic calls.
2963 // These are the only calls that can possibly call longjmp.
2964 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2965 if (isa<llvm::IntrinsicInst>(I))
2966 continue;
2967
2968 // Ignore call sites marked nounwind. This may be questionable,
2969 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2970 llvm::CallSite CS(&I);
2971 if (CS.doesNotThrow()) continue;
2972
John McCall0b251722010-08-04 05:59:32 +00002973 // Insert a read hazard before the call. This will ensure that
2974 // any writes to the locals are performed before making the
2975 // call. If the call throws, then this is sufficient to
2976 // guarantee correctness as long as it doesn't also write to any
2977 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002978 Builder.SetInsertPoint(&BB, BI);
2979 emitReadHazard(Builder);
2980 }
2981 }
2982}
2983
2984static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2985 if (V) S.insert(V);
2986}
2987
2988void FragileHazards::collectLocals() {
2989 // Compute a set of allocas to ignore.
2990 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2991 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2992 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002993
2994 // Collect all the allocas currently in the function. This is
2995 // probably way too aggressive.
2996 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2997 for (llvm::BasicBlock::iterator
2998 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2999 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3000 Locals.push_back(&*I);
3001}
3002
3003llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003005 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3006 tys[i] = Locals[i]->getType();
3007 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003008}
3009
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003010/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003011
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003012 Objective-C setjmp-longjmp (sjlj) Exception Handling
3013 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003014
John McCallf1549f62010-07-06 01:34:17 +00003015 A catch buffer is a setjmp buffer plus:
3016 - a pointer to the exception that was caught
3017 - a pointer to the previous exception data buffer
3018 - two pointers of reserved storage
3019 Therefore catch buffers form a stack, with a pointer to the top
3020 of the stack kept in thread-local storage.
3021
3022 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3023 objc_exception_try_exit pops the given catch buffer, which is
3024 required to be the top of the EH stack.
3025 objc_exception_throw pops the top of the EH stack, writes the
3026 thrown exception into the appropriate field, and longjmps
3027 to the setjmp buffer. It crashes the process (with a printf
3028 and an abort()) if there are no catch buffers on the stack.
3029 objc_exception_extract just reads the exception pointer out of the
3030 catch buffer.
3031
3032 There's no reason an implementation couldn't use a light-weight
3033 setjmp here --- something like __builtin_setjmp, but API-compatible
3034 with the heavyweight setjmp. This will be more important if we ever
3035 want to implement correct ObjC/C++ exception interactions for the
3036 fragile ABI.
3037
3038 Note that for this use of setjmp/longjmp to be correct, we may need
3039 to mark some local variables volatile: if a non-volatile local
3040 variable is modified between the setjmp and the longjmp, it has
3041 indeterminate value. For the purposes of LLVM IR, it may be
3042 sufficient to make loads and stores within the @try (to variables
3043 declared outside the @try) volatile. This is necessary for
3044 optimized correctness, but is not currently being done; this is
3045 being tracked as rdar://problem/8160285
3046
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003047 The basic framework for a @try-catch-finally is as follows:
3048 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003049 objc_exception_data d;
3050 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003051 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003052
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003053 objc_exception_try_enter(&d);
3054 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003055 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003056 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003057 // exception path
3058 id _caught = objc_exception_extract(&d);
3059
3060 // enter new try scope for handlers
3061 if (!setjmp(d.jmp_buf)) {
3062 ... match exception and execute catch blocks ...
3063
3064 // fell off end, rethrow.
3065 _rethrow = _caught;
3066 ... jump-through-finally to finally_rethrow ...
3067 } else {
3068 // exception in catch block
3069 _rethrow = objc_exception_extract(&d);
3070 _call_try_exit = false;
3071 ... jump-through-finally to finally_rethrow ...
3072 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003073 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003074 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003075
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003076 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003077 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003078 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003079
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003080 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003081 ... dispatch to finally destination ...
3082
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003083 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003084 objc_exception_throw(_rethrow);
3085
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086 finally_end:
3087 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003088
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003089 This framework differs slightly from the one gcc uses, in that gcc
3090 uses _rethrow to determine if objc_exception_try_exit should be called
3091 and if the object should be rethrown. This breaks in the face of
3092 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003093
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003094 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003095
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096 - If there are no catch blocks, then we avoid emitting the second
3097 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003098
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003099 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3100 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003101
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102 - FIXME: If there is no @finally block we can do a few more
3103 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003104
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003105 Rethrows and Jumps-Through-Finally
3106 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003107
John McCallf1549f62010-07-06 01:34:17 +00003108 '@throw;' is supported by pushing the currently-caught exception
3109 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003110
John McCallf1549f62010-07-06 01:34:17 +00003111 Branches through the @finally block are handled with an ordinary
3112 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3113 exceptions are not compatible with C++ exceptions, and this is
3114 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003115
John McCallf1549f62010-07-06 01:34:17 +00003116 @synchronized(expr) { stmt; } is emitted as if it were:
3117 id synch_value = expr;
3118 objc_sync_enter(synch_value);
3119 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003120*/
3121
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003122void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3123 const Stmt &S) {
3124 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003125
3126 // A destination for the fall-through edges of the catch handlers to
3127 // jump to.
3128 CodeGenFunction::JumpDest FinallyEnd =
3129 CGF.getJumpDestInCurrentScope("finally.end");
3130
3131 // A destination for the rethrow edge of the catch handlers to jump
3132 // to.
3133 CodeGenFunction::JumpDest FinallyRethrow =
3134 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003135
Daniel Dunbar1c566672009-02-24 01:43:46 +00003136 // For @synchronized, call objc_sync_enter(sync.expr). The
3137 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003138 // @synchronized. We can't avoid a temp here because we need the
3139 // value to be preserved. If the backend ever does liveness
3140 // correctly after setjmp, this will be unnecessary.
3141 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003142 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003143 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003144 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3145 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003146 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3147 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003148
3149 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3150 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003151 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003152
John McCall0b251722010-08-04 05:59:32 +00003153 // Allocate memory for the setjmp buffer. This needs to be kept
3154 // live throughout the try and catch blocks.
3155 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3156 "exceptiondata.ptr");
3157
John McCall87bb5822010-07-31 23:20:56 +00003158 // Create the fragile hazards. Note that this will not capture any
3159 // of the allocas required for exception processing, but will
3160 // capture the current basic block (which extends all the way to the
3161 // setjmp call) as "before the @try".
3162 FragileHazards Hazards(CGF);
3163
John McCallf1549f62010-07-06 01:34:17 +00003164 // Create a flag indicating whether the cleanup needs to call
3165 // objc_exception_try_exit. This is true except when
3166 // - no catches match and we're branching through the cleanup
3167 // just to rethrow the exception, or
3168 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003169 // The setjmp-safety rule here is that we should always store to this
3170 // variable in a place that dominates the branch through the cleanup
3171 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003172 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003173 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003174
John McCall9e2213d2010-10-04 23:42:51 +00003175 // A slot containing the exception to rethrow. Only needed when we
3176 // have both a @catch and a @finally.
3177 llvm::Value *PropagatingExnVar = 0;
3178
John McCallf1549f62010-07-06 01:34:17 +00003179 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003180 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003181 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003182 CallTryExitVar,
3183 ExceptionData,
3184 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003185
3186 // Enter a try block:
3187 // - Call objc_exception_try_enter to push ExceptionData on top of
3188 // the EH stack.
3189 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3190 ->setDoesNotThrow();
3191
3192 // - Call setjmp on the exception data buffer.
3193 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3194 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3195 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003196 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003197 llvm::CallInst *SetJmpResult =
3198 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3199 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003200 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003201
3202 // If setjmp returned 0, enter the protected block; otherwise,
3203 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003204 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3205 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003206 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003207 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3208 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003209
John McCallf1549f62010-07-06 01:34:17 +00003210 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003211 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003212 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003214 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003215
3216 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003217
John McCallf1549f62010-07-06 01:34:17 +00003218 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003219 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003220
John McCall87bb5822010-07-31 23:20:56 +00003221 // Don't optimize loads of the in-scope locals across this point.
3222 Hazards.emitWriteHazard();
3223
John McCallf1549f62010-07-06 01:34:17 +00003224 // For a @synchronized (or a @try with no catches), just branch
3225 // through the cleanup to the rethrow block.
3226 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3227 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003228 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003229 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003230
3231 // Otherwise, we have to match against the caught exceptions.
3232 } else {
John McCall0b251722010-08-04 05:59:32 +00003233 // Retrieve the exception object. We may emit multiple blocks but
3234 // nothing can cross this so the value is already in SSA form.
3235 llvm::CallInst *Caught =
3236 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3237 ExceptionData, "caught");
3238 Caught->setDoesNotThrow();
3239
John McCallf1549f62010-07-06 01:34:17 +00003240 // Push the exception to rethrow onto the EH value stack for the
3241 // benefit of any @throws in the handlers.
3242 CGF.ObjCEHValueStack.push_back(Caught);
3243
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003244 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003245
John McCall0b251722010-08-04 05:59:32 +00003246 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003247
John McCall0b251722010-08-04 05:59:32 +00003248 llvm::BasicBlock *CatchBlock = 0;
3249 llvm::BasicBlock *CatchHandler = 0;
3250 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003251 // Save the currently-propagating exception before
3252 // objc_exception_try_enter clears the exception slot.
3253 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3254 "propagating_exception");
3255 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3256
John McCall0b251722010-08-04 05:59:32 +00003257 // Enter a new exception try block (in case a @catch block
3258 // throws an exception).
3259 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3260 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003261
John McCall0b251722010-08-04 05:59:32 +00003262 llvm::CallInst *SetJmpResult =
3263 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3264 "setjmp.result");
3265 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003266 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003267
John McCall0b251722010-08-04 05:59:32 +00003268 llvm::Value *Threw =
3269 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3270
3271 CatchBlock = CGF.createBasicBlock("catch");
3272 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3273 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3274
3275 CGF.EmitBlock(CatchBlock);
3276 }
3277
3278 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003279
Daniel Dunbar55e40722008-09-27 07:03:52 +00003280 // Handle catch list. As a special case we check if everything is
3281 // matched and avoid generating code for falling off the end if
3282 // so.
3283 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003284 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3285 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003286
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003287 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003288 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003289
Anders Carlsson80f25672008-09-09 17:59:25 +00003290 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003291 if (!CatchParam) {
3292 AllMatched = true;
3293 } else {
John McCall183700f2009-09-21 23:43:11 +00003294 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003295
John McCallf1549f62010-07-06 01:34:17 +00003296 // catch(id e) always matches under this ABI, since only
3297 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003298 // FIXME: For the time being we also match id<X>; this should
3299 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003300 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003301 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003302 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003303
John McCallf1549f62010-07-06 01:34:17 +00003304 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003305 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003306 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3307
Anders Carlssondde0a942008-09-11 09:15:33 +00003308 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003309 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003310 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003311
3312 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003313 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003314 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003315
Anders Carlssondde0a942008-09-11 09:15:33 +00003316 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003317
3318 // The scope of the catch variable ends right here.
3319 CatchVarCleanups.ForceCleanup();
3320
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003321 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003322 break;
3323 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003324
Steve Naroff14108da2009-07-10 23:34:53 +00003325 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003326 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003327
3328 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003329 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3330 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003331
3332 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003333 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334
John McCallf1549f62010-07-06 01:34:17 +00003335 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003336 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3337 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003338 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003339
John McCallf1549f62010-07-06 01:34:17 +00003340 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3341 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003342
3343 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003344 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003345
Anders Carlsson80f25672008-09-09 17:59:25 +00003346 // Emit the @catch block.
3347 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003348
3349 // Collect any cleanups for the catch variable. The scope lasts until
3350 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003351 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003352
John McCallb6bbcc92010-10-15 04:57:14 +00003353 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003354 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003355
John McCallf1549f62010-07-06 01:34:17 +00003356 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003357 llvm::Value *Tmp =
3358 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003359 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003360 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003361
Anders Carlssondde0a942008-09-11 09:15:33 +00003362 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003363
3364 // We're done with the catch variable.
3365 CatchVarCleanups.ForceCleanup();
3366
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003367 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003368
Anders Carlsson80f25672008-09-09 17:59:25 +00003369 CGF.EmitBlock(NextCatchBlock);
3370 }
3371
John McCallf1549f62010-07-06 01:34:17 +00003372 CGF.ObjCEHValueStack.pop_back();
3373
John McCall0b251722010-08-04 05:59:32 +00003374 // If nothing wanted anything to do with the caught exception,
3375 // kill the extract call.
3376 if (Caught->use_empty())
3377 Caught->eraseFromParent();
3378
3379 if (!AllMatched)
3380 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3381
3382 if (HasFinally) {
3383 // Emit the exception handler for the @catch blocks.
3384 CGF.EmitBlock(CatchHandler);
3385
3386 // In theory we might now need a write hazard, but actually it's
3387 // unnecessary because there's no local-accessing code between
3388 // the try's write hazard and here.
3389 //Hazards.emitWriteHazard();
3390
John McCall9e2213d2010-10-04 23:42:51 +00003391 // Extract the new exception and save it to the
3392 // propagating-exception slot.
3393 assert(PropagatingExnVar);
3394 llvm::CallInst *NewCaught =
3395 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3396 ExceptionData, "caught");
3397 NewCaught->setDoesNotThrow();
3398 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3399
John McCall0b251722010-08-04 05:59:32 +00003400 // Don't pop the catch handler; the throw already did.
3401 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003402 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003403 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003405
John McCall87bb5822010-07-31 23:20:56 +00003406 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003407 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003408
John McCallf1549f62010-07-06 01:34:17 +00003409 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003410 CGF.Builder.restoreIP(TryFallthroughIP);
3411 if (CGF.HaveInsertPoint())
3412 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003413 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003414 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003415
John McCallf1549f62010-07-06 01:34:17 +00003416 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003417 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003418 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003419 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003420 // If we have a propagating-exception variable, check it.
3421 llvm::Value *PropagatingExn;
3422 if (PropagatingExnVar) {
3423 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003424
John McCall9e2213d2010-10-04 23:42:51 +00003425 // Otherwise, just look in the buffer for the exception to throw.
3426 } else {
3427 llvm::CallInst *Caught =
3428 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3429 ExceptionData);
3430 Caught->setDoesNotThrow();
3431 PropagatingExn = Caught;
3432 }
3433
3434 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003435 ->setDoesNotThrow();
3436 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003437 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003438
John McCall87bb5822010-07-31 23:20:56 +00003439 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003440}
3441
3442void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003443 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003444 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003445
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003446 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003447 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003448 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003449 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003450 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003451 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003452 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003453 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003454 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455
John McCallf1549f62010-07-06 01:34:17 +00003456 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3457 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003458 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003459
3460 // Clear the insertion point to indicate we are in unreachable code.
3461 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003462}
3463
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003464/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003465/// object: objc_read_weak (id *src)
3466///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003467llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003468 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003469 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003470 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3471 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3472 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003473 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003474 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003475 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003476 return read_weak;
3477}
3478
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003479/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3480/// objc_assign_weak (id src, id *dst)
3481///
3482void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003483 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003484 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003485 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003486 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003487 assert(Size <= 8 && "does not support size > 8");
3488 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003489 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003490 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3491 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003492 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3493 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003494 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003495 src, dst, "weakassign");
3496 return;
3497}
3498
Fariborz Jahanian58626502008-11-19 00:59:10 +00003499/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3500/// objc_assign_global (id src, id *dst)
3501///
3502void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003503 llvm::Value *src, llvm::Value *dst,
3504 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003505 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003506 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003507 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003508 assert(Size <= 8 && "does not support size > 8");
3509 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003510 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003511 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3512 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003513 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3514 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003515 if (!threadlocal)
3516 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3517 src, dst, "globalassign");
3518 else
3519 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3520 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003521 return;
3522}
3523
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003524/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003525/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003526///
3527void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003528 llvm::Value *src, llvm::Value *dst,
3529 llvm::Value *ivarOffset) {
3530 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003531 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003532 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003533 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003534 assert(Size <= 8 && "does not support size > 8");
3535 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003537 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3538 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003539 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3540 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003541 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3542 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003543 return;
3544}
3545
Fariborz Jahanian58626502008-11-19 00:59:10 +00003546/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3547/// objc_assign_strongCast (id src, id *dst)
3548///
3549void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003550 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003551 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003552 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003553 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003554 assert(Size <= 8 && "does not support size > 8");
3555 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003557 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3558 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003559 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3560 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003561 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003562 src, dst, "weakassign");
3563 return;
3564}
3565
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003566void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003567 llvm::Value *DestPtr,
3568 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003569 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003570 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3571 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003572 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003573 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003574 return;
3575}
3576
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003577/// EmitObjCValueForIvar - Code Gen for ivar reference.
3578///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003579LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3580 QualType ObjectTy,
3581 llvm::Value *BaseValue,
3582 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003583 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003584 const ObjCInterfaceDecl *ID =
3585 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003586 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3587 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003588}
3589
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003590llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003591 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003592 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003593 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003594 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003595 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3596 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003597}
3598
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003599/* *** Private Interface *** */
3600
3601/// EmitImageInfo - Emit the image info marker used to encode some module
3602/// level information.
3603///
3604/// See: <rdr://4810609&4810587&4810587>
3605/// struct IMAGE_INFO {
3606/// unsigned version;
3607/// unsigned flags;
3608/// };
3609enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003610 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003611 eImageInfo_GarbageCollected = (1 << 1),
3612 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003613 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3614
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003615 // A flag indicating that the module has no instances of a @synthesize of a
3616 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00003617 eImageInfo_CorrectedSynthesize = (1 << 4),
3618 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003619};
3620
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003621void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003622 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00003623 const char *Section = (ObjCABI == 1) ?
3624 "__OBJC, __image_info,regular" :
3625 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003626
Bill Wendling3973acc2012-02-16 01:13:30 +00003627 // Generate module-level named metadata to convey this information to the
3628 // linker and code-gen.
3629 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003630
Bill Wendling3973acc2012-02-16 01:13:30 +00003631 // Add the ObjC ABI version to the module flags.
3632 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3633 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3634 version);
3635 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3636 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003637
David Blaikie4e4d0842012-03-11 07:00:24 +00003638 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003639 // Non-GC overrides those files which specify GC.
3640 Mod.addModuleFlag(llvm::Module::Override,
3641 "Objective-C Garbage Collection", (uint32_t)0);
3642 } else {
3643 // Add the ObjC garbage collection value.
3644 Mod.addModuleFlag(llvm::Module::Error,
3645 "Objective-C Garbage Collection",
3646 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003647
David Blaikie4e4d0842012-03-11 07:00:24 +00003648 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003649 // Add the ObjC GC Only value.
3650 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3651 eImageInfo_GCOnly);
3652
3653 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3654 llvm::Value *Ops[2] = {
3655 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3656 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3657 eImageInfo_GarbageCollected)
3658 };
3659 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3660 llvm::MDNode::get(VMContext, Ops));
3661 }
3662 }
Bill Wendling09822512012-04-24 11:04:57 +00003663
3664 // Indicate whether we're compiling this to run on a simulator.
3665 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3666 if (Triple.getOS() == llvm::Triple::IOS &&
3667 (Triple.getArch() == llvm::Triple::x86 ||
3668 Triple.getArch() == llvm::Triple::x86_64))
3669 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3670 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003671}
3672
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003673// struct objc_module {
3674// unsigned long version;
3675// unsigned long size;
3676// const char *name;
3677// Symtab symtab;
3678// };
3679
3680// FIXME: Get from somewhere
3681static const int ModuleVersion = 7;
3682
3683void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00003684 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003685
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003686 llvm::Constant *Values[] = {
3687 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3688 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3689 // This used to be the filename, now it is unused. <rdr://4327263>
3690 GetClassName(&CGM.getContext().Idents.get("")),
3691 EmitModuleSymbols()
3692 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003693 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003694 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003695 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003696 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003697}
3698
3699llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003700 unsigned NumClasses = DefinedClasses.size();
3701 unsigned NumCategories = DefinedCategories.size();
3702
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003703 // Return null if no symbols were defined.
3704 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003705 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003706
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003707 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003708 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003709 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003710 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3711 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003712
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003713 // The runtime expects exactly the list of defined classes followed
3714 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00003715 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003716 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003717 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003718 ObjCTypes.Int8PtrTy);
3719 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003720 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003721 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003722 ObjCTypes.Int8PtrTy);
3723
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003724 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003725 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00003726 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003727 Symbols);
3728
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003729 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003730
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003731 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003732 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3733 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003734 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003735 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003736}
3737
John McCallf85e1932011-06-15 23:02:42 +00003738llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3739 IdentifierInfo *II) {
3740 LazySymbols.insert(II);
3741
3742 llvm::GlobalVariable *&Entry = ClassReferences[II];
3743
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003744 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003745 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003746 llvm::ConstantExpr::getBitCast(GetClassName(II),
3747 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003749 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3750 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3751 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003752 }
John McCallf85e1932011-06-15 23:02:42 +00003753
Benjamin Kramer578faa82011-09-27 21:06:10 +00003754 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003755}
3756
John McCallf85e1932011-06-15 23:02:42 +00003757llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3758 const ObjCInterfaceDecl *ID) {
3759 return EmitClassRefFromId(Builder, ID->getIdentifier());
3760}
3761
3762llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3763 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3764 return EmitClassRefFromId(Builder, II);
3765}
3766
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003767llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3768 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003769 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003770
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003771 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003772 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003773 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003774 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003775 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003776 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3777 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003778 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003779 }
3780
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003781 if (lvalue)
3782 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003783 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003784}
3785
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003786llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003787 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003788
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003789 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003790 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00003791 llvm::ConstantDataArray::getString(VMContext,
3792 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003793 ((ObjCABI == 2) ?
3794 "__TEXT,__objc_classname,cstring_literals" :
3795 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003796 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003797
Owen Andersona1cf15f2009-07-14 23:10:40 +00003798 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003799}
3800
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003801llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3802 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3803 I = MethodDefinitions.find(MD);
3804 if (I != MethodDefinitions.end())
3805 return I->second;
3806
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003807 return NULL;
3808}
3809
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003810/// GetIvarLayoutName - Returns a unique constant for the given
3811/// ivar layout bitmap.
3812llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003813 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003814 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003815}
3816
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003817void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003818 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003819 bool ForStrongLayout,
3820 bool &HasUnion) {
3821 const RecordDecl *RD = RT->getDecl();
3822 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00003823 SmallVector<const FieldDecl*, 16> Fields;
3824 for (RecordDecl::field_iterator i = RD->field_begin(),
3825 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00003826 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00003827 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003828 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00003829 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003830
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003831 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3832 ForStrongLayout, HasUnion);
3833}
3834
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003835void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003836 const llvm::StructLayout *Layout,
3837 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00003838 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003839 unsigned int BytePos, bool ForStrongLayout,
3840 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003841 bool IsUnion = (RD && RD->isUnion());
3842 uint64_t MaxUnionIvarSize = 0;
3843 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003844 const FieldDecl *MaxField = 0;
3845 const FieldDecl *MaxSkippedField = 0;
3846 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003847 uint64_t MaxFieldOffset = 0;
3848 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003849 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003850 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003851
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003852 if (RecFields.empty())
3853 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003854 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3855 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00003856 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003857 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003858 FirstFieldDelta =
3859 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3860 }
3861
Chris Lattnerf1690852009-03-31 08:48:01 +00003862 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003863 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003864 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003865 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003866 // Note that 'i' here is actually the field index inside RD of Field,
3867 // although this dependency is hidden.
3868 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003869 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003870 } else
John McCallf85e1932011-06-15 23:02:42 +00003871 FieldOffset =
3872 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003873
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003874 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003875 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003876 LastFieldBitfieldOrUnnamed = Field;
3877 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003878 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003879 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003880
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003881 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003882 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003883 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003884 if (FQT->isUnionType())
3885 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003886
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003887 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003888 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003889 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003890 continue;
3891 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003892
Chris Lattnerf1690852009-03-31 08:48:01 +00003893 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003894 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003895 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003896 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003897 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003898 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003899 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3900 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003901 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003902 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003903 FQT = CArray->getElementType();
3904 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003905
3906 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003907 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003908 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003909 int OldIndex = IvarsInfo.size() - 1;
3910 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003911
Ted Kremenek6217b802009-07-29 21:53:49 +00003912 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003913 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003914 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003915
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003916 // Replicate layout information for each array element. Note that
3917 // one element is already done.
3918 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003919 for (int FirstIndex = IvarsInfo.size() - 1,
3920 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003921 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003922 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3923 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3924 IvarsInfo[i].ivar_size));
3925 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3926 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3927 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003928 }
3929 continue;
3930 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003931 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003932 // At this point, we are done with Record/Union and array there of.
3933 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003934 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003935
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003936 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003937 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3938 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003939 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003940 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003941 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003942 MaxUnionIvarSize = UnionIvarSize;
3943 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003944 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003945 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003946 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003947 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003948 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003949 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003950 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003951 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3952 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003953 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003954 // FIXME: Why the asymmetry? We divide by word size in bits on other
3955 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003956 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003957 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003958 MaxSkippedUnionIvarSize = UnionIvarSize;
3959 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003960 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003961 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003962 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003963 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003964 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003965 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003966 }
3967 }
3968 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003969
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003970 if (LastFieldBitfieldOrUnnamed) {
3971 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3972 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003973 uint64_t BitFieldSize
3974 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003975 GC_IVAR skivar;
3976 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3977 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3978 + ((BitFieldSize % ByteSizeInBits) != 0);
3979 SkipIvars.push_back(skivar);
3980 } else {
3981 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3982 // Last field was unnamed. Must update skip info.
3983 unsigned FieldSize
3984 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3985 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3986 FieldSize / ByteSizeInBits));
3987 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003988 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003989
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003990 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003992 MaxUnionIvarSize));
3993 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003994 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003995 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003996}
3997
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003998/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3999/// the computations and returning the layout bitmap (for ivar or blocks) in
4000/// the given argument BitMap string container. Routine reads
4001/// two containers, IvarsInfo and SkipIvars which are assumed to be
4002/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004003llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004004 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004005 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004006
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004007 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004008 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004009 unsigned int WordSize =
Micah Villmow25a6a842012-10-08 16:25:52 +00004010 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004011 if (IvarsInfo[0].ivar_bytepos == 0) {
4012 WordsToSkip = 0;
4013 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004014 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004015 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4016 WordsToScan = IvarsInfo[0].ivar_size;
4017 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004018 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004019 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004020 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004021 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004022 // consecutive 'scanned' object pointers.
4023 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004024 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004025 // Skip over 'gc'able object pointer which lay over each other.
4026 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4027 continue;
4028 // Must skip over 1 or more words. We save current skip/scan values
4029 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004030 SKIP_SCAN SkScan;
4031 SkScan.skip = WordsToSkip;
4032 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004033 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004034
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004035 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004036 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4037 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004038 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004039 WordsToSkip = 0;
4040 WordsToScan = IvarsInfo[i].ivar_size;
4041 }
4042 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004043 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004044 SKIP_SCAN SkScan;
4045 SkScan.skip = WordsToSkip;
4046 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004047 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004048 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004049
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004050 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004051 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004052 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004053 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004054 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004055 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004056 IvarsInfo[LastIndex].ivar_bytepos +
4057 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004058 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004059 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004060 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004061 SKIP_SCAN SkScan;
4062 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4063 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004064 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004065 }
4066 }
4067 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4068 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004069 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004070 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004071 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4072 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4073 // 0xM0 followed by 0x0N detected.
4074 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4075 for (int j = i+1; j < SkipScan; j++)
4076 SkipScanIvars[j] = SkipScanIvars[j+1];
4077 --SkipScan;
4078 }
4079 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004080
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004081 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004082 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004083 unsigned char byte;
4084 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4085 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4086 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4087 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004088
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004089 // first skip big.
4090 for (unsigned int ix = 0; ix < skip_big; ix++)
4091 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004092
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004093 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004094 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004095 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004096 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004097 byte |= 0xf;
4098 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004099 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004100 byte |= scan_small;
4101 scan_small = 0;
4102 }
4103 BitMap += byte;
4104 }
4105 // next scan big
4106 for (unsigned int ix = 0; ix < scan_big; ix++)
4107 BitMap += (unsigned char)(0x0f);
4108 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004109 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004110 byte = scan_small;
4111 BitMap += byte;
4112 }
4113 }
4114 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004115 unsigned char zero = 0;
4116 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004117
4118 llvm::GlobalVariable * Entry =
4119 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004120 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004121 ((ObjCABI == 2) ?
4122 "__TEXT,__objc_classname,cstring_literals" :
4123 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004124 1, true);
4125 return getConstantGEP(VMContext, Entry, 0, 0);
4126}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004128/// BuildIvarLayout - Builds ivar layout bitmap for the class
4129/// implementation for the __strong or __weak case.
4130/// The layout map displays which words in ivar list must be skipped
4131/// and which must be scanned by GC (see below). String is built of bytes.
4132/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4133/// of words to skip and right nibble is count of words to scan. So, each
4134/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4135/// represented by a 0x00 byte which also ends the string.
4136/// 1. when ForStrongLayout is true, following ivars are scanned:
4137/// - id, Class
4138/// - object *
4139/// - __strong anything
4140///
4141/// 2. When ForStrongLayout is false, following ivars are scanned:
4142/// - __weak anything
4143///
4144llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4145 const ObjCImplementationDecl *OMD,
4146 bool ForStrongLayout) {
4147 bool hasUnion = false;
4148
Chris Lattner8b418682012-02-07 00:39:47 +00004149 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004150 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4151 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004152 return llvm::Constant::getNullValue(PtrTy);
4153
Jordy Rosedb8264e2011-07-22 02:08:32 +00004154 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4155 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004156 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004157 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004158 IVD; IVD = IVD->getNextIvar())
4159 RecFields.push_back(cast<FieldDecl>(IVD));
4160 }
4161 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004162 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004163 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004164
Jordy Rosedb8264e2011-07-22 02:08:32 +00004165 // FIXME: This is not ideal; we shouldn't have to do this copy.
4166 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004167 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004168
4169 if (RecFields.empty())
4170 return llvm::Constant::getNullValue(PtrTy);
4171
4172 SkipIvars.clear();
4173 IvarsInfo.clear();
4174
4175 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4176 if (IvarsInfo.empty())
4177 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004178 // Sort on byte position in case we encounterred a union nested in
4179 // the ivar list.
4180 if (hasUnion && !IvarsInfo.empty())
4181 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4182 if (hasUnion && !SkipIvars.empty())
4183 std::sort(SkipIvars.begin(), SkipIvars.end());
4184
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004185 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004186 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004187
David Blaikie4e4d0842012-03-11 07:00:24 +00004188 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004189 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004190 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004191 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004192 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004193 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004194 if (!(s[i] & 0xf0))
4195 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4196 else
4197 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4198 printf("\n");
4199 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004200 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004201}
4202
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004203llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004204 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4205
Chris Lattner0b239712012-02-06 22:16:34 +00004206 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004207 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004208 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004209 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004210 ((ObjCABI == 2) ?
4211 "__TEXT,__objc_methname,cstring_literals" :
4212 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004213 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004214
Owen Andersona1cf15f2009-07-14 23:10:40 +00004215 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004216}
4217
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004218// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004219llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004220 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4221}
4222
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004223llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004224 std::string TypeStr;
4225 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4226
4227 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004228
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004229 if (!Entry)
4230 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004231 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004232 ((ObjCABI == 2) ?
4233 "__TEXT,__objc_methtype,cstring_literals" :
4234 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004235 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004236
Owen Andersona1cf15f2009-07-14 23:10:40 +00004237 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004238}
4239
Bob Wilsondc8dab62011-11-30 01:57:58 +00004240llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4241 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004242 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004243 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004244 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004245
4246 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4247
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004248 if (!Entry)
4249 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004250 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004251 ((ObjCABI == 2) ?
4252 "__TEXT,__objc_methtype,cstring_literals" :
4253 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004254 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004255
Owen Andersona1cf15f2009-07-14 23:10:40 +00004256 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004257}
4258
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004259// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004260llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004261 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004262
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004263 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004265 llvm::ConstantDataArray::getString(VMContext,
4266 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004267 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004268 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004269
Owen Andersona1cf15f2009-07-14 23:10:40 +00004270 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004271}
4272
4273// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004274// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004275llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004276CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4277 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004278 std::string TypeStr;
4279 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004280 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4281}
4282
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004283void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004284 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004285 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004286 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004287 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004288 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4289 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004290 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004291 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004292 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004293 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004294}
4295
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004296void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004297 EmitModuleInfo();
4298
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004299 // Emit the dummy bodies for any protocols which were referenced but
4300 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004301 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004302 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4303 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004304 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004305
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004306 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004307 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004308 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004309 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004310 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004311 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004312 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004313 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004314 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004315 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004316 }
4317
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004318 // Add assembler directives to add lazy undefined symbol references
4319 // for classes which are referenced but not defined. This is
4320 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004321 //
4322 // FIXME: It would be nice if we had an LLVM construct for this.
4323 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004324 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004325 Asm += CGM.getModule().getModuleInlineAsm();
4326 if (!Asm.empty() && Asm.back() != '\n')
4327 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004328
Daniel Dunbar33063492009-09-07 00:20:42 +00004329 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004330 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4331 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004332 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4333 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004334 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004335 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004336 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004337 }
4338
Bill Wendling13562a12012-02-07 09:06:01 +00004339 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004340 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4341 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4342 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004343
Daniel Dunbar33063492009-09-07 00:20:42 +00004344 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004345 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004346}
4347
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004348CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004349 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004350 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004351 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004352 ObjCABI = 2;
4353}
4354
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004355/* *** */
4356
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004357ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004358 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4359{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004360 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4361 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004362
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004363 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004364 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004365 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004366 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004367 Int8PtrTy = CGM.Int8PtrTy;
4368 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004369
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004370 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004371 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004372 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004374 // I'm not sure I like this. The implicit coordination is a bit
4375 // gross. We should solve this in a reasonable fashion because this
4376 // is a pretty common task (match some runtime data structure with
4377 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004378
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004379 // FIXME: This is leaked.
4380 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004381
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004382 // struct _objc_super {
4383 // id self;
4384 // Class cls;
4385 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004386 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004387 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004388 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004389 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004390 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004391 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004392 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004393 Ctx.getObjCClassType(), 0, 0, false,
4394 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004395 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004396
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004397 SuperCTy = Ctx.getTagDeclType(RD);
4398 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004399
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004400 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004401 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4402
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004403 // struct _prop_t {
4404 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004405 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004406 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004407 PropertyTy = llvm::StructType::create("struct._prop_t",
4408 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004410 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004411 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004412 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004413 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004414 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004415 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004416 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4417 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004418 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004419 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004420
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004421 // struct _objc_method {
4422 // SEL _cmd;
4423 // char *method_type;
4424 // char *_imp;
4425 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004426 MethodTy = llvm::StructType::create("struct._objc_method",
4427 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4428 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004429
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004430 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004431 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004432 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004433
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004434}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004435
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004436ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004437 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004438 // struct _objc_method_description {
4439 // SEL name;
4440 // char *types;
4441 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004442 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004443 llvm::StructType::create("struct._objc_method_description",
4444 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004445
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004446 // struct _objc_method_description_list {
4447 // int count;
4448 // struct _objc_method_description[1];
4449 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004451 llvm::StructType::create("struct._objc_method_description_list",
4452 IntTy,
4453 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004454
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004455 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004456 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004457 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004458
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004459 // Protocol description structures
4460
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004461 // struct _objc_protocol_extension {
4462 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4463 // struct _objc_method_description_list *optional_instance_methods;
4464 // struct _objc_method_description_list *optional_class_methods;
4465 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004466 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004467 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004468 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004469 llvm::StructType::create("struct._objc_protocol_extension",
4470 IntTy, MethodDescriptionListPtrTy,
4471 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004472 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004474 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004475 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004476
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004477 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004478
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004479 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004480 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004481
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004482 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004483 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004484 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004485 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004486 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004487 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004488
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004489 // struct _objc_protocol {
4490 // struct _objc_protocol_extension *isa;
4491 // char *protocol_name;
4492 // struct _objc_protocol **_objc_protocol_list;
4493 // struct _objc_method_description_list *instance_methods;
4494 // struct _objc_method_description_list *class_methods;
4495 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004496 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4497 llvm::PointerType::getUnqual(ProtocolListTy),
4498 MethodDescriptionListPtrTy,
4499 MethodDescriptionListPtrTy,
4500 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004501
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004502 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004503 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004504
Owen Anderson96e0fc72009-07-29 22:16:19 +00004505 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004506
4507 // Class description structures
4508
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004509 // struct _objc_ivar {
4510 // char *ivar_name;
4511 // char *ivar_type;
4512 // int ivar_offset;
4513 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004514 IvarTy = llvm::StructType::create("struct._objc_ivar",
4515 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004516
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004517 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004518 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004519 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004520 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004521
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004522 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004523 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004524 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004525 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004526
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004527 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004528 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004529 llvm::StructType::create("struct._objc_class_extension",
4530 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004531 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004532
Chris Lattnerc1c20112011-08-12 17:43:31 +00004533 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004534
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004535 // struct _objc_class {
4536 // Class isa;
4537 // Class super_class;
4538 // char *name;
4539 // long version;
4540 // long info;
4541 // long instance_size;
4542 // struct _objc_ivar_list *ivars;
4543 // struct _objc_method_list *methods;
4544 // struct _objc_cache *cache;
4545 // struct _objc_protocol_list *protocols;
4546 // char *ivar_layout;
4547 // struct _objc_class_ext *ext;
4548 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004549 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4550 llvm::PointerType::getUnqual(ClassTy),
4551 Int8PtrTy,
4552 LongTy,
4553 LongTy,
4554 LongTy,
4555 IvarListPtrTy,
4556 MethodListPtrTy,
4557 CachePtrTy,
4558 ProtocolListPtrTy,
4559 Int8PtrTy,
4560 ClassExtensionPtrTy,
4561 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004562
Owen Anderson96e0fc72009-07-29 22:16:19 +00004563 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004564
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004565 // struct _objc_category {
4566 // char *category_name;
4567 // char *class_name;
4568 // struct _objc_method_list *instance_method;
4569 // struct _objc_method_list *class_method;
4570 // uint32_t size; // sizeof(struct _objc_category)
4571 // struct _objc_property_list *instance_properties;// category's @property
4572 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004573 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004574 llvm::StructType::create("struct._objc_category",
4575 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4576 MethodListPtrTy, ProtocolListPtrTy,
4577 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004578
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004579 // Global metadata structures
4580
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004581 // struct _objc_symtab {
4582 // long sel_ref_cnt;
4583 // SEL *refs;
4584 // short cls_def_cnt;
4585 // short cat_def_cnt;
4586 // char *defs[cls_def_cnt + cat_def_cnt];
4587 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004588 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004589 llvm::StructType::create("struct._objc_symtab",
4590 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4591 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004592 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004593
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004594 // struct _objc_module {
4595 // long version;
4596 // long size; // sizeof(struct _objc_module)
4597 // char *name;
4598 // struct _objc_symtab* symtab;
4599 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004600 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004601 llvm::StructType::create("struct._objc_module",
4602 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004603
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Mike Stumpf5408fe2009-05-16 07:57:57 +00004605 // FIXME: This is the size of the setjmp buffer and should be target
4606 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004607 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Anders Carlsson124526b2008-09-09 10:10:21 +00004609 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00004610 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004611
4612 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004613 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00004614 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4615 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004616
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004617}
4618
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004619ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004620 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004621 // struct _method_list_t {
4622 // uint32_t entsize; // sizeof(struct _objc_method)
4623 // uint32_t method_count;
4624 // struct _objc_method method_list[method_count];
4625 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004626 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004627 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4628 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004629 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004630 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004631
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004632 // struct _protocol_t {
4633 // id isa; // NULL
4634 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004635 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004636 // const struct method_list_t * const instance_methods;
4637 // const struct method_list_t * const class_methods;
4638 // const struct method_list_t *optionalInstanceMethods;
4639 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004640 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004641 // const uint32_t size; // sizeof(struct _protocol_t)
4642 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004643 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004644 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004645
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004646 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004647 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004648 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004649
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004650 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004651 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4652 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4653 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4654 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004655 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4656 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004657
4658 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004659 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004660
Fariborz Jahanianda320092009-01-29 19:24:30 +00004661 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004662 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004663 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004664 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004665 ProtocolListnfABITy->setBody(LongTy,
4666 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4667 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004668
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004669 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004670 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004671
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004672 // struct _ivar_t {
4673 // unsigned long int *offset; // pointer to ivar offset location
4674 // char *name;
4675 // char *type;
4676 // uint32_t alignment;
4677 // uint32_t size;
4678 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004679 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004680 llvm::StructType::create("struct._ivar_t",
4681 llvm::PointerType::getUnqual(LongTy),
4682 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004683
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004684 // struct _ivar_list_t {
4685 // uint32 entsize; // sizeof(struct _ivar_t)
4686 // uint32 count;
4687 // struct _iver_t list[count];
4688 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004689 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004690 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4691 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Owen Anderson96e0fc72009-07-29 22:16:19 +00004693 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004694
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004695 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004696 // uint32_t const flags;
4697 // uint32_t const instanceStart;
4698 // uint32_t const instanceSize;
4699 // uint32_t const reserved; // only when building for 64bit targets
4700 // const uint8_t * const ivarLayout;
4701 // const char *const name;
4702 // const struct _method_list_t * const baseMethods;
4703 // const struct _objc_protocol_list *const baseProtocols;
4704 // const struct _ivar_list_t *const ivars;
4705 // const uint8_t * const weakIvarLayout;
4706 // const struct _prop_list_t * const properties;
4707 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004708
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004709 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004710 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4711 IntTy, IntTy, IntTy, Int8PtrTy,
4712 Int8PtrTy, MethodListnfABIPtrTy,
4713 ProtocolListnfABIPtrTy,
4714 IvarListnfABIPtrTy,
4715 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004716
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004717 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004718 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004719 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4720 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004721
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004722 // struct _class_t {
4723 // struct _class_t *isa;
4724 // struct _class_t * const superclass;
4725 // void *cache;
4726 // IMP *vtable;
4727 // struct class_ro_t *ro;
4728 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004729
Chris Lattnerc1c20112011-08-12 17:43:31 +00004730 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004731 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4732 llvm::PointerType::getUnqual(ClassnfABITy),
4733 CachePtrTy,
4734 llvm::PointerType::getUnqual(ImpnfABITy),
4735 llvm::PointerType::getUnqual(ClassRonfABITy),
4736 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004737
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004738 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004739 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004740
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004741 // struct _category_t {
4742 // const char * const name;
4743 // struct _class_t *const cls;
4744 // const struct _method_list_t * const instance_methods;
4745 // const struct _method_list_t * const class_methods;
4746 // const struct _protocol_list_t * const protocols;
4747 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004748 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004749 CategorynfABITy = llvm::StructType::create("struct._category_t",
4750 Int8PtrTy, ClassnfABIPtrTy,
4751 MethodListnfABIPtrTy,
4752 MethodListnfABIPtrTy,
4753 ProtocolListnfABIPtrTy,
4754 PropertyListPtrTy,
4755 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004756
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004757 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004758 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4759 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004761 // MessageRefTy - LLVM for:
4762 // struct _message_ref_t {
4763 // IMP messenger;
4764 // SEL name;
4765 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004766
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004767 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004768 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004769 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004770 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004771 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004772 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004773 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004774 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004775 Ctx.getObjCSelType(), 0, 0, false,
4776 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004777 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004778
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004779 MessageRefCTy = Ctx.getTagDeclType(RD);
4780 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4781 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004782
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004783 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004784 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004785
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004786 // SuperMessageRefTy - LLVM for:
4787 // struct _super_message_ref_t {
4788 // SUPER_IMP messenger;
4789 // SEL name;
4790 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004791 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004792 llvm::StructType::create("struct._super_message_ref_t",
4793 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004794
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004795 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004796 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004797
Daniel Dunbare588b992009-03-01 04:46:24 +00004798
4799 // struct objc_typeinfo {
4800 // const void** vtable; // objc_ehtype_vtable + 2
4801 // const char* name; // c++ typeinfo string
4802 // Class cls;
4803 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004804 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004805 llvm::StructType::create("struct._objc_typeinfo",
4806 llvm::PointerType::getUnqual(Int8PtrTy),
4807 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004808 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004809}
4810
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004811llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004812 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004813
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004814 return NULL;
4815}
4816
Bill Wendlingbb028552012-02-07 09:25:09 +00004817void CGObjCNonFragileABIMac::
4818AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4819 const char *SymbolName,
4820 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00004821 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004822
Daniel Dunbar463b8762009-05-15 21:48:48 +00004823 if (!NumClasses)
4824 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004825
Chris Lattner0b239712012-02-06 22:16:34 +00004826 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004827 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004828 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004829 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00004830 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004831 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004832 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00004833 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004834
Daniel Dunbar463b8762009-05-15 21:48:48 +00004835 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004836 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004837 llvm::GlobalValue::InternalLinkage,
4838 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004839 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00004840 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004841 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004842 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004843}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004845void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4846 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004847
Daniel Dunbar463b8762009-05-15 21:48:48 +00004848 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004849 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004850 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004851 "\01L_OBJC_LABEL_CLASS_$",
4852 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004853
Bill Wendling13562a12012-02-07 09:06:01 +00004854 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004855 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4856 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4857 continue;
4858 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004859 }
4860
Bill Wendling13562a12012-02-07 09:06:01 +00004861 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004862 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4863 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4864 continue;
4865 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4866 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004867
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004869 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4870 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004871
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004872 // Build list of all implemented category addresses in array
4873 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004874 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004875 "\01L_OBJC_LABEL_CATEGORY_$",
4876 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004878 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4879 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004880
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004881 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004882}
4883
John McCall944c8432011-05-14 03:10:52 +00004884/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4885/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004886/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004887/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004888bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4889 // At various points we've experimented with using vtable-based
4890 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004891 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004892 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004893 return false;
John McCall944c8432011-05-14 03:10:52 +00004894 case CodeGenOptions::NonLegacy:
4895 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004896 case CodeGenOptions::Mixed:
4897 break;
4898 }
4899
4900 // If so, see whether this selector is in the white-list of things which must
4901 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004902 if (VTableDispatchMethods.empty()) {
4903 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4904 VTableDispatchMethods.insert(GetNullarySelector("class"));
4905 VTableDispatchMethods.insert(GetNullarySelector("self"));
4906 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4907 VTableDispatchMethods.insert(GetNullarySelector("length"));
4908 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004909
John McCall944c8432011-05-14 03:10:52 +00004910 // These are vtable-based if GC is disabled.
4911 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004912 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004913 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4914 VTableDispatchMethods.insert(GetNullarySelector("release"));
4915 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4916 }
4917
4918 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4919 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4920 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4921 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4922 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4923 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4924 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4925
4926 // These are vtable-based if GC is enabled.
4927 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004928 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004929 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4930 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4931
4932 // "countByEnumeratingWithState:objects:count"
4933 IdentifierInfo *KeyIdents[] = {
4934 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4935 &CGM.getContext().Idents.get("objects"),
4936 &CGM.getContext().Idents.get("count")
4937 };
4938 VTableDispatchMethods.insert(
4939 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4940 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004941 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004942
John McCall944c8432011-05-14 03:10:52 +00004943 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004944}
4945
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004946// Metadata flags
4947enum MetaDataDlags {
4948 CLS = 0x0,
4949 CLS_META = 0x1,
4950 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004951 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004952 CLS_EXCEPTION = 0x20,
4953
4954 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4955 CLS_HAS_IVAR_RELEASER = 0x40,
4956 /// class was compiled with -fobjc-arr
4957 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004958};
4959/// BuildClassRoTInitializer - generate meta-data for:
4960/// struct _class_ro_t {
4961/// uint32_t const flags;
4962/// uint32_t const instanceStart;
4963/// uint32_t const instanceSize;
4964/// uint32_t const reserved; // only when building for 64bit targets
4965/// const uint8_t * const ivarLayout;
4966/// const char *const name;
4967/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004968/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004969/// const struct _ivar_list_t *const ivars;
4970/// const uint8_t * const weakIvarLayout;
4971/// const struct _prop_list_t * const properties;
4972/// }
4973///
4974llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975 unsigned flags,
4976 unsigned InstanceStart,
4977 unsigned InstanceSize,
4978 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004979 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004980 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004981
David Blaikie4e4d0842012-03-11 07:00:24 +00004982 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00004983 flags |= CLS_COMPILED_BY_ARC;
4984
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004985 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4986 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4987 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004988 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004989 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4990 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004991 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004992 // const struct _method_list_t * const baseMethods;
4993 std::vector<llvm::Constant*> Methods;
4994 std::string MethodListName("\01l_OBJC_$_");
4995 if (flags & CLS_META) {
4996 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004997 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004998 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004999 // Class methods should always be defined.
5000 Methods.push_back(GetMethodConstant(*i));
5001 }
5002 } else {
5003 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005004 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005005 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005006 // Instance methods should always be defined.
5007 Methods.push_back(GetMethodConstant(*i));
5008 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005010 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005011 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005012
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005013 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5014 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005015
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005016 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5017 if (llvm::Constant *C = GetMethodConstant(MD))
5018 Methods.push_back(C);
5019 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5020 if (llvm::Constant *C = GetMethodConstant(MD))
5021 Methods.push_back(C);
5022 }
5023 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005024 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005025 Values[ 5] = EmitMethodList(MethodListName,
5026 "__DATA, __objc_const", Methods);
5027
Fariborz Jahanianda320092009-01-29 19:24:30 +00005028 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5029 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005030 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005031 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005032 OID->all_referenced_protocol_begin(),
5033 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005034
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005035 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005036 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005037 else
5038 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005039 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
5040 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005041 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005042 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005043 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005044 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5045 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005046 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005047 Values);
5048 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005049 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5050 llvm::GlobalValue::InternalLinkage,
5051 Init,
5052 (flags & CLS_META) ?
5053 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5054 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005055 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005056 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005057 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005058 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005059
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005060}
5061
5062/// BuildClassMetaData - This routine defines that to-level meta-data
5063/// for the given ClassName for:
5064/// struct _class_t {
5065/// struct _class_t *isa;
5066/// struct _class_t * const superclass;
5067/// void *cache;
5068/// IMP *vtable;
5069/// struct class_ro_t *ro;
5070/// }
5071///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005072llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005073 std::string &ClassName,
5074 llvm::Constant *IsAGV,
5075 llvm::Constant *SuperClassGV,
5076 llvm::Constant *ClassRoGV,
5077 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005078 llvm::Constant *Values[] = {
5079 IsAGV,
5080 SuperClassGV,
5081 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5082 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5083 ClassRoGV // &CLASS_RO_GV
5084 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005085 if (!Values[1])
5086 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005087 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005088 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005089 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5090 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005091 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005092 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005093 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005094 if (HiddenVisibility)
5095 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005096 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005097}
5098
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005099bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005100CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005101 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005102}
5103
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005104void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005105 uint32_t &InstanceStart,
5106 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005107 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005108 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005110 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005111 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005112
5113 // If there are no fields, the start is the same as the end.
5114 if (!RL.getFieldCount())
5115 InstanceStart = InstanceSize;
5116 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005117 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005118}
5119
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005120void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5121 std::string ClassName = ID->getNameAsString();
5122 if (!ObjCEmptyCacheVar) {
5123 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005124 CGM.getModule(),
5125 ObjCTypes.CacheTy,
5126 false,
5127 llvm::GlobalValue::ExternalLinkage,
5128 0,
5129 "_objc_empty_cache");
5130
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005131 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005132 CGM.getModule(),
5133 ObjCTypes.ImpnfABITy,
5134 false,
5135 llvm::GlobalValue::ExternalLinkage,
5136 0,
5137 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005138 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005139 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005140 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005141 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005142 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005143 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005144 uint32_t InstanceSize = InstanceStart;
5145 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005146 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5147 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005148
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005149 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150
5151 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005152 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005153 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005154 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005155 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005156 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005157 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005158 // class is root
5159 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005160 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005161 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005162 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005163 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005164 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5165 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5166 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005167 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005168 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005169 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005170 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005171 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005172 ObjCMetaClassName +
5173 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005174 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005175 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005176 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005177 }
5178 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5179 InstanceStart,
5180 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005181 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005182 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005183 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5184 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005185 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005186
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005187 // Metadata for the class
5188 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005189 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005190 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005191 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005192 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005193
Douglas Gregor68584ed2009-06-18 16:11:24 +00005194 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005195 flags |= CLS_EXCEPTION;
5196
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005197 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005198 flags |= CLS_ROOT;
5199 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005200 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005201 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005202 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005203 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005204 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005205 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005206 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005207 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005208 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005209 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005210 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005211 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005212 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005213
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005214 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005215 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005216 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5217 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005218 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005219
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005220 // Determine if this class is also "non-lazy".
5221 if (ImplementationIsNonLazy(ID))
5222 DefinedNonLazyClasses.push_back(ClassMD);
5223
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005224 // Force the definition of the EHType if necessary.
5225 if (flags & CLS_EXCEPTION)
5226 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005227 // Make sure method definition entries are all clear for next implementation.
5228 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005229}
5230
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005231/// GenerateProtocolRef - This routine is called to generate code for
5232/// a protocol reference expression; as in:
5233/// @code
5234/// @protocol(Proto1);
5235/// @endcode
5236/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5237/// which will hold address of the protocol meta-data.
5238///
5239llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005240 const ObjCProtocolDecl *PD) {
5241
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005242 // This routine is called for @protocol only. So, we must build definition
5243 // of protocol's meta-data (not a reference to it!)
5244 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005245 llvm::Constant *Init =
5246 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005247 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005248
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005249 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005250 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005251
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005252 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5253 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005254 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005255 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005256 CGM.getModule(),
5257 Init->getType(), false,
5258 llvm::GlobalValue::WeakAnyLinkage,
5259 Init,
5260 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005261 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5262 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005263 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005264 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005265}
5266
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005267/// GenerateCategory - Build metadata for a category implementation.
5268/// struct _category_t {
5269/// const char * const name;
5270/// struct _class_t *const cls;
5271/// const struct _method_list_t * const instance_methods;
5272/// const struct _method_list_t * const class_methods;
5273/// const struct _protocol_list_t * const protocols;
5274/// const struct _prop_list_t * const properties;
5275/// }
5276///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005277void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005278 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005279 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005280 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5281 "_$_" + OCD->getNameAsString());
5282 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005283 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005284
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005285 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005286 Values[0] = GetClassName(OCD->getIdentifier());
5287 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005288 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005289 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005290 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5291
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005292 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005293 std::vector<llvm::Constant*> Methods;
5294 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005295 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005296 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297
5298 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005299 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005300 // Instance methods should always be defined.
5301 Methods.push_back(GetMethodConstant(*i));
5302 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303
5304 Values[2] = EmitMethodList(MethodListName,
5305 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005306 Methods);
5307
5308 MethodListName = Prefix;
5309 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5310 OCD->getNameAsString();
5311 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005313 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005314 // Class methods should always be defined.
5315 Methods.push_back(GetMethodConstant(*i));
5316 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005317
5318 Values[3] = EmitMethodList(MethodListName,
5319 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005320 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005322 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005323 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005324 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005325 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5326 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005327 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005328 + Interface->getName() + "_$_"
5329 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005330 Category->protocol_begin(),
5331 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005332 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5333 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005334 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005335 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5336 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005337 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005338
5339 llvm::Constant *Init =
5340 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005341 Values);
5342 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005343 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005344 false,
5345 llvm::GlobalValue::InternalLinkage,
5346 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005347 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005348 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005349 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005350 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005351 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005352 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005353
5354 // Determine if this category is also "non-lazy".
5355 if (ImplementationIsNonLazy(OCD))
5356 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005357 // method definition entries must be clear for next implementation.
5358 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005359}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005360
5361/// GetMethodConstant - Return a struct objc_method constant for the
5362/// given method if it has been defined. The result is null if the
5363/// method has not been defined. The return value has type MethodPtrTy.
5364llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005366 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005367 if (!Fn)
5368 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005370 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005371 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005372 ObjCTypes.SelectorPtrTy),
5373 GetMethodVarType(MD),
5374 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5375 };
Owen Anderson08e25242009-07-27 22:29:56 +00005376 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005377}
5378
5379/// EmitMethodList - Build meta-data for method declarations
5380/// struct _method_list_t {
5381/// uint32_t entsize; // sizeof(struct _objc_method)
5382/// uint32_t method_count;
5383/// struct _objc_method method_list[method_count];
5384/// }
5385///
Bill Wendlingbb028552012-02-07 09:25:09 +00005386llvm::Constant *
5387CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5388 const char *Section,
5389 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005390 // Return null for empty list.
5391 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005392 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005393
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005394 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005395 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005396 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005397 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005398 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005399 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005400 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005401 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005402 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005403 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005404
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005405 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005406 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005407 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005408 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005409 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005410 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005411 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005412}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005413
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005414/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5415/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005416llvm::GlobalVariable *
5417CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5418 const ObjCIvarDecl *Ivar) {
5419 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005420 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005421 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005422 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005423 CGM.getModule().getGlobalVariable(Name);
5424 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005425 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005426 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005427 false,
5428 llvm::GlobalValue::ExternalLinkage,
5429 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005430 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005431 return IvarOffsetGV;
5432}
5433
Daniel Dunbare83be122010-04-02 21:14:02 +00005434llvm::Constant *
5435CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5436 const ObjCIvarDecl *Ivar,
5437 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005438 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005439 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005440 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005441 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005442 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005443
Mike Stumpf5408fe2009-05-16 07:57:57 +00005444 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5445 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005446 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5447 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005448 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005449 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005450 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005451 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005452 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005453 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005454}
5455
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005456/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005457/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005458/// IvarListnfABIPtrTy.
5459/// struct _ivar_t {
5460/// unsigned long int *offset; // pointer to ivar offset location
5461/// char *name;
5462/// char *type;
5463/// uint32_t alignment;
5464/// uint32_t size;
5465/// }
5466/// struct _ivar_list_t {
5467/// uint32 entsize; // sizeof(struct _ivar_t)
5468/// uint32 count;
5469/// struct _iver_t list[count];
5470/// }
5471///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005472
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005473llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005474 const ObjCImplementationDecl *ID) {
5475
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005476 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005477
Jordy Rosedb8264e2011-07-22 02:08:32 +00005478 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005479 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005481 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005482
Jordy Rosedb8264e2011-07-22 02:08:32 +00005483 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005484 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005485 // Ignore unnamed bit-fields.
5486 if (!IVD->getDeclName())
5487 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005488 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005489 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005490 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005491 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5492 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005493 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005494 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00005495 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005496 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005497 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005498 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005499 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005500 // NOTE. Size of a bitfield does not match gcc's, because of the
5501 // way bitfields are treated special in each. But I am told that
5502 // 'size' for bitfield ivars is ignored by the runtime so it does
5503 // not matter. If it matters, there is enough info to get the
5504 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005505 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005506 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005507 }
5508 // Return null for empty list.
5509 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005510 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005511
5512 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00005513 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005514 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5515 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005516 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005517 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005518 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005519 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005520 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5521 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005522 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005523 llvm::GlobalValue::InternalLinkage,
5524 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005525 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005526 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005527 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005528 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005529
Chris Lattnerad64e022009-07-17 23:57:13 +00005530 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005531 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005532}
5533
5534llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005535 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005536 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005537
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 if (!Entry) {
5539 // We use the initializer as a marker of whether this is a forward
5540 // reference or not. At module finalization we add the empty
5541 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005542 Entry =
5543 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5544 llvm::GlobalValue::ExternalLinkage,
5545 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005546 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005547 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005548 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005549
Fariborz Jahanianda320092009-01-29 19:24:30 +00005550 return Entry;
5551}
5552
5553/// GetOrEmitProtocol - Generate the protocol meta-data:
5554/// @code
5555/// struct _protocol_t {
5556/// id isa; // NULL
5557/// const char * const protocol_name;
5558/// const struct _protocol_list_t * protocol_list; // super protocols
5559/// const struct method_list_t * const instance_methods;
5560/// const struct method_list_t * const class_methods;
5561/// const struct method_list_t *optionalInstanceMethods;
5562/// const struct method_list_t *optionalClassMethods;
5563/// const struct _prop_list_t * properties;
5564/// const uint32_t size; // sizeof(struct _protocol_t)
5565/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005566/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005567/// }
5568/// @endcode
5569///
5570
5571llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005572 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00005573 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005574
Fariborz Jahanianda320092009-01-29 19:24:30 +00005575 // Early exit if a defining object has already been generated.
5576 if (Entry && Entry->hasInitializer())
5577 return Entry;
5578
Douglas Gregor1d784b22012-01-01 19:51:50 +00005579 // Use the protocol definition, if there is one.
5580 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5581 PD = Def;
5582
Fariborz Jahanianda320092009-01-29 19:24:30 +00005583 // Construct method lists.
5584 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5585 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005586 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005587 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005588 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005589 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005590 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005591 if (!C)
5592 return GetOrEmitProtocolRef(PD);
5593
Fariborz Jahanianda320092009-01-29 19:24:30 +00005594 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5595 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005596 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005597 } else {
5598 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005599 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005600 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005601 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005602
5603 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005604 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005605 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005606 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005607 if (!C)
5608 return GetOrEmitProtocolRef(PD);
5609
Fariborz Jahanianda320092009-01-29 19:24:30 +00005610 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5611 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005612 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005613 } else {
5614 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005615 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005616 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005617 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005618
Bob Wilsondc8dab62011-11-30 01:57:58 +00005619 MethodTypesExt.insert(MethodTypesExt.end(),
5620 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5621
5622 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005623 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005624 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005625 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005626 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5627 PD->protocol_begin(),
5628 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005629
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005630 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005631 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005632 "__DATA, __objc_const",
5633 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005634 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005635 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005636 "__DATA, __objc_const",
5637 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005638 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005639 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005640 "__DATA, __objc_const",
5641 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005642 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005643 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005644 "__DATA, __objc_const",
5645 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005646 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005647 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00005649 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005650 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005651 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005652 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5653 + PD->getName(),
5654 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005655 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005656 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005657
Fariborz Jahanianda320092009-01-29 19:24:30 +00005658 if (Entry) {
5659 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005660 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005661 Entry->setInitializer(Init);
5662 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005664 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5665 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5666 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005667 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005668 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005669 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00005670
5671 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005672 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005673 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005674 CGM.AddUsedGlobal(Entry);
5675
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005676 // Use this protocol meta-data to build protocol list table in section
5677 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005678 llvm::GlobalVariable *PTGV =
5679 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5680 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5681 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005682 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005683 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005684 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005685 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005686 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005687 return Entry;
5688}
5689
5690/// EmitProtocolList - Generate protocol list meta-data:
5691/// @code
5692/// struct _protocol_list_t {
5693/// long protocol_count; // Note, this is 32/64 bit
5694/// struct _protocol_t[protocol_count];
5695/// }
5696/// @endcode
5697///
5698llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005699CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005700 ObjCProtocolDecl::protocol_iterator begin,
5701 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00005702 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005703
Fariborz Jahanianda320092009-01-29 19:24:30 +00005704 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005706 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707
Daniel Dunbar948e2582009-02-15 07:36:20 +00005708 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005709 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005710 Name.toVector(TmpName);
5711 llvm::GlobalVariable *GV =
5712 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005713 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005714 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005715
Daniel Dunbar948e2582009-02-15 07:36:20 +00005716 for (; begin != end; ++begin)
5717 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5718
Fariborz Jahanianda320092009-01-29 19:24:30 +00005719 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005720 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005721 ObjCTypes.ProtocolnfABIPtrTy));
5722
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005723 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005724 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005725 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005726 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00005727 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5728 ProtocolRefs.size()),
5729 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005730
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005731 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005732 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005733 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005734 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005735 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005736 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005737 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005738 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005739 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005740 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005741}
5742
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005743/// GetMethodDescriptionConstant - This routine build following meta-data:
5744/// struct _objc_method {
5745/// SEL _cmd;
5746/// char *method_type;
5747/// char *_imp;
5748/// }
5749
5750llvm::Constant *
5751CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005752 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005753 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005754 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5755 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005756 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005757 if (!Desc[1])
5758 return 0;
5759
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005760 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005761 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005762 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005763}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005764
5765/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5766/// This code gen. amounts to generating code for:
5767/// @code
5768/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5769/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005770///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005771LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005772 CodeGen::CodeGenFunction &CGF,
5773 QualType ObjectTy,
5774 llvm::Value *BaseValue,
5775 const ObjCIvarDecl *Ivar,
5776 unsigned CVRQualifiers) {
5777 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005778 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5779 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5780 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5781 llvm::MDNode::get(VMContext,
5782 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00005783 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005784 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005785}
5786
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005787llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005788 CodeGen::CodeGenFunction &CGF,
5789 const ObjCInterfaceDecl *Interface,
5790 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005791 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005792}
5793
John McCallb1e81442011-05-13 23:16:18 +00005794static void appendSelectorForMessageRefTable(std::string &buffer,
5795 Selector selector) {
5796 if (selector.isUnarySelector()) {
5797 buffer += selector.getNameForSlot(0);
5798 return;
5799 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005800
John McCallb1e81442011-05-13 23:16:18 +00005801 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5802 buffer += selector.getNameForSlot(i);
5803 buffer += '_';
5804 }
5805}
5806
John McCall944c8432011-05-14 03:10:52 +00005807/// Emit a "v-table" message send. We emit a weak hidden-visibility
5808/// struct, initially containing the selector pointer and a pointer to
5809/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5810/// load and call the function pointer, passing the address of the
5811/// struct as the second parameter. The runtime determines whether
5812/// the selector is currently emitted using vtable dispatch; if so, it
5813/// substitutes a stub function which simply tail-calls through the
5814/// appropriate vtable slot, and if not, it substitues a stub function
5815/// which tail-calls objc_msgSend. Both stubs adjust the selector
5816/// argument to correctly point to the selector.
5817RValue
5818CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5819 ReturnValueSlot returnSlot,
5820 QualType resultType,
5821 Selector selector,
5822 llvm::Value *arg0,
5823 QualType arg0Type,
5824 bool isSuper,
5825 const CallArgList &formalArgs,
5826 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005827 // Compute the actual arguments.
5828 CallArgList args;
5829
John McCall944c8432011-05-14 03:10:52 +00005830 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005831 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005832 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5833 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005834
John McCall944c8432011-05-14 03:10:52 +00005835 // Second argument: a pointer to the message ref structure. Leave
5836 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005837 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5838
5839 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5840
John McCallde5d3c72012-02-17 03:33:10 +00005841 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00005842
John McCallcba681a2011-05-14 21:12:11 +00005843 NullReturnState nullReturn;
5844
John McCall944c8432011-05-14 03:10:52 +00005845 // Find the function to call and the mangled name for the message
5846 // ref structure. Using a different mangled name wouldn't actually
5847 // be a problem; it would just be a waste.
5848 //
5849 // The runtime currently never uses vtable dispatch for anything
5850 // except normal, non-super message-sends.
5851 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005852 llvm::Constant *fn = 0;
5853 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00005854 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005855 if (isSuper) {
5856 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5857 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005858 } else {
John McCallcba681a2011-05-14 21:12:11 +00005859 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005860 fn = ObjCTypes.getMessageSendStretFixupFn();
5861 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005862 }
John McCallb1e81442011-05-13 23:16:18 +00005863 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5864 fn = ObjCTypes.getMessageSendFpretFixupFn();
5865 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005866 } else {
John McCallb1e81442011-05-13 23:16:18 +00005867 if (isSuper) {
5868 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5869 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005870 } else {
John McCallb1e81442011-05-13 23:16:18 +00005871 fn = ObjCTypes.getMessageSendFixupFn();
5872 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005873 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005874 }
John McCallb1e81442011-05-13 23:16:18 +00005875 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5876 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005877
John McCallb1e81442011-05-13 23:16:18 +00005878 // Append the selector name, except use underscores anywhere we
5879 // would have used colons.
5880 appendSelectorForMessageRefTable(messageRefName, selector);
5881
5882 llvm::GlobalVariable *messageRef
5883 = CGM.getModule().getGlobalVariable(messageRefName);
5884 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005885 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005886 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005887 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005888 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5889 init->getType(),
5890 /*constant*/ false,
5891 llvm::GlobalValue::WeakAnyLinkage,
5892 init,
5893 messageRefName);
5894 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5895 messageRef->setAlignment(16);
5896 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5897 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005898
5899 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00005900 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005901 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5902 e = method->param_end(); i != e; ++i) {
5903 const ParmVarDecl *ParamDecl = (*i);
5904 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5905 if (!nullReturn.NullBB)
5906 nullReturn.init(CGF, arg0);
5907 requiresnullCheck = true;
5908 break;
5909 }
5910 }
5911
John McCallb1e81442011-05-13 23:16:18 +00005912 llvm::Value *mref =
5913 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5914
John McCall944c8432011-05-14 03:10:52 +00005915 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005916 args[1].RV = RValue::get(mref);
5917
5918 // Load the function to call from the message ref table.
5919 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5920 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5921
John McCallde5d3c72012-02-17 03:33:10 +00005922 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00005923
John McCallde5d3c72012-02-17 03:33:10 +00005924 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005925 return nullReturn.complete(CGF, result, resultType, formalArgs,
5926 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005927}
5928
5929/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005930CodeGen::RValue
5931CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005932 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005933 QualType ResultType,
5934 Selector Sel,
5935 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005936 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005937 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005938 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005939 return isVTableDispatchedSelector(Sel)
5940 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005941 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005942 false, CallArgs, Method)
5943 : EmitMessageSend(CGF, Return, ResultType,
5944 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005945 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005946 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005947}
5948
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005949llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005950CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005951 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5952
Daniel Dunbardfff2302009-03-02 05:18:14 +00005953 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005954 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005955 false, llvm::GlobalValue::ExternalLinkage,
5956 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005957 }
5958
5959 return GV;
5960}
5961
John McCallf85e1932011-06-15 23:02:42 +00005962llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5963 IdentifierInfo *II) {
5964 llvm::GlobalVariable *&Entry = ClassReferences[II];
5965
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005966 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005967 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005968 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005969 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005970 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5971 false, llvm::GlobalValue::InternalLinkage,
5972 ClassGV,
5973 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005974 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005975 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005976 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005977 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005978 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005979 }
John McCallf85e1932011-06-15 23:02:42 +00005980
Benjamin Kramer578faa82011-09-27 21:06:10 +00005981 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005982}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005983
John McCallf85e1932011-06-15 23:02:42 +00005984llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5985 const ObjCInterfaceDecl *ID) {
5986 return EmitClassRefFromId(Builder, ID->getIdentifier());
5987}
5988
5989llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5990 CGBuilderTy &Builder) {
5991 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5992 return EmitClassRefFromId(Builder, II);
5993}
5994
Daniel Dunbar11394522009-04-18 08:51:00 +00005995llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005996CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005997 const ObjCInterfaceDecl *ID) {
5998 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005999
Daniel Dunbar11394522009-04-18 08:51:00 +00006000 if (!Entry) {
6001 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6002 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003 Entry =
6004 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006005 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006006 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006007 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006008 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006009 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006010 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006011 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006012 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006013 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006014
Benjamin Kramer578faa82011-09-27 21:06:10 +00006015 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006016}
6017
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006018/// EmitMetaClassRef - Return a Value * of the address of _class_t
6019/// meta-data
6020///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006021llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6022 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006023 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6024 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006025 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006026
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006027 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006028 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006029 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006030 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006031 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006032 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006033 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006034 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006035 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006036 ObjCTypes.ClassnfABIPtrTy));
6037
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006038 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006039 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006040
Benjamin Kramer578faa82011-09-27 21:06:10 +00006041 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006042}
6043
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006044/// GetClass - Return a reference to the class for the given interface
6045/// decl.
6046llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6047 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006048 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006049 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6050 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6051 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6052 }
6053
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006054 return EmitClassRef(Builder, ID);
6055}
6056
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006057/// Generates a message send where the super is the receiver. This is
6058/// a message send to self with special delivery semantics indicating
6059/// which class's method should be called.
6060CodeGen::RValue
6061CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006062 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006063 QualType ResultType,
6064 Selector Sel,
6065 const ObjCInterfaceDecl *Class,
6066 bool isCategoryImpl,
6067 llvm::Value *Receiver,
6068 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006069 const CodeGen::CallArgList &CallArgs,
6070 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006071 // ...
6072 // Create and init a super structure; this is a (receiver, class)
6073 // pair we will pass to objc_msgSendSuper.
6074 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006075 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006076
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006077 llvm::Value *ReceiverAsObject =
6078 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6079 CGF.Builder.CreateStore(ReceiverAsObject,
6080 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006081
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006082 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006083 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006084 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006085 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006086 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006087 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006088
Mike Stumpf5408fe2009-05-16 07:57:57 +00006089 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6090 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006091 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006092 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6093 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6094 CGF.Builder.CreateStore(Target,
6095 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006096
John McCall944c8432011-05-14 03:10:52 +00006097 return (isVTableDispatchedSelector(Sel))
6098 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006099 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006100 true, CallArgs, Method)
6101 : EmitMessageSend(CGF, Return, ResultType,
6102 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006103 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006104 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006105}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006106
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006107llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006108 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006109 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006110
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006111 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006112 llvm::Constant *Casted =
6113 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6114 ObjCTypes.SelectorPtrTy);
6115 Entry =
6116 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6117 llvm::GlobalValue::InternalLinkage,
6118 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006119 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006120 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006121 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006122
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006123 if (lval)
6124 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006125 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6126
6127 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6128 llvm::MDNode::get(VMContext,
6129 ArrayRef<llvm::Value*>()));
6130 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006131}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006132/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006133/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006134///
6135void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006136 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006137 llvm::Value *dst,
6138 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006139 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006140 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006141 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006142 assert(Size <= 8 && "does not support size > 8");
6143 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6144 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006145 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6146 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006147 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6148 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006149 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6150 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006151 return;
6152}
6153
6154/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6155/// objc_assign_strongCast (id src, id *dst)
6156///
6157void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006158 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006159 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006160 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006161 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006162 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006163 assert(Size <= 8 && "does not support size > 8");
6164 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006165 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006166 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6167 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006168 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6169 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006170 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006171 src, dst, "weakassign");
6172 return;
6173}
6174
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006175void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006176 CodeGen::CodeGenFunction &CGF,
6177 llvm::Value *DestPtr,
6178 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006179 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006180 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6181 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006182 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006183 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006184 return;
6185}
6186
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006187/// EmitObjCWeakRead - Code gen for loading value of a __weak
6188/// object: objc_read_weak (id *src)
6189///
6190llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006191 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006192 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006193 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006194 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6195 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006196 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006197 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006198 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006199 return read_weak;
6200}
6201
6202/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6203/// objc_assign_weak (id src, id *dst)
6204///
6205void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006206 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006207 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006208 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006209 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006210 assert(Size <= 8 && "does not support size > 8");
6211 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6212 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006213 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6214 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006215 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6216 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006217 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006218 src, dst, "weakassign");
6219 return;
6220}
6221
6222/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6223/// objc_assign_global (id src, id *dst)
6224///
6225void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006226 llvm::Value *src, llvm::Value *dst,
6227 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006228 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006229 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006230 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006231 assert(Size <= 8 && "does not support size > 8");
6232 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6233 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006234 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6235 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006236 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6237 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006238 if (!threadlocal)
6239 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6240 src, dst, "globalassign");
6241 else
6242 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6243 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006244 return;
6245}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006246
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006247void
John McCallf1549f62010-07-06 01:34:17 +00006248CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6249 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006250 EmitAtSynchronizedStmt(CGF, S,
6251 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6252 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006253}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006254
John McCall5a180392010-07-24 00:37:23 +00006255llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006256CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006257 // There's a particular fixed type info for 'id'.
6258 if (T->isObjCIdType() ||
6259 T->isObjCQualifiedIdType()) {
6260 llvm::Constant *IDEHType =
6261 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6262 if (!IDEHType)
6263 IDEHType =
6264 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6265 false,
6266 llvm::GlobalValue::ExternalLinkage,
6267 0, "OBJC_EHTYPE_id");
6268 return IDEHType;
6269 }
6270
6271 // All other types should be Objective-C interface pointer types.
6272 const ObjCObjectPointerType *PT =
6273 T->getAs<ObjCObjectPointerType>();
6274 assert(PT && "Invalid @catch type.");
6275 const ObjCInterfaceType *IT = PT->getInterfaceType();
6276 assert(IT && "Invalid @catch type.");
6277 return GetInterfaceEHType(IT->getDecl(), false);
6278}
6279
John McCallf1549f62010-07-06 01:34:17 +00006280void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6281 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006282 EmitTryCatchStmt(CGF, S,
6283 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6284 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6285 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006286}
6287
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006288/// EmitThrowStmt - Generate code for a throw statement.
6289void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6290 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006291 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006292 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006293 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006294 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006295 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006296 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006297 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006298 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006299 }
6300
John McCall7ec404c2010-10-16 08:21:07 +00006301 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006302 CGF.Builder.ClearInsertionPoint();
6303}
Daniel Dunbare588b992009-03-01 04:46:24 +00006304
John McCall5a180392010-07-24 00:37:23 +00006305llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006306CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006307 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006308 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006309
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006310 // If we don't need a definition, return the entry if found or check
6311 // if we use an external reference.
6312 if (!ForDefinition) {
6313 if (Entry)
6314 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006315
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006316 // If this type (or a super class) has the __objc_exception__
6317 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006318 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006319 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006320 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006321 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006322 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006323 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006324 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006326
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006327 // Otherwise we need to either make a new entry or fill in the
6328 // initializer.
6329 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006330 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006331 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006332 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006333 CGM.getModule().getGlobalVariable(VTableName);
6334 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006335 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6336 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006337 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006338 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006339
Chris Lattner8b418682012-02-07 00:39:47 +00006340 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006341
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006342 llvm::Constant *Values[] = {
6343 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6344 GetClassName(ID->getIdentifier()),
6345 GetClassGlobal(ClassName)
6346 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006347 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006348 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006349
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006350 if (Entry) {
6351 Entry->setInitializer(Init);
6352 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006353 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006354 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006355 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006356 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006357 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006358 }
6359
David Blaikie4e4d0842012-03-11 07:00:24 +00006360 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006361 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006362 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006363 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006364
6365 if (ForDefinition) {
6366 Entry->setSection("__DATA,__objc_const");
6367 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6368 } else {
6369 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6370 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006371
6372 return Entry;
6373}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006374
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006375/* *** */
6376
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006377CodeGen::CGObjCRuntime *
6378CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006379 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6380 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006381 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006382
6383 case ObjCRuntime::MacOSX:
6384 case ObjCRuntime::iOS:
6385 return new CGObjCNonFragileABIMac(CGM);
6386
David Chisnall11d3f4c2012-07-03 20:49:52 +00006387 case ObjCRuntime::GNUstep:
6388 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006389 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006390 llvm_unreachable("these runtimes are not Mac runtimes");
6391 }
6392 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006393}