blob: c89171db5505adc59cee5cb096822302f875df3e [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 };
Bill Wendling0d583392012-10-15 20:36:26 +000066 llvm::AttrBuilder B;
Bill Wendling603571a2012-10-10 07:36:56 +000067 B.addAttribute(llvm::Attributes::NonLazyBind);
John McCall0774cb82011-05-15 01:53:33 +000068 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
69 params, true),
John McCallf85e1932011-06-15 23:02:42 +000070 "objc_msgSend",
Bill Wendling50e6b182012-10-15 04:47:45 +000071 llvm::Attributes::get(CGM.getLLVMContext(),
72 B));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000074
John McCall0774cb82011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000086 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000087
John McCall0774cb82011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
96 params, true),
John McCall0774cb82011-05-15 01:53:33 +000097 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000098
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000099 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000100
Anders Carlssoneea64802011-10-31 16:27:11 +0000101 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
102 ///
103 /// The messenger used when the return value is returned in two values on the
104 /// x87 floating point stack; without a special entrypoint, the nil case
105 /// would be unbalanced. Only used on 64-bit X86.
106 llvm::Constant *getMessageSendFp2retFn() const {
107 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
108 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
109 llvm::Type *resultType =
110 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
111
112 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
113 params, true),
114 "objc_msgSend_fp2ret");
115 }
116
John McCall0774cb82011-05-15 01:53:33 +0000117 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
118 ///
119 /// The messenger used for super calls, which have different dispatch
120 /// semantics. The class passed is the superclass of the current
121 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000122 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000123 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000124 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000125 params, true),
126 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000127 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000128
John McCall0774cb82011-05-15 01:53:33 +0000129 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
130 ///
131 /// A slightly different messenger used for super calls. The class
132 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000133 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000134 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000135 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000136 params, true),
137 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000138 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000139
John McCall0774cb82011-05-15 01:53:33 +0000140 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
141 /// SEL op, ...)
142 ///
143 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000144 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000145 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000146 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000147 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000148 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000149 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000150
John McCall0774cb82011-05-15 01:53:33 +0000151 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
152 /// SEL op, ...)
153 ///
154 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000155 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000156 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000157 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000158 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000160 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000161
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000162 llvm::Constant *getMessageSendSuperFpretFn() const {
163 // There is no objc_msgSendSuper_fpret? How can that work?
164 return getMessageSendSuperFn();
165 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000166
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000167 llvm::Constant *getMessageSendSuperFpretFn2() const {
168 // There is no objc_msgSendSuper_fpret? How can that work?
169 return getMessageSendSuperFn2();
170 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000171
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000172protected:
173 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000174
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000175public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000176 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000177 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000178
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000179 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000180 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000182 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000183 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000184
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000185 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000186 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000187
188private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000189 /// ProtocolPtrTy - LLVM type for external protocol handles
190 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000191 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000192
193public:
194 llvm::Type *getExternalProtocolPtrTy() {
195 if (!ExternalProtocolPtrTy) {
196 // FIXME: It would be nice to unify this with the opaque type, so that the
197 // IR comes out a bit cleaner.
198 CodeGen::CodeGenTypes &Types = CGM.getTypes();
199 ASTContext &Ctx = CGM.getContext();
200 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
201 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
202 }
203
204 return ExternalProtocolPtrTy;
205 }
206
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000207 // SuperCTy - clang type for struct objc_super.
208 QualType SuperCTy;
209 // SuperPtrCTy - clang type for struct objc_super *.
210 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000211
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000212 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000214 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000215 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000216
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
218 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000219 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000220
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000221 /// PropertyListTy - LLVM type for struct objc_property_list
222 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000223 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000224 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000225 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000226
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000227 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000228 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000229
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000230 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000231 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000232 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000233 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000234
Chris Lattner72db6c32009-04-22 02:44:54 +0000235 llvm::Constant *getGetPropertyFn() {
236 CodeGen::CodeGenTypes &Types = CGM.getTypes();
237 ASTContext &Ctx = CGM.getContext();
238 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000240 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
241 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000242 Params.push_back(IdType);
243 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000244 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000245 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000246 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000247 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
248 FunctionType::ExtInfo(),
249 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000250 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
251 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000252
Chris Lattner72db6c32009-04-22 02:44:54 +0000253 llvm::Constant *getSetPropertyFn() {
254 CodeGen::CodeGenTypes &Types = CGM.getTypes();
255 ASTContext &Ctx = CGM.getContext();
256 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000257 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000258 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
259 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000260 Params.push_back(IdType);
261 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000262 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000263 Params.push_back(IdType);
264 Params.push_back(Ctx.BoolTy);
265 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000266 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000267 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
268 FunctionType::ExtInfo(),
269 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000270 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
271 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000272
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000273 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
274 CodeGen::CodeGenTypes &Types = CGM.getTypes();
275 ASTContext &Ctx = CGM.getContext();
276 // void objc_setProperty_atomic(id self, SEL _cmd,
277 // id newValue, ptrdiff_t offset);
278 // void objc_setProperty_nonatomic(id self, SEL _cmd,
279 // id newValue, ptrdiff_t offset);
280 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
281 // id newValue, ptrdiff_t offset);
282 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
283 // id newValue, ptrdiff_t offset);
284
285 SmallVector<CanQualType,4> Params;
286 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
287 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
288 Params.push_back(IdType);
289 Params.push_back(SelType);
290 Params.push_back(IdType);
291 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
292 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000293 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
294 FunctionType::ExtInfo(),
295 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000296 const char *name;
297 if (atomic && copy)
298 name = "objc_setProperty_atomic_copy";
299 else if (atomic && !copy)
300 name = "objc_setProperty_atomic";
301 else if (!atomic && copy)
302 name = "objc_setProperty_nonatomic_copy";
303 else
304 name = "objc_setProperty_nonatomic";
305
306 return CGM.CreateRuntimeFunction(FTy, name);
307 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000308
309 llvm::Constant *getCopyStructFn() {
310 CodeGen::CodeGenTypes &Types = CGM.getTypes();
311 ASTContext &Ctx = CGM.getContext();
312 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000313 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000314 Params.push_back(Ctx.VoidPtrTy);
315 Params.push_back(Ctx.VoidPtrTy);
316 Params.push_back(Ctx.LongTy);
317 Params.push_back(Ctx.BoolTy);
318 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000319 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000320 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
321 FunctionType::ExtInfo(),
322 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000323 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
324 }
325
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000326 /// This routine declares and returns address of:
327 /// void objc_copyCppObjectAtomic(
328 /// void *dest, const void *src,
329 /// void (*copyHelper) (void *dest, const void *source));
330 llvm::Constant *getCppAtomicObjectFunction() {
331 CodeGen::CodeGenTypes &Types = CGM.getTypes();
332 ASTContext &Ctx = CGM.getContext();
333 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
334 SmallVector<CanQualType,3> Params;
335 Params.push_back(Ctx.VoidPtrTy);
336 Params.push_back(Ctx.VoidPtrTy);
337 Params.push_back(Ctx.VoidPtrTy);
338 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000339 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
340 FunctionType::ExtInfo(),
341 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000342 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
343 }
344
Chris Lattner72db6c32009-04-22 02:44:54 +0000345 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000346 CodeGen::CodeGenTypes &Types = CGM.getTypes();
347 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000348 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000349 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000350 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000351 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000352 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000353 FunctionType::ExtInfo(),
354 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000355 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
356 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000357
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000358 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000359 llvm::Constant *getGcReadWeakFn() {
360 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000361 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000362 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000363 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000364 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000365 }
366
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000367 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000368 llvm::Constant *getGcAssignWeakFn() {
369 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000370 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000371 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000372 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000373 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
374 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000375
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000376 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000377 llvm::Constant *getGcAssignGlobalFn() {
378 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000379 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000380 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000381 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000382 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
383 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000384
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000385 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
386 llvm::Constant *getGcAssignThreadLocalFn() {
387 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000388 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000389 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000390 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000391 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
392 }
393
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000394 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000395 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000396 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000397 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
398 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000399 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000400 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000401 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
402 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000403
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000404 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
405 llvm::Constant *GcMemmoveCollectableFn() {
406 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000407 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000408 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000409 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000411
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000412 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000413 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000414 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000415 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000416 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000417 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
419 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000420
421 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000422 llvm::Constant *getExceptionThrowFn() {
423 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000424 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000425 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000426 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000427 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
428 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000429
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000430 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
431 llvm::Constant *getExceptionRethrowFn() {
432 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000433 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000434 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
435 }
436
Daniel Dunbar1c566672009-02-24 01:43:46 +0000437 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000438 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000439 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000440 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000441 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000442 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000443 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
444 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000445
Daniel Dunbar1c566672009-02-24 01:43:46 +0000446 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000447 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000448 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000449 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000450 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000451 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000452 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
453 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000454
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000455 llvm::Constant *getSendFn(bool IsSuper) const {
456 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
457 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000458
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000459 llvm::Constant *getSendFn2(bool IsSuper) const {
460 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
461 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000462
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000463 llvm::Constant *getSendStretFn(bool IsSuper) const {
464 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
465 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000466
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000467 llvm::Constant *getSendStretFn2(bool IsSuper) const {
468 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
469 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000470
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000471 llvm::Constant *getSendFpretFn(bool IsSuper) const {
472 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
473 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000474
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000475 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
476 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
477 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000478
Anders Carlssoneea64802011-10-31 16:27:11 +0000479 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
480 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
481 }
482
483 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
485 }
486
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000487 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
488 ~ObjCCommonTypesHelper(){}
489};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000490
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000491/// ObjCTypesHelper - Helper class that encapsulates lazy
492/// construction of varies types used during ObjC generation.
493class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000494public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000495 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000496 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000499 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000500 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000501
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000502 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000503 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000505 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000506 /// ProtocolExtensionTy - LLVM type for struct
507 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000508 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000509 /// ProtocolExtensionTy - LLVM type for struct
510 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000511 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000512 /// MethodDescriptionTy - LLVM type for struct
513 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000514 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000515 /// MethodDescriptionListTy - LLVM type for struct
516 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000517 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000518 /// MethodDescriptionListPtrTy - LLVM type for struct
519 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000520 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000522 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000523 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000524 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000525 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000526 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000527 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000528 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000529 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000530 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000532 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000533 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000535 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000536 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000537 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000538 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000539 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000541 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000542 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000543 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000544 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000545
Anders Carlsson124526b2008-09-09 10:10:21 +0000546 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000547 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000548
Anders Carlsson124526b2008-09-09 10:10:21 +0000549 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000550 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000551 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000552 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000553 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000554 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000555 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000556
557 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000558 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000559 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000560 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000561 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000562 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000563 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000564
565 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000566 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000568 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000569 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000571 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572
Anders Carlsson124526b2008-09-09 10:10:21 +0000573 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000574 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000575 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000577 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000578 "objc_exception_match");
579
Chris Lattner34b02a12009-04-22 02:26:14 +0000580 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581
Anders Carlsson124526b2008-09-09 10:10:21 +0000582 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000583 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000584 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000585 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
Bill Wendling0d583392012-10-15 20:36:26 +0000586 llvm::AttrBuilder B;
Bill Wendling603571a2012-10-10 07:36:56 +0000587 B.addAttribute(llvm::Attributes::NonLazyBind);
John McCall0774cb82011-05-15 01:53:33 +0000588 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
589 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000590 "_setjmp",
Bill Wendling50e6b182012-10-15 04:47:45 +0000591 llvm::Attributes::get(CGM.getLLVMContext(),
592 B));
Chris Lattner34b02a12009-04-22 02:26:14 +0000593 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000594
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000595public:
596 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000598};
599
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000600/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000601/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000602class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000603public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000605 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000607
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000608 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000609 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000610
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000611 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000612 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000613
Daniel Dunbar948e2582009-02-15 07:36:20 +0000614 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000615 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000616
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000617 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000618 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000620 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000621 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000622
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000623 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000624 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000625
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000626 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000627 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000628
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000629 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000630 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000631
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000632 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000633 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000634
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000635 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000636 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000637
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000638 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000639 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000640
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000641 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000642 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000643
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000644 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000645 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000647 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000648
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000649 // MessageRefTy - LLVM for:
650 // struct _message_ref_t {
651 // IMP messenger;
652 // SEL name;
653 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000654 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000655 // MessageRefCTy - clang type for struct _message_ref_t
656 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000658 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000659 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000660 // MessageRefCPtrTy - clang type for struct _message_ref_t*
661 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000662
Fariborz Jahanianef163782009-02-05 01:13:09 +0000663 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000664 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000666 // SuperMessageRefTy - LLVM for:
667 // struct _super_message_ref_t {
668 // SUPER_IMP messenger;
669 // SEL name;
670 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000671 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000672
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000673 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000674 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000675
Chris Lattner1c02f862009-04-22 02:53:24 +0000676 llvm::Constant *getMessageSendFixupFn() {
677 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000678 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000679 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000680 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000681 "objc_msgSend_fixup");
682 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000683
Chris Lattner1c02f862009-04-22 02:53:24 +0000684 llvm::Constant *getMessageSendFpretFixupFn() {
685 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000686 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000687 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000688 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000689 "objc_msgSend_fpret_fixup");
690 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691
Chris Lattner1c02f862009-04-22 02:53:24 +0000692 llvm::Constant *getMessageSendStretFixupFn() {
693 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000694 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000695 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000696 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000697 "objc_msgSend_stret_fixup");
698 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000701 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000703 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000704 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000705 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000706 "objc_msgSendSuper2_fixup");
707 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000708
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000710 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000711 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000712 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000714 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000715 "objc_msgSendSuper2_stret_fixup");
716 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner8a569112009-04-22 02:15:23 +0000718 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000719 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000720 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721
Chris Lattner8a569112009-04-22 02:15:23 +0000722 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000723
Chris Lattner8a569112009-04-22 02:15:23 +0000724 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000725 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000726 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000727 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000728 "objc_begin_catch");
729 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000730
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000731 llvm::StructType *EHTypeTy;
732 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000733
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000734 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
735 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000736};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000737
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000738class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000739public:
740 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000741 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000742 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000743 unsigned ivar_bytepos;
744 unsigned ivar_size;
745 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000746 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000747
748 // Allow sorting based on byte pos.
749 bool operator<(const GC_IVAR &b) const {
750 return ivar_bytepos < b.ivar_bytepos;
751 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000752 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000754 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000755 public:
756 unsigned skip;
757 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000758 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000759 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000760 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762protected:
Owen Anderson69243822009-07-13 04:10:07 +0000763 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000764 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000765 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000767 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000768 SmallVector<GC_IVAR, 16> SkipIvars;
769 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000771 /// LazySymbols - Symbols to generate a lazy reference for. See
772 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000773 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000774
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000775 /// DefinedSymbols - External symbols which are defined by this
776 /// module. The symbols in this list and LazySymbols are used to add
777 /// special linker symbols which ensure that Objective-C modules are
778 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000779 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000780
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000781 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000782 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000783
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000784 /// MethodVarNames - uniqued method variable names.
785 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000786
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000787 /// DefinedCategoryNames - list of category names in form Class_Category.
788 llvm::SetVector<std::string> DefinedCategoryNames;
789
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000790 /// MethodVarTypes - uniqued method type signatures. We have to use
791 /// a StringMap here because have no other unique reference.
792 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000793
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000794 /// MethodDefinitions - map of methods which have been defined in
795 /// this translation unit.
796 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000797
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000798 /// PropertyNames - uniqued method variable names.
799 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000800
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000801 /// ClassReferences - uniqued class references.
802 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000803
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000804 /// SelectorReferences - uniqued selector references.
805 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000806
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000807 /// Protocols - Protocols for which an objc_protocol structure has
808 /// been emitted. Forward declarations are handled by creating an
809 /// empty structure whose initializer is filled in when/if defined.
810 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000811
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000812 /// DefinedProtocols - Protocols which have actually been
813 /// defined. We should not need this, see FIXME in GenerateProtocol.
814 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000816 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000817 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000818
819 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000820 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000821
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000822 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000823 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000824
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000825 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000826 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000827
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000828 /// GetNameForMethod - Return a name for the given method.
829 /// \param[out] NameOut - The return value.
830 void GetNameForMethod(const ObjCMethodDecl *OMD,
831 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000832 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000833
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000834 /// GetMethodVarName - Return a unique constant for the given
835 /// selector's name. The return value has type char *.
836 llvm::Constant *GetMethodVarName(Selector Sel);
837 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000839 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000840 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000841
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000842 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000843 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
844 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000845 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000847 /// GetPropertyName - Return a unique constant for the given
848 /// name. The return value has type char *.
849 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000851 // FIXME: This can be dropped once string functions are unified.
852 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
853 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000855 /// GetClassName - Return a unique constant for the given selector's
856 /// name. The return value has type char *.
857 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000858
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000859 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
860
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000861 /// BuildIvarLayout - Builds ivar layout bitmap for the class
862 /// implementation for the __strong or __weak case.
863 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000864 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
865 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000866
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000867 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000869 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000870 unsigned int BytePos, bool ForStrongLayout,
871 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000872 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000873 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000874 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000875 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000876 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000877 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000878
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000879 /// GetIvarLayoutName - Returns a unique constant for the given
880 /// ivar layout bitmap.
881 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
882 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000884 /// EmitPropertyList - Emit the given property list. The return
885 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000886 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000887 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000888 const ObjCContainerDecl *OCD,
889 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Bob Wilsondc8dab62011-11-30 01:57:58 +0000891 /// EmitProtocolMethodTypes - Generate the array of extended method type
892 /// strings. The return value has type Int8PtrPtrTy.
893 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000894 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000895 const ObjCCommonTypesHelper &ObjCTypes);
896
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000897 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000898 void PushProtocolProperties(
899 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
900 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
901 const Decl *Container,
902 const ObjCProtocolDecl *PROTO,
903 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000904
Fariborz Jahanianda320092009-01-29 19:24:30 +0000905 /// GetProtocolRef - Return a reference to the internal protocol
906 /// description, creating an empty one if it has not been
907 /// defined. The return value has type ProtocolPtrTy.
908 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000909
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000910 /// CreateMetadataVar - Create a global variable with internal
911 /// linkage for use by the Objective-C runtime.
912 ///
913 /// This is a convenience wrapper which not only creates the
914 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000915 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000916 ///
917 /// \param Name - The variable name.
918 /// \param Init - The variable initializer; this is also used to
919 /// define the type of the variable.
920 /// \param Section - The section the variable should go into, or 0.
921 /// \param Align - The alignment for the variable, or 0.
922 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000923 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000924 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000925 llvm::Constant *Init,
926 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000927 unsigned Align,
928 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000929
John McCall944c8432011-05-14 03:10:52 +0000930 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
931 ReturnValueSlot Return,
932 QualType ResultType,
933 llvm::Value *Sel,
934 llvm::Value *Arg0,
935 QualType Arg0Ty,
936 bool IsSuper,
937 const CallArgList &CallArgs,
938 const ObjCMethodDecl *OMD,
939 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000940
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000941 /// EmitImageInfo - Emit the image info marker used to encode some module
942 /// level information.
943 void EmitImageInfo();
944
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000945public:
Owen Anderson69243822009-07-13 04:10:07 +0000946 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +0000947 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000948
David Chisnall0d13f6f2010-01-23 02:40:42 +0000949 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000950
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000951 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
952 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000953
Fariborz Jahanianda320092009-01-29 19:24:30 +0000954 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000955
Fariborz Jahanianda320092009-01-29 19:24:30 +0000956 /// GetOrEmitProtocol - Get the protocol object for the given
957 /// declaration, emitting it if necessary. The return value has type
958 /// ProtocolPtrTy.
959 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000960
Fariborz Jahanianda320092009-01-29 19:24:30 +0000961 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
962 /// object for the given declaration, emitting it if needed. These
963 /// forward references will be filled in with empty bodies if no
964 /// definition is seen. The return value has type ProtocolPtrTy.
965 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000966 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
967 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000968
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000969};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000970
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000971class CGObjCMac : public CGObjCCommonMac {
972private:
973 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000974
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000975 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000976 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000977 void EmitModuleInfo();
978
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000979 /// EmitModuleSymols - Emit module symbols, the list of defined
980 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000981 llvm::Constant *EmitModuleSymbols();
982
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000983 /// FinishModule - Write out global data structures at the end of
984 /// processing a translation unit.
985 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000986
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987 /// EmitClassExtension - Generate the class extension structure used
988 /// to store the weak ivar layout and properties. The return value
989 /// has type ClassExtensionPtrTy.
990 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
991
992 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
993 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000994 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000995 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000996
John McCallf85e1932011-06-15 23:02:42 +0000997 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
998 IdentifierInfo *II);
999
1000 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1001
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001002 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1003 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001004
1005 /// EmitIvarList - Emit the ivar list for the given
1006 /// implementation. If ForClass is true the list of class ivars
1007 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1008 /// interface ivars will be emitted. The return value has type
1009 /// IvarListPtrTy.
1010 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001011 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001012
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001013 /// EmitMetaClass - Emit a forward reference to the class structure
1014 /// for the metaclass of the given interface. The return value has
1015 /// type ClassPtrTy.
1016 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1017
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001019 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001020 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1021 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001022 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001023
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001024 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001025
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001026 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001027
1028 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001029 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001030 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001031 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001032 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001033
1034 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001035 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001036 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001037 /// - IsProtocol: True iff these methods are for a protocol.
1038 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039 /// - Required: When true, only "required" methods are
1040 /// listed. Similarly, when false only "optional" methods are
1041 /// listed. For classes this should always be true.
1042 /// - begin, end: The method list to output.
1043 ///
1044 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001045 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001046 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001047 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001048
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001049 /// GetOrEmitProtocol - Get the protocol object for the given
1050 /// declaration, emitting it if necessary. The return value has type
1051 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001052 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001053
1054 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1055 /// object for the given declaration, emitting it if needed. These
1056 /// forward references will be filled in with empty bodies if no
1057 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001058 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001059
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001060 /// EmitProtocolExtension - Generate the protocol extension
1061 /// structure used to store optional instance and class methods, and
1062 /// protocol properties. The return value has type
1063 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064 llvm::Constant *
1065 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001066 ArrayRef<llvm::Constant*> OptInstanceMethods,
1067 ArrayRef<llvm::Constant*> OptClassMethods,
1068 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001069
1070 /// EmitProtocolList - Generate the list of referenced
1071 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001072 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001073 ObjCProtocolDecl::protocol_iterator begin,
1074 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001076 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1077 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001078 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1079 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001080
1081public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001082 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001083
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001084 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001085
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001086 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001087 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001088 QualType ResultType,
1089 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001090 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001091 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001092 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001093 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001094
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001095 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001096 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001097 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001098 QualType ResultType,
1099 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001100 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001101 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001102 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001103 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001104 const CallArgList &CallArgs,
1105 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001106
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001107 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001108 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001109
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001110 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1111 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001112
1113 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1114 /// untyped one.
1115 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1116 const ObjCMethodDecl *Method);
1117
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001118 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001119
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001120 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001121
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001122 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001123
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001124 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001125
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001126 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001127 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001128
Chris Lattner74391b42009-03-22 21:03:39 +00001129 virtual llvm::Constant *GetPropertyGetFunction();
1130 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001131 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1132 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001133 virtual llvm::Constant *GetGetStructFunction();
1134 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001135 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001136 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001137
John McCallf1549f62010-07-06 01:34:17 +00001138 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1139 const ObjCAtTryStmt &S);
1140 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1141 const ObjCAtSynchronizedStmt &S);
1142 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001143 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1144 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001145 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001146 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001147 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001148 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001149 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001150 llvm::Value *src, llvm::Value *dest,
1151 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001152 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001153 llvm::Value *src, llvm::Value *dest,
1154 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001155 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1156 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001157 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1158 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001159 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001160
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001161 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1162 QualType ObjectTy,
1163 llvm::Value *BaseValue,
1164 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001165 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001166 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001167 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001168 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001169
1170 /// GetClassGlobal - Return the global variable for the Objective-C
1171 /// class of the given name.
1172 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001173 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001174 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001175};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001177class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001178private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001179 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001180 llvm::GlobalVariable* ObjCEmptyCacheVar;
1181 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001182
Daniel Dunbar11394522009-04-18 08:51:00 +00001183 /// SuperClassReferences - uniqued super class references.
1184 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001185
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001186 /// MetaClassReferences - uniqued meta class references.
1187 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001188
1189 /// EHTypeReferences - uniqued class ehtype references.
1190 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001191
John McCall944c8432011-05-14 03:10:52 +00001192 /// VTableDispatchMethods - List of methods for which we generate
1193 /// vtable-based message dispatch.
1194 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001195
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001196 /// DefinedMetaClasses - List of defined meta-classes.
1197 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1198
John McCall944c8432011-05-14 03:10:52 +00001199 /// isVTableDispatchedSelector - Returns true if SEL is a
1200 /// vtable-based selector.
1201 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001203 /// FinishNonFragileABIModule - Write out global data structures at the end of
1204 /// processing a translation unit.
1205 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001206
Daniel Dunbar463b8762009-05-15 21:48:48 +00001207 /// AddModuleClassList - Add the given list of class pointers to the
1208 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001209 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001210 const char *SymbolName,
1211 const char *SectionName);
1212
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001213 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1214 unsigned InstanceStart,
1215 unsigned InstanceSize,
1216 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001217 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001218 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001219 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001220 llvm::Constant *ClassRoGV,
1221 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001222
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001223 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001224
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001225 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001226
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001227 /// EmitMethodList - Emit the method list for the given
1228 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001229 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001230 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001231 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001232 /// EmitIvarList - Emit the ivar list for the given
1233 /// implementation. If ForClass is true the list of class ivars
1234 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1235 /// interface ivars will be emitted. The return value has type
1236 /// IvarListnfABIPtrTy.
1237 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001239 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001240 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001241 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
Fariborz Jahanianda320092009-01-29 19:24:30 +00001243 /// GetOrEmitProtocol - Get the protocol object for the given
1244 /// declaration, emitting it if necessary. The return value has type
1245 /// ProtocolPtrTy.
1246 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001247
Fariborz Jahanianda320092009-01-29 19:24:30 +00001248 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1249 /// object for the given declaration, emitting it if needed. These
1250 /// forward references will be filled in with empty bodies if no
1251 /// definition is seen. The return value has type ProtocolPtrTy.
1252 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001253
Fariborz Jahanianda320092009-01-29 19:24:30 +00001254 /// EmitProtocolList - Generate the list of referenced
1255 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001256 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001257 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001258 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001259
John McCall944c8432011-05-14 03:10:52 +00001260 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1261 ReturnValueSlot Return,
1262 QualType ResultType,
1263 Selector Sel,
1264 llvm::Value *Receiver,
1265 QualType Arg0Ty,
1266 bool IsSuper,
1267 const CallArgList &CallArgs,
1268 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001269
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001270 /// GetClassGlobal - Return the global variable for the Objective-C
1271 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001272 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001273
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001274 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001275 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001276 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001277 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001278
1279 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1280 IdentifierInfo *II);
1281
1282 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001283
Daniel Dunbar11394522009-04-18 08:51:00 +00001284 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1285 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001286 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1287 const ObjCInterfaceDecl *ID);
1288
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001289 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1290 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001291 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001292 const ObjCInterfaceDecl *ID);
1293
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001294 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1295 /// the given ivar.
1296 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001297 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001298 const ObjCInterfaceDecl *ID,
1299 const ObjCIvarDecl *Ivar);
1300
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001301 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1302 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001303 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1304 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001305
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001306 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001307 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001308 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001309 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001310
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001311 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001312 return "OBJC_METACLASS_$_";
1313 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001315 const char *getClassSymbolPrefix() const {
1316 return "OBJC_CLASS_$_";
1317 }
1318
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001319 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001320 uint32_t &InstanceStart,
1321 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001322
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001323 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001324 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001325 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1326 return CGM.getContext().Selectors.getSelector(0, &II);
1327 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001328
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001329 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001330 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1331 return CGM.getContext().Selectors.getSelector(1, &II);
1332 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001333
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001334 /// ImplementationIsNonLazy - Check whether the given category or
1335 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001336 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001337
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001338public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001339 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001340 // FIXME. All stubs for now!
1341 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001342
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001343 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001344 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001345 QualType ResultType,
1346 Selector Sel,
1347 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001348 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001349 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001350 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001351
1352 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001354 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 QualType ResultType,
1356 Selector Sel,
1357 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001358 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001359 llvm::Value *Receiver,
1360 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001361 const CallArgList &CallArgs,
1362 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001363
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001364 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001365 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001366
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001367 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1368 bool lvalue = false)
1369 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001370
1371 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1372 /// untyped one.
1373 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1374 const ObjCMethodDecl *Method)
1375 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001376
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001377 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001378
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001379 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001380
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001381 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001382
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001383 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001384 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001385
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001386 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001387
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001388 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001389 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001390 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001391 virtual llvm::Constant *GetPropertySetFunction() {
1392 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001393 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001394
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001395 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1396 bool copy) {
1397 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1398 }
1399
David Chisnall8fac25d2010-12-26 22:13:16 +00001400 virtual llvm::Constant *GetSetStructFunction() {
1401 return ObjCTypes.getCopyStructFn();
1402 }
1403 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001404 return ObjCTypes.getCopyStructFn();
1405 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001406 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1407 return ObjCTypes.getCppAtomicObjectFunction();
1408 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001409
Chris Lattner74391b42009-03-22 21:03:39 +00001410 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001411 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001412 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001413
John McCallf1549f62010-07-06 01:34:17 +00001414 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1415 const ObjCAtTryStmt &S);
1416 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1417 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001419 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001420 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001421 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001423 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001424 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001425 llvm::Value *src, llvm::Value *dest,
1426 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001427 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001428 llvm::Value *src, llvm::Value *dest,
1429 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001430 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001431 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001432 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1433 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001434 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001435 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1436 QualType ObjectTy,
1437 llvm::Value *BaseValue,
1438 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001439 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001440 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001441 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001442 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001443};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001444
1445/// A helper class for performing the null-initialization of a return
1446/// value.
1447struct NullReturnState {
1448 llvm::BasicBlock *NullBB;
1449 llvm::BasicBlock *callBB;
1450 NullReturnState() : NullBB(0), callBB(0) {}
1451
1452 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1453 // Make blocks for the null-init and call edges.
1454 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1455 callBB = CGF.createBasicBlock("msgSend.call");
1456
1457 // Check for a null receiver and, if there is one, jump to the
1458 // null-init test.
1459 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1460 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1461
1462 // Otherwise, start performing the call.
1463 CGF.EmitBlock(callBB);
1464 }
1465
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001466 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1467 const CallArgList &CallArgs,
1468 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001469 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001470
1471 llvm::Value *NullInitPtr = 0;
1472 if (result.isScalar() && !resultType->isVoidType()) {
1473 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1474 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1475 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001476
1477 // Finish the call path.
1478 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1479 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1480
1481 // Emit the null-init block and perform the null-initialization there.
1482 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001483
1484 // Release consumed arguments along the null-receiver path.
1485 if (Method) {
1486 CallArgList::const_iterator I = CallArgs.begin();
1487 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1488 e = Method->param_end(); i != e; ++i, ++I) {
1489 const ParmVarDecl *ParamDecl = (*i);
1490 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1491 RValue RV = I->RV;
1492 assert(RV.isScalar() &&
1493 "NullReturnState::complete - arg not on object");
1494 CGF.EmitARCRelease(RV.getScalarVal(), true);
1495 }
1496 }
1497 }
1498
1499 if (result.isScalar()) {
1500 if (NullInitPtr)
1501 CGF.EmitNullInitialization(NullInitPtr, resultType);
1502 // Jump to the continuation block.
1503 CGF.EmitBlock(contBB);
1504 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1505 : result;
1506 }
1507
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001508 if (!resultType->isAnyComplexType()) {
1509 assert(result.isAggregate() && "null init of non-aggregate result?");
1510 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1511 // Jump to the continuation block.
1512 CGF.EmitBlock(contBB);
1513 return result;
1514 }
1515
1516 // _Complex type
1517 // FIXME. Now easy to handle any other scalar type whose result is returned
1518 // in memory due to ABI limitations.
1519 CGF.EmitBlock(contBB);
1520 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1521 llvm::Type *MemberType = CallCV.first->getType();
1522 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1523 // Create phi instruction for scalar complex value.
1524 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1525 PHIReal->addIncoming(ZeroCV, NullBB);
1526 PHIReal->addIncoming(CallCV.first, callBB);
1527 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1528 PHIImag->addIncoming(ZeroCV, NullBB);
1529 PHIImag->addIncoming(CallCV.second, callBB);
1530 return RValue::getComplex(PHIReal, PHIImag);
1531 }
1532};
1533
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001534} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001535
1536/* *** Helper Functions *** */
1537
1538/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001539static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001540 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001541 unsigned idx0,
1542 unsigned idx1) {
1543 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001544 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1545 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001546 };
Jay Foada5c04342011-07-21 14:31:17 +00001547 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001548}
1549
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001550/// hasObjCExceptionAttribute - Return true if this class or any super
1551/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001552static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001553 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001554 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001555 return true;
1556 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001557 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001558 return false;
1559}
1560
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001561/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001562
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001563CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001564 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001565 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001566 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001567}
1568
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001569/// GetClass - Return a reference to the class for the given interface
1570/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001571llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001572 const ObjCInterfaceDecl *ID) {
1573 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001574}
1575
1576/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001577llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1578 bool lval) {
1579 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001580}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001581llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001582 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001583 return EmitSelector(Builder, Method->getSelector());
1584}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001585
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001586llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001587 if (T->isObjCIdType() ||
1588 T->isObjCQualifiedIdType()) {
1589 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001590 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001591 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001592 if (T->isObjCClassType() ||
1593 T->isObjCQualifiedClassType()) {
1594 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001595 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001596 }
1597 if (T->isObjCObjectPointerType())
1598 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1599
John McCall5a180392010-07-24 00:37:23 +00001600 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001601}
1602
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001603/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001604/*
1605 struct __builtin_CFString {
1606 const int *isa; // point to __CFConstantStringClassReference
1607 int flags;
1608 const char *str;
1609 long length;
1610 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001611*/
1612
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001613/// or Generate a constant NSString object.
1614/*
1615 struct __builtin_NSString {
1616 const int *isa; // point to __NSConstantStringClassReference
1617 const char *str;
1618 unsigned int length;
1619 };
1620*/
1621
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001622llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001623 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001624 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001625 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001626 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001627}
1628
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001629enum {
1630 kCFTaggedObjectID_Integer = (1 << 1) + 1
1631};
1632
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001633/// Generates a message send where the super is the receiver. This is
1634/// a message send to self with special delivery semantics indicating
1635/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001636CodeGen::RValue
1637CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001638 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001639 QualType ResultType,
1640 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001641 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001642 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001643 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001644 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001645 const CodeGen::CallArgList &CallArgs,
1646 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001647 // Create and init a super structure; this is a (receiver, class)
1648 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001649 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001650 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001651 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001652 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001653 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001654 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001655
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001656 // If this is a class message the metaclass is passed as the target.
1657 llvm::Value *Target;
1658 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001659 if (isCategoryImpl) {
1660 // Message sent to 'super' in a class method defined in a category
1661 // implementation requires an odd treatment.
1662 // If we are in a class method, we must retrieve the
1663 // _metaclass_ for the current class, pointed at by
1664 // the class's "isa" pointer. The following assumes that
1665 // isa" is the first ivar in a class (which it must be).
1666 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1667 Target = CGF.Builder.CreateStructGEP(Target, 0);
1668 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001669 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001670 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1671 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1672 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1673 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001674 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001675 }
1676 else if (isCategoryImpl)
1677 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1678 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001679 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1680 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1681 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001682 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001683 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1684 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001685 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001686 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001687 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001688 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001689 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001690 return EmitMessageSend(CGF, Return, ResultType,
1691 EmitSelector(CGF.Builder, Sel),
1692 ObjCSuper, ObjCTypes.SuperPtrCTy,
1693 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001694}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001695
1696/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001697CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001698 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001699 QualType ResultType,
1700 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001701 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001702 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001703 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001704 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001705 return EmitMessageSend(CGF, Return, ResultType,
1706 EmitSelector(CGF.Builder, Sel),
1707 Receiver, CGF.getContext().getObjCIdType(),
1708 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001709}
1710
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001711CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001712CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1713 ReturnValueSlot Return,
1714 QualType ResultType,
1715 llvm::Value *Sel,
1716 llvm::Value *Arg0,
1717 QualType Arg0Ty,
1718 bool IsSuper,
1719 const CallArgList &CallArgs,
1720 const ObjCMethodDecl *Method,
1721 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001722 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001723 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001724 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001725 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1726 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001727 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001728
John McCallde5d3c72012-02-17 03:33:10 +00001729 // If we're calling a method, use the formal signature.
1730 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001731
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001732 if (Method)
1733 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1734 CGM.getContext().getCanonicalType(ResultType) &&
1735 "Result type mismatch!");
1736
John McCallcba681a2011-05-14 21:12:11 +00001737 NullReturnState nullReturn;
1738
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001739 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001740 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001741 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001742 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001743 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001744 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1745 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1746 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001747 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1748 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1749 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001750 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001751 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001752 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001753 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001754
1755 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001756 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001757 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1758 e = Method->param_end(); i != e; ++i) {
1759 const ParmVarDecl *ParamDecl = (*i);
1760 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1761 if (!nullReturn.NullBB)
1762 nullReturn.init(CGF, Arg0);
1763 requiresnullCheck = true;
1764 break;
1765 }
1766 }
1767
John McCallde5d3c72012-02-17 03:33:10 +00001768 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1769 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001770 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1771 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001772}
1773
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001774static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1775 if (FQT.isObjCGCStrong())
1776 return Qualifiers::Strong;
1777
John McCallf85e1932011-06-15 23:02:42 +00001778 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001779 return Qualifiers::Weak;
1780
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001781 // check for __unsafe_unretained
1782 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1783 return Qualifiers::GCNone;
1784
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001785 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1786 return Qualifiers::Strong;
1787
1788 if (const PointerType *PT = FQT->getAs<PointerType>())
1789 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1790
1791 return Qualifiers::GCNone;
1792}
1793
John McCall6b5a61b2011-02-07 10:33:21 +00001794llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1795 const CGBlockInfo &blockInfo) {
Chris Lattner8b418682012-02-07 00:39:47 +00001796 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall6b5a61b2011-02-07 10:33:21 +00001797
David Blaikie4e4d0842012-03-11 07:00:24 +00001798 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1799 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001800 return nullPtr;
1801
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001802 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001803 SkipIvars.clear();
1804 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001805 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1806 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001807
Fariborz Jahanian81979822010-09-09 00:21:45 +00001808 // __isa is the first field in block descriptor and must assume by runtime's
1809 // convention that it is GC'able.
1810 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001811
1812 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1813
1814 // Calculate the basic layout of the block structure.
1815 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001816 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001817
1818 // Ignore the optional 'this' capture: C++ objects are not assumed
1819 // to be GC'ed.
1820
1821 // Walk the captured variables.
1822 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1823 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1824 const VarDecl *variable = ci->getVariable();
1825 QualType type = variable->getType();
1826
1827 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1828
1829 // Ignore constant captures.
1830 if (capture.isConstant()) continue;
1831
1832 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1833
1834 // __block variables are passed by their descriptor address.
1835 if (ci->isByRef()) {
1836 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001837 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001838 }
1839
1840 assert(!type->isArrayType() && "array variable should not be caught");
1841 if (const RecordType *record = type->getAs<RecordType>()) {
1842 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001843 continue;
1844 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001845
John McCall6b5a61b2011-02-07 10:33:21 +00001846 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1847 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1848
1849 if (GCAttr == Qualifiers::Strong)
1850 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1851 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001852 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001853 SkipIvars.push_back(GC_IVAR(fieldOffset,
1854 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001855 }
1856
1857 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001858 return nullPtr;
1859
1860 // Sort on byte position; captures might not be allocated in order,
1861 // and unions can do funny things.
1862 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1863 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001864
1865 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001866 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001867 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001868 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001869 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001870 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001871 if (!(s[i] & 0xf0))
1872 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1873 else
1874 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1875 printf("\n");
1876 }
1877
1878 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001879}
1880
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001881llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001882 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001883 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001884 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001885 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1886
Owen Anderson3c4972d2009-07-29 18:54:39 +00001887 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001888 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001889}
1890
Fariborz Jahanianda320092009-01-29 19:24:30 +00001891void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001892 // FIXME: We shouldn't need this, the protocol decl should contain enough
1893 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001894 DefinedProtocols.insert(PD->getIdentifier());
1895
1896 // If we have generated a forward reference to this protocol, emit
1897 // it now. Otherwise do nothing, the protocol objects are lazily
1898 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001899 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001900 GetOrEmitProtocol(PD);
1901}
1902
Fariborz Jahanianda320092009-01-29 19:24:30 +00001903llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001904 if (DefinedProtocols.count(PD->getIdentifier()))
1905 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001906
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001907 return GetOrEmitProtocolRef(PD);
1908}
1909
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001910/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001911// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1912struct _objc_protocol {
1913struct _objc_protocol_extension *isa;
1914char *protocol_name;
1915struct _objc_protocol_list *protocol_list;
1916struct _objc__method_prototype_list *instance_methods;
1917struct _objc__method_prototype_list *class_methods
1918};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001920See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001921*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001922llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00001923 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001924
1925 // Early exit if a defining object has already been generated.
1926 if (Entry && Entry->hasInitializer())
1927 return Entry;
1928
Douglas Gregor1d784b22012-01-01 19:51:50 +00001929 // Use the protocol definition, if there is one.
1930 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1931 PD = Def;
1932
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001933 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001934 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001935 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1936
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001937 // Construct method lists.
1938 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1939 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001940 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001941 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001942 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001943 ObjCMethodDecl *MD = *i;
1944 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001945 if (!C)
1946 return GetOrEmitProtocolRef(PD);
1947
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001948 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1949 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001950 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001951 } else {
1952 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001953 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001954 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001955 }
1956
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001957 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001958 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001959 ObjCMethodDecl *MD = *i;
1960 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001961 if (!C)
1962 return GetOrEmitProtocolRef(PD);
1963
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001964 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1965 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001966 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001967 } else {
1968 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001969 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001970 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001971 }
1972
Bob Wilsondc8dab62011-11-30 01:57:58 +00001973 MethodTypesExt.insert(MethodTypesExt.end(),
1974 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1975
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001976 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001977 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1978 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001979 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001980 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001981 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001982 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001983 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001984 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001985 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001986 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001987 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001988 ClassMethods)
1989 };
Owen Anderson08e25242009-07-27 22:29:56 +00001990 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001991 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001992
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001993 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001994 // Already created, fix the linkage and update the initializer.
1995 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001996 Entry->setInitializer(Init);
1997 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001998 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001999 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002000 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002001 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002002 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002003 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002004 // FIXME: Is this necessary? Why only for protocol?
2005 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002006
2007 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002008 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002009 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002010
2011 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002012}
2013
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002014llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002015 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2016
2017 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002018 // We use the initializer as a marker of whether this is a forward
2019 // reference or not. At module finalization we add the empty
2020 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002021 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002022 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002023 llvm::GlobalValue::ExternalLinkage,
2024 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002025 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002026 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002027 // FIXME: Is this necessary? Why only for protocol?
2028 Entry->setAlignment(4);
2029 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002030
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002031 return Entry;
2032}
2033
2034/*
2035 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002036 uint32_t size;
2037 struct objc_method_description_list *optional_instance_methods;
2038 struct objc_method_description_list *optional_class_methods;
2039 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002040 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002041 };
2042*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002043llvm::Constant *
2044CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002045 ArrayRef<llvm::Constant*> OptInstanceMethods,
2046 ArrayRef<llvm::Constant*> OptClassMethods,
2047 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002048 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002049 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002050 llvm::Constant *Values[] = {
2051 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002052 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002053 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002054 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002055 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002056 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002057 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002058 OptClassMethods),
2059 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002060 ObjCTypes),
2061 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2062 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002063 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002064
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002065 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002066 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002067 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002068 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002069
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002070 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002071 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002072
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002073 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002074 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002075 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002076 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002077}
2078
2079/*
2080 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002081 struct objc_protocol_list *next;
2082 long count;
2083 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002084 };
2085*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002086llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002087CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002088 ObjCProtocolDecl::protocol_iterator begin,
2089 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002090 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002091
Daniel Dunbardbc93372008-08-21 21:57:41 +00002092 for (; begin != end; ++begin)
2093 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002094
2095 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002096 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002097 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002098
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002099 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002100 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002101
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002102 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002104 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002105 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002106 ProtocolRefs.size() - 1);
2107 Values[2] =
2108 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2109 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002110 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002112 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002113 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002114 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002115 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002116 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002117}
2118
Bill Wendlingbb028552012-02-07 09:25:09 +00002119void CGObjCCommonMac::
2120PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002121 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002122 const Decl *Container,
2123 const ObjCProtocolDecl *PROTO,
2124 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002125 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2126 E = PROTO->protocol_end(); P != E; ++P)
2127 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2128 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2129 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002130 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002131 if (!PropertySet.insert(PD->getIdentifier()))
2132 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002133 llvm::Constant *Prop[] = {
2134 GetPropertyName(PD->getIdentifier()),
2135 GetPropertyTypeString(PD, Container)
2136 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002137 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2138 }
2139}
2140
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002141/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002142 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002143 const char * const name;
2144 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002145 };
2146
2147 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002148 uint32_t entsize; // sizeof (struct _objc_property)
2149 uint32_t prop_count;
2150 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002151 };
2152*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002153llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002154 const Decl *Container,
2155 const ObjCContainerDecl *OCD,
2156 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002157 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002158 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002159 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2160 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002161 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002162 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002163 llvm::Constant *Prop[] = {
2164 GetPropertyName(PD->getIdentifier()),
2165 GetPropertyTypeString(PD, Container)
2166 };
Owen Anderson08e25242009-07-27 22:29:56 +00002167 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002168 Prop));
2169 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002170 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002171 for (ObjCInterfaceDecl::all_protocol_iterator
2172 P = OID->all_referenced_protocol_begin(),
2173 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002174 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2175 ObjCTypes);
2176 }
2177 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2178 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2179 E = CD->protocol_end(); P != E; ++P)
2180 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2181 ObjCTypes);
2182 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002183
2184 // Return null for empty list.
2185 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002186 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002187
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002188 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002189 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002190 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002191 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2192 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002193 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002194 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002195 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002196 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002197
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002198 llvm::GlobalVariable *GV =
2199 CreateMetadataVar(Name, Init,
2200 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002201 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002202 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002203 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002204 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002205}
2206
Bill Wendlingbb028552012-02-07 09:25:09 +00002207llvm::Constant *
2208CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2209 ArrayRef<llvm::Constant*> MethodTypes,
2210 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002211 // Return null for empty list.
2212 if (MethodTypes.empty())
2213 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2214
2215 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2216 MethodTypes.size());
2217 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2218
2219 llvm::GlobalVariable *GV =
2220 CreateMetadataVar(Name, Init,
2221 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2222 (ObjCABI == 2) ? 8 : 4,
2223 true);
2224 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2225}
2226
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002227/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002228 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002229 int count;
2230 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002231 };
2232*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002233llvm::Constant *
2234CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002235 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002236 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002237 ObjCTypes.SelectorPtrTy),
2238 GetMethodVarType(MD)
2239 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002240 if (!Desc[1])
2241 return 0;
2242
Owen Anderson08e25242009-07-27 22:29:56 +00002243 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002244 Desc);
2245}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002246
Bill Wendlingbb028552012-02-07 09:25:09 +00002247llvm::Constant *
2248CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2249 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002250 // Return null for empty list.
2251 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002252 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002253
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002254 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002255 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002256 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002257 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002258 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002259 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002260
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002261 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002262 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002263 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002264}
2265
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002266/*
2267 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002268 char *category_name;
2269 char *class_name;
2270 struct _objc_method_list *instance_methods;
2271 struct _objc_method_list *class_methods;
2272 struct _objc_protocol_list *protocols;
2273 uint32_t size; // <rdar://4585769>
2274 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002275 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002276*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002277void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002278 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002279
Mike Stumpf5408fe2009-05-16 07:57:57 +00002280 // FIXME: This is poor design, the OCD should have a pointer to the category
2281 // decl. Additionally, note that Category can be null for the @implementation
2282 // w/o an @interface case. Sema should just create one for us as it does for
2283 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002284 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002285 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002286 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002287
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002288 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002289 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2290 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002291
Bill Wendling3964e622012-02-09 22:16:49 +00002292 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002293 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002294 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002295 // Instance methods should always be defined.
2296 InstanceMethods.push_back(GetMethodConstant(*i));
2297 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002298 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002299 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002300 // Class methods should always be defined.
2301 ClassMethods.push_back(GetMethodConstant(*i));
2302 }
2303
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002304 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002305 Values[0] = GetClassName(OCD->getIdentifier());
2306 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002307 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002308 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002309 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002310 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002311 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002312 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002313 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002314 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002315 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002316 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002317 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002318 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002319 Category->protocol_begin(),
2320 Category->protocol_end());
2321 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002322 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002323 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002324 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002325
2326 // If there is no category @interface then there can be no properties.
2327 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002328 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002329 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002330 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002331 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002332 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002333
Owen Anderson08e25242009-07-27 22:29:56 +00002334 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002335 Values);
2336
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002338 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002339 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002340 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002341 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002342 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002343 // method definition entries must be clear for next implementation.
2344 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002345}
2346
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002347// FIXME: Get from somewhere?
2348enum ClassFlags {
2349 eClassFlags_Factory = 0x00001,
2350 eClassFlags_Meta = 0x00002,
2351 // <rdr://5142207>
2352 eClassFlags_HasCXXStructors = 0x02000,
2353 eClassFlags_Hidden = 0x20000,
2354 eClassFlags_ABI2_Hidden = 0x00010,
2355 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2356};
2357
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358/*
2359 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002360 Class isa;
2361 Class super_class;
2362 const char *name;
2363 long version;
2364 long info;
2365 long instance_size;
2366 struct _objc_ivar_list *ivars;
2367 struct _objc_method_list *methods;
2368 struct _objc_cache *cache;
2369 struct _objc_protocol_list *protocols;
2370 // Objective-C 1.0 extensions (<rdr://4585769>)
2371 const char *ivar_layout;
2372 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373 };
2374
2375 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002376*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002377void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002378 DefinedSymbols.insert(ID->getIdentifier());
2379
Chris Lattner8ec03f52008-11-24 03:54:41 +00002380 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002383 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002384 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002385 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002386 Interface->all_referenced_protocol_begin(),
2387 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002388 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002389 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002390 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002391 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002392 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393
2394 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002395 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002396 Flags |= eClassFlags_Hidden;
2397
Bill Wendling3964e622012-02-09 22:16:49 +00002398 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002399 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002400 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002401 // Instance methods should always be defined.
2402 InstanceMethods.push_back(GetMethodConstant(*i));
2403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002404 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002405 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002406 // Class methods should always be defined.
2407 ClassMethods.push_back(GetMethodConstant(*i));
2408 }
2409
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002410 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002411 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002412 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002413
2414 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2415 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2416
2417 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2418 if (llvm::Constant *C = GetMethodConstant(MD))
2419 InstanceMethods.push_back(C);
2420 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2421 if (llvm::Constant *C = GetMethodConstant(MD))
2422 InstanceMethods.push_back(C);
2423 }
2424 }
2425
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002426 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002427 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002429 // Record a reference to the super class.
2430 LazySymbols.insert(Super->getIdentifier());
2431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002432 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002433 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 ObjCTypes.ClassPtrTy);
2435 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002436 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002437 }
2438 Values[ 2] = GetClassName(ID->getIdentifier());
2439 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002440 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2441 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2442 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002443 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002444 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002445 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002446 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002447 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002449 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002451 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002453 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002455 std::string Name("\01L_OBJC_CLASS_");
2456 Name += ClassName;
2457 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2458 // Check for a forward reference.
2459 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2460 if (GV) {
2461 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2462 "Forward metaclass reference has incorrect type.");
2463 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2464 GV->setInitializer(Init);
2465 GV->setSection(Section);
2466 GV->setAlignment(4);
2467 CGM.AddUsedGlobal(GV);
2468 }
2469 else
2470 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002471 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002472 // method definition entries must be clear for next implementation.
2473 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002474}
2475
2476llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2477 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00002478 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 unsigned Flags = eClassFlags_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00002480 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481
John McCall1fb0caa2010-10-22 21:05:15 +00002482 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002483 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002484
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002485 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002486 // The isa for the metaclass is the root of the hierarchy.
2487 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2488 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2489 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002490 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002491 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002492 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002493 // The super class for the metaclass is emitted as the name of the
2494 // super class. The runtime fixes this up to point to the
2495 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002496 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002498 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002499 ObjCTypes.ClassPtrTy);
2500 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002501 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002502 }
2503 Values[ 2] = GetClassName(ID->getIdentifier());
2504 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002505 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2506 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2507 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002508 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002509 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002510 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002511 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002512 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002513 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002514 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002515 Values[ 9] = Protocols;
2516 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002517 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002518 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002519 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002520 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002521 Values);
2522
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002523 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00002524 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002525
2526 // Check for a forward reference.
2527 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2528 if (GV) {
2529 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2530 "Forward metaclass reference has incorrect type.");
2531 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2532 GV->setInitializer(Init);
2533 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002534 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002535 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002536 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002537 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002538 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002539 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002540 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002541
2542 return GV;
2543}
2544
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002545llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002546 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002547
Mike Stumpf5408fe2009-05-16 07:57:57 +00002548 // FIXME: Should we look these up somewhere other than the module. Its a bit
2549 // silly since we only generate these while processing an implementation, so
2550 // exactly one pointer would work if know when we entered/exitted an
2551 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002552
2553 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002554 // Previously, metaclass with internal linkage may have been defined.
2555 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002556 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2557 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002558 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2559 "Forward metaclass reference has incorrect type.");
2560 return GV;
2561 } else {
2562 // Generate as an external reference to keep a consistent
2563 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002564 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002565 llvm::GlobalValue::ExternalLinkage,
2566 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002567 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002568 }
2569}
2570
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002571llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2572 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2573
2574 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2575 true)) {
2576 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2577 "Forward class metadata reference has incorrect type.");
2578 return GV;
2579 } else {
2580 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2581 llvm::GlobalValue::ExternalLinkage,
2582 0,
2583 Name);
2584 }
2585}
2586
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002587/*
2588 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002589 uint32_t size;
2590 const char *weak_ivar_layout;
2591 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002592 };
2593*/
2594llvm::Constant *
2595CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002596 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002597 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002598
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002599 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002600 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002601 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002602 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002603 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002604
2605 // Return null if no extension bits are used.
2606 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002607 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002608
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002609 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002610 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002611 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002612 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002613 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002614}
2615
2616/*
2617 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00002618 char *ivar_name;
2619 char *ivar_type;
2620 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002621 };
2622
2623 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00002624 int ivar_count;
2625 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002626 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002627*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002628llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002629 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002630 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002631
2632 // When emitting the root class GCC emits ivar entries for the
2633 // actual class structure. It is not clear if we need to follow this
2634 // behavior; for now lets try and get away with not doing it. If so,
2635 // the cleanest solution would be to make up an ObjCInterfaceDecl
2636 // for the class.
2637 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002638 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002639
Jordy Rosedb8264e2011-07-22 02:08:32 +00002640 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002641
Jordy Rosedb8264e2011-07-22 02:08:32 +00002642 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002643 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002644 // Ignore unnamed bit-fields.
2645 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002646 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002647 llvm::Constant *Ivar[] = {
2648 GetMethodVarName(IVD->getIdentifier()),
2649 GetMethodVarType(IVD),
2650 llvm::ConstantInt::get(ObjCTypes.IntTy,
2651 ComputeIvarBaseOffset(CGM, OID, IVD))
2652 };
Owen Anderson08e25242009-07-27 22:29:56 +00002653 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002654 }
2655
2656 // Return null for empty list.
2657 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002658 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002659
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002660 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002661 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002662 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002663 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002664 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002665 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002666
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002667 llvm::GlobalVariable *GV;
2668 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002669 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002670 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002671 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002672 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002673 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002674 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002675 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002676 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002677}
2678
2679/*
2680 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002681 SEL method_name;
2682 char *method_types;
2683 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002684 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002685
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002686 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002687 struct objc_method_list *obsolete;
2688 int count;
2689 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002690 };
2691*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002692
2693/// GetMethodConstant - Return a struct objc_method constant for the
2694/// given method if it has been defined. The result is null if the
2695/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002696llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002697 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002698 if (!Fn)
2699 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002700
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002701 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002702 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002703 ObjCTypes.SelectorPtrTy),
2704 GetMethodVarType(MD),
2705 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2706 };
Owen Anderson08e25242009-07-27 22:29:56 +00002707 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002708}
2709
Chris Lattner5f9e2722011-07-23 10:55:15 +00002710llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002711 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00002712 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002713 // Return null for empty list.
2714 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002715 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002716
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002717 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002718 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002719 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002720 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002721 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002722 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002723 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002724
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002725 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002726 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002727}
2728
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002729llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002730 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002731 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002732 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002733
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002734 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002735 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00002736 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002737 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002738 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002739 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002740 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002741 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002742 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002743
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002744 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002745}
2746
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002747llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002748CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002749 llvm::Constant *Init,
2750 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002751 unsigned Align,
2752 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002753 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002754 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002755 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002756 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002757 if (Section)
2758 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002759 if (Align)
2760 GV->setAlignment(Align);
2761 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002762 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002763 return GV;
2764}
2765
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002766llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002767 // Abuse this interface function as a place to finalize.
2768 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002769 return NULL;
2770}
2771
Chris Lattner74391b42009-03-22 21:03:39 +00002772llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002773 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002774}
2775
Chris Lattner74391b42009-03-22 21:03:39 +00002776llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002777 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002778}
2779
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002780llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2781 bool copy) {
2782 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2783}
2784
David Chisnall8fac25d2010-12-26 22:13:16 +00002785llvm::Constant *CGObjCMac::GetGetStructFunction() {
2786 return ObjCTypes.getCopyStructFn();
2787}
2788llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002789 return ObjCTypes.getCopyStructFn();
2790}
2791
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002792llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2793 return ObjCTypes.getCppAtomicObjectFunction();
2794}
2795
Chris Lattner74391b42009-03-22 21:03:39 +00002796llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002797 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002798}
2799
John McCallf1549f62010-07-06 01:34:17 +00002800void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2801 return EmitTryOrSynchronizedStmt(CGF, S);
2802}
2803
2804void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2805 const ObjCAtSynchronizedStmt &S) {
2806 return EmitTryOrSynchronizedStmt(CGF, S);
2807}
2808
John McCallcc505292010-07-21 06:59:36 +00002809namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002810 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002811 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002812 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002813 llvm::Value *CallTryExitVar;
2814 llvm::Value *ExceptionData;
2815 ObjCTypesHelper &ObjCTypes;
2816 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002817 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002818 llvm::Value *CallTryExitVar,
2819 llvm::Value *ExceptionData,
2820 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002821 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002822 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2823
John McCallad346f42011-07-12 20:27:29 +00002824 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002825 // Check whether we need to call objc_exception_try_exit.
2826 // In optimized code, this branch will always be folded.
2827 llvm::BasicBlock *FinallyCallExit =
2828 CGF.createBasicBlock("finally.call_exit");
2829 llvm::BasicBlock *FinallyNoCallExit =
2830 CGF.createBasicBlock("finally.no_call_exit");
2831 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2832 FinallyCallExit, FinallyNoCallExit);
2833
2834 CGF.EmitBlock(FinallyCallExit);
2835 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2836 ->setDoesNotThrow();
2837
2838 CGF.EmitBlock(FinallyNoCallExit);
2839
2840 if (isa<ObjCAtTryStmt>(S)) {
2841 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002842 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2843 // Save the current cleanup destination in case there's
2844 // control flow inside the finally statement.
2845 llvm::Value *CurCleanupDest =
2846 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2847
John McCallcc505292010-07-21 06:59:36 +00002848 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2849
John McCalld96a8e72010-08-11 00:16:14 +00002850 if (CGF.HaveInsertPoint()) {
2851 CGF.Builder.CreateStore(CurCleanupDest,
2852 CGF.getNormalCleanupDestSlot());
2853 } else {
2854 // Currently, the end of the cleanup must always exist.
2855 CGF.EnsureInsertPoint();
2856 }
2857 }
John McCallcc505292010-07-21 06:59:36 +00002858 } else {
2859 // Emit objc_sync_exit(expr); as finally's sole statement for
2860 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002861 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002862 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2863 ->setDoesNotThrow();
2864 }
2865 }
2866 };
John McCall87bb5822010-07-31 23:20:56 +00002867
2868 class FragileHazards {
2869 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002870 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002871 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2872
2873 llvm::InlineAsm *ReadHazard;
2874 llvm::InlineAsm *WriteHazard;
2875
2876 llvm::FunctionType *GetAsmFnType();
2877
2878 void collectLocals();
2879 void emitReadHazard(CGBuilderTy &Builder);
2880
2881 public:
2882 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002883
John McCall87bb5822010-07-31 23:20:56 +00002884 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002885 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002886 };
2887}
2888
2889/// Create the fragile-ABI read and write hazards based on the current
2890/// state of the function, which is presumed to be immediately prior
2891/// to a @try block. These hazards are used to maintain correct
2892/// semantics in the face of optimization and the fragile ABI's
2893/// cavalier use of setjmp/longjmp.
2894FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2895 collectLocals();
2896
2897 if (Locals.empty()) return;
2898
2899 // Collect all the blocks in the function.
2900 for (llvm::Function::iterator
2901 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2902 BlocksBeforeTry.insert(&*I);
2903
2904 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2905
2906 // Create a read hazard for the allocas. This inhibits dead-store
2907 // optimizations and forces the values to memory. This hazard is
2908 // inserted before any 'throwing' calls in the protected scope to
2909 // reflect the possibility that the variables might be read from the
2910 // catch block if the call throws.
2911 {
2912 std::string Constraint;
2913 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2914 if (I) Constraint += ',';
2915 Constraint += "*m";
2916 }
2917
2918 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2919 }
2920
2921 // Create a write hazard for the allocas. This inhibits folding
2922 // loads across the hazard. This hazard is inserted at the
2923 // beginning of the catch path to reflect the possibility that the
2924 // variables might have been written within the protected scope.
2925 {
2926 std::string Constraint;
2927 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2928 if (I) Constraint += ',';
2929 Constraint += "=*m";
2930 }
2931
2932 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2933 }
2934}
2935
2936/// Emit a write hazard at the current location.
2937void FragileHazards::emitWriteHazard() {
2938 if (Locals.empty()) return;
2939
Jay Foad4c7d9f12011-07-15 08:37:34 +00002940 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002941}
2942
John McCall87bb5822010-07-31 23:20:56 +00002943void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2944 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002945 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002946}
2947
2948/// Emit read hazards in all the protected blocks, i.e. all the blocks
2949/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002950void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002951 if (Locals.empty()) return;
2952
2953 CGBuilderTy Builder(CGF.getLLVMContext());
2954
2955 // Iterate through all blocks, skipping those prior to the try.
2956 for (llvm::Function::iterator
2957 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2958 llvm::BasicBlock &BB = *FI;
2959 if (BlocksBeforeTry.count(&BB)) continue;
2960
2961 // Walk through all the calls in the block.
2962 for (llvm::BasicBlock::iterator
2963 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2964 llvm::Instruction &I = *BI;
2965
2966 // Ignore instructions that aren't non-intrinsic calls.
2967 // These are the only calls that can possibly call longjmp.
2968 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2969 if (isa<llvm::IntrinsicInst>(I))
2970 continue;
2971
2972 // Ignore call sites marked nounwind. This may be questionable,
2973 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2974 llvm::CallSite CS(&I);
2975 if (CS.doesNotThrow()) continue;
2976
John McCall0b251722010-08-04 05:59:32 +00002977 // Insert a read hazard before the call. This will ensure that
2978 // any writes to the locals are performed before making the
2979 // call. If the call throws, then this is sufficient to
2980 // guarantee correctness as long as it doesn't also write to any
2981 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002982 Builder.SetInsertPoint(&BB, BI);
2983 emitReadHazard(Builder);
2984 }
2985 }
2986}
2987
2988static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2989 if (V) S.insert(V);
2990}
2991
2992void FragileHazards::collectLocals() {
2993 // Compute a set of allocas to ignore.
2994 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2995 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2996 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002997
2998 // Collect all the allocas currently in the function. This is
2999 // probably way too aggressive.
3000 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
3001 for (llvm::BasicBlock::iterator
3002 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3003 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3004 Locals.push_back(&*I);
3005}
3006
3007llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003008 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003009 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3010 tys[i] = Locals[i]->getType();
3011 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003012}
3013
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003014/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003015
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003016 Objective-C setjmp-longjmp (sjlj) Exception Handling
3017 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003018
John McCallf1549f62010-07-06 01:34:17 +00003019 A catch buffer is a setjmp buffer plus:
3020 - a pointer to the exception that was caught
3021 - a pointer to the previous exception data buffer
3022 - two pointers of reserved storage
3023 Therefore catch buffers form a stack, with a pointer to the top
3024 of the stack kept in thread-local storage.
3025
3026 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3027 objc_exception_try_exit pops the given catch buffer, which is
3028 required to be the top of the EH stack.
3029 objc_exception_throw pops the top of the EH stack, writes the
3030 thrown exception into the appropriate field, and longjmps
3031 to the setjmp buffer. It crashes the process (with a printf
3032 and an abort()) if there are no catch buffers on the stack.
3033 objc_exception_extract just reads the exception pointer out of the
3034 catch buffer.
3035
3036 There's no reason an implementation couldn't use a light-weight
3037 setjmp here --- something like __builtin_setjmp, but API-compatible
3038 with the heavyweight setjmp. This will be more important if we ever
3039 want to implement correct ObjC/C++ exception interactions for the
3040 fragile ABI.
3041
3042 Note that for this use of setjmp/longjmp to be correct, we may need
3043 to mark some local variables volatile: if a non-volatile local
3044 variable is modified between the setjmp and the longjmp, it has
3045 indeterminate value. For the purposes of LLVM IR, it may be
3046 sufficient to make loads and stores within the @try (to variables
3047 declared outside the @try) volatile. This is necessary for
3048 optimized correctness, but is not currently being done; this is
3049 being tracked as rdar://problem/8160285
3050
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003051 The basic framework for a @try-catch-finally is as follows:
3052 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003053 objc_exception_data d;
3054 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003055 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003056
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003057 objc_exception_try_enter(&d);
3058 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003059 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003060 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003061 // exception path
3062 id _caught = objc_exception_extract(&d);
3063
3064 // enter new try scope for handlers
3065 if (!setjmp(d.jmp_buf)) {
3066 ... match exception and execute catch blocks ...
3067
3068 // fell off end, rethrow.
3069 _rethrow = _caught;
3070 ... jump-through-finally to finally_rethrow ...
3071 } else {
3072 // exception in catch block
3073 _rethrow = objc_exception_extract(&d);
3074 _call_try_exit = false;
3075 ... jump-through-finally to finally_rethrow ...
3076 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003077 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003078 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003079
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003080 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003081 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003082 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003083
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003084 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003085 ... dispatch to finally destination ...
3086
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003087 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003088 objc_exception_throw(_rethrow);
3089
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003090 finally_end:
3091 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003092
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093 This framework differs slightly from the one gcc uses, in that gcc
3094 uses _rethrow to determine if objc_exception_try_exit should be called
3095 and if the object should be rethrown. This breaks in the face of
3096 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003097
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003098 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003099
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003100 - If there are no catch blocks, then we avoid emitting the second
3101 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003102
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003103 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3104 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003105
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003106 - FIXME: If there is no @finally block we can do a few more
3107 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003108
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003109 Rethrows and Jumps-Through-Finally
3110 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003111
John McCallf1549f62010-07-06 01:34:17 +00003112 '@throw;' is supported by pushing the currently-caught exception
3113 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003114
John McCallf1549f62010-07-06 01:34:17 +00003115 Branches through the @finally block are handled with an ordinary
3116 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3117 exceptions are not compatible with C++ exceptions, and this is
3118 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003119
John McCallf1549f62010-07-06 01:34:17 +00003120 @synchronized(expr) { stmt; } is emitted as if it were:
3121 id synch_value = expr;
3122 objc_sync_enter(synch_value);
3123 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003124*/
3125
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003126void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3127 const Stmt &S) {
3128 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003129
3130 // A destination for the fall-through edges of the catch handlers to
3131 // jump to.
3132 CodeGenFunction::JumpDest FinallyEnd =
3133 CGF.getJumpDestInCurrentScope("finally.end");
3134
3135 // A destination for the rethrow edge of the catch handlers to jump
3136 // to.
3137 CodeGenFunction::JumpDest FinallyRethrow =
3138 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003139
Daniel Dunbar1c566672009-02-24 01:43:46 +00003140 // For @synchronized, call objc_sync_enter(sync.expr). The
3141 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003142 // @synchronized. We can't avoid a temp here because we need the
3143 // value to be preserved. If the backend ever does liveness
3144 // correctly after setjmp, this will be unnecessary.
3145 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003146 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003147 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003148 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3149 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003150 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3151 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003152
3153 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3154 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003155 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003156
John McCall0b251722010-08-04 05:59:32 +00003157 // Allocate memory for the setjmp buffer. This needs to be kept
3158 // live throughout the try and catch blocks.
3159 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3160 "exceptiondata.ptr");
3161
John McCall87bb5822010-07-31 23:20:56 +00003162 // Create the fragile hazards. Note that this will not capture any
3163 // of the allocas required for exception processing, but will
3164 // capture the current basic block (which extends all the way to the
3165 // setjmp call) as "before the @try".
3166 FragileHazards Hazards(CGF);
3167
John McCallf1549f62010-07-06 01:34:17 +00003168 // Create a flag indicating whether the cleanup needs to call
3169 // objc_exception_try_exit. This is true except when
3170 // - no catches match and we're branching through the cleanup
3171 // just to rethrow the exception, or
3172 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003173 // The setjmp-safety rule here is that we should always store to this
3174 // variable in a place that dominates the branch through the cleanup
3175 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003176 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003177 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003178
John McCall9e2213d2010-10-04 23:42:51 +00003179 // A slot containing the exception to rethrow. Only needed when we
3180 // have both a @catch and a @finally.
3181 llvm::Value *PropagatingExnVar = 0;
3182
John McCallf1549f62010-07-06 01:34:17 +00003183 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003184 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003185 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003186 CallTryExitVar,
3187 ExceptionData,
3188 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003189
3190 // Enter a try block:
3191 // - Call objc_exception_try_enter to push ExceptionData on top of
3192 // the EH stack.
3193 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3194 ->setDoesNotThrow();
3195
3196 // - Call setjmp on the exception data buffer.
3197 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3198 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3199 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003200 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003201 llvm::CallInst *SetJmpResult =
3202 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3203 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003204 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003205
3206 // If setjmp returned 0, enter the protected block; otherwise,
3207 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003208 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3209 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003210 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003211 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3212 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003213
John McCallf1549f62010-07-06 01:34:17 +00003214 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003215 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003216 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003217 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003218 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003219
3220 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003221
John McCallf1549f62010-07-06 01:34:17 +00003222 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003223 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003224
John McCall87bb5822010-07-31 23:20:56 +00003225 // Don't optimize loads of the in-scope locals across this point.
3226 Hazards.emitWriteHazard();
3227
John McCallf1549f62010-07-06 01:34:17 +00003228 // For a @synchronized (or a @try with no catches), just branch
3229 // through the cleanup to the rethrow block.
3230 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3231 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003232 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003233 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003234
3235 // Otherwise, we have to match against the caught exceptions.
3236 } else {
John McCall0b251722010-08-04 05:59:32 +00003237 // Retrieve the exception object. We may emit multiple blocks but
3238 // nothing can cross this so the value is already in SSA form.
3239 llvm::CallInst *Caught =
3240 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3241 ExceptionData, "caught");
3242 Caught->setDoesNotThrow();
3243
John McCallf1549f62010-07-06 01:34:17 +00003244 // Push the exception to rethrow onto the EH value stack for the
3245 // benefit of any @throws in the handlers.
3246 CGF.ObjCEHValueStack.push_back(Caught);
3247
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003248 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003249
John McCall0b251722010-08-04 05:59:32 +00003250 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003251
John McCall0b251722010-08-04 05:59:32 +00003252 llvm::BasicBlock *CatchBlock = 0;
3253 llvm::BasicBlock *CatchHandler = 0;
3254 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003255 // Save the currently-propagating exception before
3256 // objc_exception_try_enter clears the exception slot.
3257 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3258 "propagating_exception");
3259 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3260
John McCall0b251722010-08-04 05:59:32 +00003261 // Enter a new exception try block (in case a @catch block
3262 // throws an exception).
3263 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3264 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003265
John McCall0b251722010-08-04 05:59:32 +00003266 llvm::CallInst *SetJmpResult =
3267 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3268 "setjmp.result");
3269 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003270 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003271
John McCall0b251722010-08-04 05:59:32 +00003272 llvm::Value *Threw =
3273 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3274
3275 CatchBlock = CGF.createBasicBlock("catch");
3276 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3277 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3278
3279 CGF.EmitBlock(CatchBlock);
3280 }
3281
3282 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003283
Daniel Dunbar55e40722008-09-27 07:03:52 +00003284 // Handle catch list. As a special case we check if everything is
3285 // matched and avoid generating code for falling off the end if
3286 // so.
3287 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003288 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3289 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003290
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003291 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003292 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003293
Anders Carlsson80f25672008-09-09 17:59:25 +00003294 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003295 if (!CatchParam) {
3296 AllMatched = true;
3297 } else {
John McCall183700f2009-09-21 23:43:11 +00003298 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003299
John McCallf1549f62010-07-06 01:34:17 +00003300 // catch(id e) always matches under this ABI, since only
3301 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003302 // FIXME: For the time being we also match id<X>; this should
3303 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003304 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003305 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003306 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003307
John McCallf1549f62010-07-06 01:34:17 +00003308 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003309 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003310 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3311
Anders Carlssondde0a942008-09-11 09:15:33 +00003312 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003313 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003314 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003315
3316 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003317 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003318 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003319
Anders Carlssondde0a942008-09-11 09:15:33 +00003320 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003321
3322 // The scope of the catch variable ends right here.
3323 CatchVarCleanups.ForceCleanup();
3324
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003325 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003326 break;
3327 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003328
Steve Naroff14108da2009-07-10 23:34:53 +00003329 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003330 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003331
3332 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003333 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3334 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003335
3336 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003337 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003338
John McCallf1549f62010-07-06 01:34:17 +00003339 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003340 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3341 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003342 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003343
John McCallf1549f62010-07-06 01:34:17 +00003344 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3345 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003346
3347 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003348 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003349
Anders Carlsson80f25672008-09-09 17:59:25 +00003350 // Emit the @catch block.
3351 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003352
3353 // Collect any cleanups for the catch variable. The scope lasts until
3354 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003355 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003356
John McCallb6bbcc92010-10-15 04:57:14 +00003357 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003358 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003359
John McCallf1549f62010-07-06 01:34:17 +00003360 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003361 llvm::Value *Tmp =
3362 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003363 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003364 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003365
Anders Carlssondde0a942008-09-11 09:15:33 +00003366 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003367
3368 // We're done with the catch variable.
3369 CatchVarCleanups.ForceCleanup();
3370
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003371 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003372
Anders Carlsson80f25672008-09-09 17:59:25 +00003373 CGF.EmitBlock(NextCatchBlock);
3374 }
3375
John McCallf1549f62010-07-06 01:34:17 +00003376 CGF.ObjCEHValueStack.pop_back();
3377
John McCall0b251722010-08-04 05:59:32 +00003378 // If nothing wanted anything to do with the caught exception,
3379 // kill the extract call.
3380 if (Caught->use_empty())
3381 Caught->eraseFromParent();
3382
3383 if (!AllMatched)
3384 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3385
3386 if (HasFinally) {
3387 // Emit the exception handler for the @catch blocks.
3388 CGF.EmitBlock(CatchHandler);
3389
3390 // In theory we might now need a write hazard, but actually it's
3391 // unnecessary because there's no local-accessing code between
3392 // the try's write hazard and here.
3393 //Hazards.emitWriteHazard();
3394
John McCall9e2213d2010-10-04 23:42:51 +00003395 // Extract the new exception and save it to the
3396 // propagating-exception slot.
3397 assert(PropagatingExnVar);
3398 llvm::CallInst *NewCaught =
3399 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3400 ExceptionData, "caught");
3401 NewCaught->setDoesNotThrow();
3402 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3403
John McCall0b251722010-08-04 05:59:32 +00003404 // Don't pop the catch handler; the throw already did.
3405 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003406 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003407 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003409
John McCall87bb5822010-07-31 23:20:56 +00003410 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003411 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003412
John McCallf1549f62010-07-06 01:34:17 +00003413 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003414 CGF.Builder.restoreIP(TryFallthroughIP);
3415 if (CGF.HaveInsertPoint())
3416 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003417 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003418 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003419
John McCallf1549f62010-07-06 01:34:17 +00003420 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003421 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003422 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003423 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003424 // If we have a propagating-exception variable, check it.
3425 llvm::Value *PropagatingExn;
3426 if (PropagatingExnVar) {
3427 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003428
John McCall9e2213d2010-10-04 23:42:51 +00003429 // Otherwise, just look in the buffer for the exception to throw.
3430 } else {
3431 llvm::CallInst *Caught =
3432 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3433 ExceptionData);
3434 Caught->setDoesNotThrow();
3435 PropagatingExn = Caught;
3436 }
3437
3438 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003439 ->setDoesNotThrow();
3440 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003441 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003442
John McCall87bb5822010-07-31 23:20:56 +00003443 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003444}
3445
3446void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003447 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003448 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003449
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003450 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003451 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003452 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003453 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003454 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003456 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003457 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003458 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003459
John McCallf1549f62010-07-06 01:34:17 +00003460 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3461 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003462 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003463
3464 // Clear the insertion point to indicate we are in unreachable code.
3465 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003466}
3467
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003468/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003469/// object: objc_read_weak (id *src)
3470///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003471llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003472 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003473 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003474 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3475 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3476 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003477 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003478 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003479 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003480 return read_weak;
3481}
3482
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003483/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3484/// objc_assign_weak (id src, id *dst)
3485///
3486void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003487 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003488 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003489 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003490 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003491 assert(Size <= 8 && "does not support size > 8");
3492 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003493 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003494 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3495 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003496 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3497 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003498 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003499 src, dst, "weakassign");
3500 return;
3501}
3502
Fariborz Jahanian58626502008-11-19 00:59:10 +00003503/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3504/// objc_assign_global (id src, id *dst)
3505///
3506void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003507 llvm::Value *src, llvm::Value *dst,
3508 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003509 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003510 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003511 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003512 assert(Size <= 8 && "does not support size > 8");
3513 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003514 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003515 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3516 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003517 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3518 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003519 if (!threadlocal)
3520 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3521 src, dst, "globalassign");
3522 else
3523 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3524 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003525 return;
3526}
3527
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003528/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003529/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003530///
3531void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003532 llvm::Value *src, llvm::Value *dst,
3533 llvm::Value *ivarOffset) {
3534 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003535 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003536 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003537 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003538 assert(Size <= 8 && "does not support size > 8");
3539 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003540 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003541 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3542 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003543 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3544 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003545 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3546 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003547 return;
3548}
3549
Fariborz Jahanian58626502008-11-19 00:59:10 +00003550/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3551/// objc_assign_strongCast (id src, id *dst)
3552///
3553void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003554 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003555 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003556 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003557 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003558 assert(Size <= 8 && "does not support size > 8");
3559 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003560 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003561 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3562 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003563 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3564 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003565 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003566 src, dst, "weakassign");
3567 return;
3568}
3569
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003570void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571 llvm::Value *DestPtr,
3572 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003573 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003574 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3575 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003576 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003577 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003578 return;
3579}
3580
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003581/// EmitObjCValueForIvar - Code Gen for ivar reference.
3582///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003583LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3584 QualType ObjectTy,
3585 llvm::Value *BaseValue,
3586 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003587 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003588 const ObjCInterfaceDecl *ID =
3589 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003590 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3591 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003592}
3593
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003594llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003595 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003596 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003597 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003598 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3600 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003601}
3602
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003603/* *** Private Interface *** */
3604
3605/// EmitImageInfo - Emit the image info marker used to encode some module
3606/// level information.
3607///
3608/// See: <rdr://4810609&4810587&4810587>
3609/// struct IMAGE_INFO {
3610/// unsigned version;
3611/// unsigned flags;
3612/// };
3613enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003614 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003615 eImageInfo_GarbageCollected = (1 << 1),
3616 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003617 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3618
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003619 // A flag indicating that the module has no instances of a @synthesize of a
3620 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00003621 eImageInfo_CorrectedSynthesize = (1 << 4),
3622 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003623};
3624
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003625void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003626 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00003627 const char *Section = (ObjCABI == 1) ?
3628 "__OBJC, __image_info,regular" :
3629 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003630
Bill Wendling3973acc2012-02-16 01:13:30 +00003631 // Generate module-level named metadata to convey this information to the
3632 // linker and code-gen.
3633 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003634
Bill Wendling3973acc2012-02-16 01:13:30 +00003635 // Add the ObjC ABI version to the module flags.
3636 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3637 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3638 version);
3639 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3640 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003641
David Blaikie4e4d0842012-03-11 07:00:24 +00003642 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003643 // Non-GC overrides those files which specify GC.
3644 Mod.addModuleFlag(llvm::Module::Override,
3645 "Objective-C Garbage Collection", (uint32_t)0);
3646 } else {
3647 // Add the ObjC garbage collection value.
3648 Mod.addModuleFlag(llvm::Module::Error,
3649 "Objective-C Garbage Collection",
3650 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003651
David Blaikie4e4d0842012-03-11 07:00:24 +00003652 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003653 // Add the ObjC GC Only value.
3654 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3655 eImageInfo_GCOnly);
3656
3657 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3658 llvm::Value *Ops[2] = {
3659 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3660 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3661 eImageInfo_GarbageCollected)
3662 };
3663 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3664 llvm::MDNode::get(VMContext, Ops));
3665 }
3666 }
Bill Wendling09822512012-04-24 11:04:57 +00003667
3668 // Indicate whether we're compiling this to run on a simulator.
3669 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3670 if (Triple.getOS() == llvm::Triple::IOS &&
3671 (Triple.getArch() == llvm::Triple::x86 ||
3672 Triple.getArch() == llvm::Triple::x86_64))
3673 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3674 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003675}
3676
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003677// struct objc_module {
3678// unsigned long version;
3679// unsigned long size;
3680// const char *name;
3681// Symtab symtab;
3682// };
3683
3684// FIXME: Get from somewhere
3685static const int ModuleVersion = 7;
3686
3687void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00003688 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003689
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003690 llvm::Constant *Values[] = {
3691 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3692 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3693 // This used to be the filename, now it is unused. <rdr://4327263>
3694 GetClassName(&CGM.getContext().Idents.get("")),
3695 EmitModuleSymbols()
3696 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003697 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003698 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003699 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003700 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003701}
3702
3703llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003704 unsigned NumClasses = DefinedClasses.size();
3705 unsigned NumCategories = DefinedCategories.size();
3706
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003707 // Return null if no symbols were defined.
3708 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003709 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003710
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003711 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003712 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003713 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003714 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3715 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003716
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003717 // The runtime expects exactly the list of defined classes followed
3718 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00003719 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003720 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003721 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003722 ObjCTypes.Int8PtrTy);
3723 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003724 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003725 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003726 ObjCTypes.Int8PtrTy);
3727
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003728 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003729 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00003730 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003731 Symbols);
3732
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003733 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003734
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003735 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003736 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3737 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003738 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003739 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003740}
3741
John McCallf85e1932011-06-15 23:02:42 +00003742llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3743 IdentifierInfo *II) {
3744 LazySymbols.insert(II);
3745
3746 llvm::GlobalVariable *&Entry = ClassReferences[II];
3747
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003748 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003749 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003750 llvm::ConstantExpr::getBitCast(GetClassName(II),
3751 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003752 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003753 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3754 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3755 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003756 }
John McCallf85e1932011-06-15 23:02:42 +00003757
Benjamin Kramer578faa82011-09-27 21:06:10 +00003758 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003759}
3760
John McCallf85e1932011-06-15 23:02:42 +00003761llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3762 const ObjCInterfaceDecl *ID) {
3763 return EmitClassRefFromId(Builder, ID->getIdentifier());
3764}
3765
3766llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3767 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3768 return EmitClassRefFromId(Builder, II);
3769}
3770
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003771llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3772 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003773 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003774
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003775 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003776 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003777 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003778 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003779 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003780 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3781 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003782 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003783 }
3784
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003785 if (lvalue)
3786 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003787 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003788}
3789
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003790llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003791 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003792
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003793 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003794 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00003795 llvm::ConstantDataArray::getString(VMContext,
3796 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003797 ((ObjCABI == 2) ?
3798 "__TEXT,__objc_classname,cstring_literals" :
3799 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003800 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003801
Owen Andersona1cf15f2009-07-14 23:10:40 +00003802 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003803}
3804
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003805llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3806 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3807 I = MethodDefinitions.find(MD);
3808 if (I != MethodDefinitions.end())
3809 return I->second;
3810
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003811 return NULL;
3812}
3813
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003814/// GetIvarLayoutName - Returns a unique constant for the given
3815/// ivar layout bitmap.
3816llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003817 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003818 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003819}
3820
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003821void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003822 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003823 bool ForStrongLayout,
3824 bool &HasUnion) {
3825 const RecordDecl *RD = RT->getDecl();
3826 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00003827 SmallVector<const FieldDecl*, 16> Fields;
3828 for (RecordDecl::field_iterator i = RD->field_begin(),
3829 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00003830 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00003831 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003832 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00003833 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003834
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003835 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3836 ForStrongLayout, HasUnion);
3837}
3838
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003839void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003840 const llvm::StructLayout *Layout,
3841 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00003842 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003843 unsigned int BytePos, bool ForStrongLayout,
3844 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003845 bool IsUnion = (RD && RD->isUnion());
3846 uint64_t MaxUnionIvarSize = 0;
3847 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003848 const FieldDecl *MaxField = 0;
3849 const FieldDecl *MaxSkippedField = 0;
3850 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003851 uint64_t MaxFieldOffset = 0;
3852 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003853 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003854 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003855
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003856 if (RecFields.empty())
3857 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003858 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3859 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00003860 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003861 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003862 FirstFieldDelta =
3863 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3864 }
3865
Chris Lattnerf1690852009-03-31 08:48:01 +00003866 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003867 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003868 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003869 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003870 // Note that 'i' here is actually the field index inside RD of Field,
3871 // although this dependency is hidden.
3872 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003873 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003874 } else
John McCallf85e1932011-06-15 23:02:42 +00003875 FieldOffset =
3876 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003877
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003878 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003879 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003880 LastFieldBitfieldOrUnnamed = Field;
3881 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003882 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003883 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003884
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003885 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003886 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003887 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003888 if (FQT->isUnionType())
3889 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003890
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003891 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003892 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003893 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003894 continue;
3895 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003896
Chris Lattnerf1690852009-03-31 08:48:01 +00003897 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003898 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003899 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003900 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003901 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003902 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003903 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3904 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003905 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003906 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003907 FQT = CArray->getElementType();
3908 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003909
3910 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003911 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003912 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003913 int OldIndex = IvarsInfo.size() - 1;
3914 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003915
Ted Kremenek6217b802009-07-29 21:53:49 +00003916 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003917 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003918 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003919
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003920 // Replicate layout information for each array element. Note that
3921 // one element is already done.
3922 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003923 for (int FirstIndex = IvarsInfo.size() - 1,
3924 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003925 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003926 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3927 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3928 IvarsInfo[i].ivar_size));
3929 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3930 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3931 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003932 }
3933 continue;
3934 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003935 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003936 // At this point, we are done with Record/Union and array there of.
3937 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003938 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003939
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003940 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003941 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3942 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003943 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003944 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003945 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003946 MaxUnionIvarSize = UnionIvarSize;
3947 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003948 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003949 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003950 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003951 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003952 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003953 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003954 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003955 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3956 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003957 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003958 // FIXME: Why the asymmetry? We divide by word size in bits on other
3959 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003960 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003961 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003962 MaxSkippedUnionIvarSize = UnionIvarSize;
3963 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003964 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003965 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003966 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003967 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003968 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003969 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003970 }
3971 }
3972 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003973
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003974 if (LastFieldBitfieldOrUnnamed) {
3975 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3976 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003977 uint64_t BitFieldSize
3978 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003979 GC_IVAR skivar;
3980 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3981 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3982 + ((BitFieldSize % ByteSizeInBits) != 0);
3983 SkipIvars.push_back(skivar);
3984 } else {
3985 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3986 // Last field was unnamed. Must update skip info.
3987 unsigned FieldSize
3988 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3989 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3990 FieldSize / ByteSizeInBits));
3991 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003992 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003993
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003994 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003995 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003996 MaxUnionIvarSize));
3997 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003998 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003999 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004000}
4001
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004002/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4003/// the computations and returning the layout bitmap (for ivar or blocks) in
4004/// the given argument BitMap string container. Routine reads
4005/// two containers, IvarsInfo and SkipIvars which are assumed to be
4006/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004007llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004008 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004009 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004010
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004011 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004012 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004013 unsigned int WordSize =
Micah Villmow25a6a842012-10-08 16:25:52 +00004014 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004015 if (IvarsInfo[0].ivar_bytepos == 0) {
4016 WordsToSkip = 0;
4017 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004018 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004019 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4020 WordsToScan = IvarsInfo[0].ivar_size;
4021 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004022 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004023 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004024 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004025 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004026 // consecutive 'scanned' object pointers.
4027 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004028 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004029 // Skip over 'gc'able object pointer which lay over each other.
4030 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4031 continue;
4032 // Must skip over 1 or more words. We save current skip/scan values
4033 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004034 SKIP_SCAN SkScan;
4035 SkScan.skip = WordsToSkip;
4036 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004037 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004038
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004039 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004040 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4041 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004042 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004043 WordsToSkip = 0;
4044 WordsToScan = IvarsInfo[i].ivar_size;
4045 }
4046 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004047 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004048 SKIP_SCAN SkScan;
4049 SkScan.skip = WordsToSkip;
4050 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004051 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004052 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004053
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004054 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004055 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004056 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004057 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004058 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004059 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004060 IvarsInfo[LastIndex].ivar_bytepos +
4061 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004062 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004063 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004064 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004065 SKIP_SCAN SkScan;
4066 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4067 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004068 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004069 }
4070 }
4071 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4072 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004073 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004074 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004075 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4076 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4077 // 0xM0 followed by 0x0N detected.
4078 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4079 for (int j = i+1; j < SkipScan; j++)
4080 SkipScanIvars[j] = SkipScanIvars[j+1];
4081 --SkipScan;
4082 }
4083 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004084
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004085 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004086 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004087 unsigned char byte;
4088 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4089 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4090 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4091 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004092
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004093 // first skip big.
4094 for (unsigned int ix = 0; ix < skip_big; ix++)
4095 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004096
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004097 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004098 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004099 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004100 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004101 byte |= 0xf;
4102 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004103 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004104 byte |= scan_small;
4105 scan_small = 0;
4106 }
4107 BitMap += byte;
4108 }
4109 // next scan big
4110 for (unsigned int ix = 0; ix < scan_big; ix++)
4111 BitMap += (unsigned char)(0x0f);
4112 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004113 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004114 byte = scan_small;
4115 BitMap += byte;
4116 }
4117 }
4118 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004119 unsigned char zero = 0;
4120 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004121
4122 llvm::GlobalVariable * Entry =
4123 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004124 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004125 ((ObjCABI == 2) ?
4126 "__TEXT,__objc_classname,cstring_literals" :
4127 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004128 1, true);
4129 return getConstantGEP(VMContext, Entry, 0, 0);
4130}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004132/// BuildIvarLayout - Builds ivar layout bitmap for the class
4133/// implementation for the __strong or __weak case.
4134/// The layout map displays which words in ivar list must be skipped
4135/// and which must be scanned by GC (see below). String is built of bytes.
4136/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4137/// of words to skip and right nibble is count of words to scan. So, each
4138/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4139/// represented by a 0x00 byte which also ends the string.
4140/// 1. when ForStrongLayout is true, following ivars are scanned:
4141/// - id, Class
4142/// - object *
4143/// - __strong anything
4144///
4145/// 2. When ForStrongLayout is false, following ivars are scanned:
4146/// - __weak anything
4147///
4148llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4149 const ObjCImplementationDecl *OMD,
4150 bool ForStrongLayout) {
4151 bool hasUnion = false;
4152
Chris Lattner8b418682012-02-07 00:39:47 +00004153 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004154 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4155 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004156 return llvm::Constant::getNullValue(PtrTy);
4157
Jordy Rosedb8264e2011-07-22 02:08:32 +00004158 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4159 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004160 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004161 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004162 IVD; IVD = IVD->getNextIvar())
4163 RecFields.push_back(cast<FieldDecl>(IVD));
4164 }
4165 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004166 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004167 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004168
Jordy Rosedb8264e2011-07-22 02:08:32 +00004169 // FIXME: This is not ideal; we shouldn't have to do this copy.
4170 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004171 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004172
4173 if (RecFields.empty())
4174 return llvm::Constant::getNullValue(PtrTy);
4175
4176 SkipIvars.clear();
4177 IvarsInfo.clear();
4178
4179 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4180 if (IvarsInfo.empty())
4181 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004182 // Sort on byte position in case we encounterred a union nested in
4183 // the ivar list.
4184 if (hasUnion && !IvarsInfo.empty())
4185 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4186 if (hasUnion && !SkipIvars.empty())
4187 std::sort(SkipIvars.begin(), SkipIvars.end());
4188
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004189 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004190 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004191
David Blaikie4e4d0842012-03-11 07:00:24 +00004192 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004193 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004194 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004195 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004196 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004197 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004198 if (!(s[i] & 0xf0))
4199 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4200 else
4201 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4202 printf("\n");
4203 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004204 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004205}
4206
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004207llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004208 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4209
Chris Lattner0b239712012-02-06 22:16:34 +00004210 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004211 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004212 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004213 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004214 ((ObjCABI == 2) ?
4215 "__TEXT,__objc_methname,cstring_literals" :
4216 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004217 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004218
Owen Andersona1cf15f2009-07-14 23:10:40 +00004219 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004220}
4221
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004222// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004223llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004224 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4225}
4226
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004227llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004228 std::string TypeStr;
4229 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4230
4231 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004232
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004233 if (!Entry)
4234 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004235 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004236 ((ObjCABI == 2) ?
4237 "__TEXT,__objc_methtype,cstring_literals" :
4238 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004239 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004240
Owen Andersona1cf15f2009-07-14 23:10:40 +00004241 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004242}
4243
Bob Wilsondc8dab62011-11-30 01:57:58 +00004244llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4245 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004246 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004247 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004248 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004249
4250 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4251
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004252 if (!Entry)
4253 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004254 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004255 ((ObjCABI == 2) ?
4256 "__TEXT,__objc_methtype,cstring_literals" :
4257 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004258 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004259
Owen Andersona1cf15f2009-07-14 23:10:40 +00004260 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004261}
4262
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004263// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004264llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004265 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004266
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004267 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004268 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004269 llvm::ConstantDataArray::getString(VMContext,
4270 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004271 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004272 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004273
Owen Andersona1cf15f2009-07-14 23:10:40 +00004274 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004275}
4276
4277// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004278// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004279llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004280CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4281 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004282 std::string TypeStr;
4283 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004284 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4285}
4286
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004287void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004288 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004289 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004290 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004291 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004292 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4293 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004294 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004295 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004296 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004297 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004298}
4299
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004300void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004301 EmitModuleInfo();
4302
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004303 // Emit the dummy bodies for any protocols which were referenced but
4304 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004305 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004306 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4307 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004308 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004309
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004310 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004311 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004312 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004313 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004314 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004315 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004316 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004317 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004318 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004319 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004320 }
4321
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004322 // Add assembler directives to add lazy undefined symbol references
4323 // for classes which are referenced but not defined. This is
4324 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004325 //
4326 // FIXME: It would be nice if we had an LLVM construct for this.
4327 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004328 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004329 Asm += CGM.getModule().getModuleInlineAsm();
4330 if (!Asm.empty() && Asm.back() != '\n')
4331 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004332
Daniel Dunbar33063492009-09-07 00:20:42 +00004333 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004334 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4335 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004336 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4337 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004338 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004339 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004340 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004341 }
4342
Bill Wendling13562a12012-02-07 09:06:01 +00004343 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004344 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4345 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4346 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004347
Daniel Dunbar33063492009-09-07 00:20:42 +00004348 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004349 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004350}
4351
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004352CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004353 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004354 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004355 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004356 ObjCABI = 2;
4357}
4358
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004359/* *** */
4360
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004361ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004362 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4363{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004364 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4365 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004366
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004367 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004368 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004369 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004370 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004371 Int8PtrTy = CGM.Int8PtrTy;
4372 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004373
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004374 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004375 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004376 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004377
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004378 // I'm not sure I like this. The implicit coordination is a bit
4379 // gross. We should solve this in a reasonable fashion because this
4380 // is a pretty common task (match some runtime data structure with
4381 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004382
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004383 // FIXME: This is leaked.
4384 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004385
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004386 // struct _objc_super {
4387 // id self;
4388 // Class cls;
4389 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004390 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004391 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004392 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004393 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004394 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004395 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004396 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004397 Ctx.getObjCClassType(), 0, 0, false,
4398 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004399 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004400
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004401 SuperCTy = Ctx.getTagDeclType(RD);
4402 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004404 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004405 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4406
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004407 // struct _prop_t {
4408 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004409 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004410 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004411 PropertyTy = llvm::StructType::create("struct._prop_t",
4412 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004413
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004414 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004415 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004416 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004417 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004418 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004419 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004420 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4421 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004422 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004423 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004424
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004425 // struct _objc_method {
4426 // SEL _cmd;
4427 // char *method_type;
4428 // char *_imp;
4429 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004430 MethodTy = llvm::StructType::create("struct._objc_method",
4431 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4432 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004433
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004434 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004435 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004436 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004437
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004438}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004439
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004440ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004441 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004442 // struct _objc_method_description {
4443 // SEL name;
4444 // char *types;
4445 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004446 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004447 llvm::StructType::create("struct._objc_method_description",
4448 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004449
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004450 // struct _objc_method_description_list {
4451 // int count;
4452 // struct _objc_method_description[1];
4453 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004454 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004455 llvm::StructType::create("struct._objc_method_description_list",
4456 IntTy,
4457 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004459 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004460 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004461 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004462
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004463 // Protocol description structures
4464
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004465 // struct _objc_protocol_extension {
4466 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4467 // struct _objc_method_description_list *optional_instance_methods;
4468 // struct _objc_method_description_list *optional_class_methods;
4469 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004470 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004471 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004472 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004473 llvm::StructType::create("struct._objc_protocol_extension",
4474 IntTy, MethodDescriptionListPtrTy,
4475 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004476 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004477
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004478 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004479 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004480
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004481 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004482
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004483 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004484 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004485
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004486 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004487 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004488 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004489 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004490 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004491 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004492
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004493 // struct _objc_protocol {
4494 // struct _objc_protocol_extension *isa;
4495 // char *protocol_name;
4496 // struct _objc_protocol **_objc_protocol_list;
4497 // struct _objc_method_description_list *instance_methods;
4498 // struct _objc_method_description_list *class_methods;
4499 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004500 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4501 llvm::PointerType::getUnqual(ProtocolListTy),
4502 MethodDescriptionListPtrTy,
4503 MethodDescriptionListPtrTy,
4504 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004505
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004506 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004507 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004508
Owen Anderson96e0fc72009-07-29 22:16:19 +00004509 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004510
4511 // Class description structures
4512
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004513 // struct _objc_ivar {
4514 // char *ivar_name;
4515 // char *ivar_type;
4516 // int ivar_offset;
4517 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004518 IvarTy = llvm::StructType::create("struct._objc_ivar",
4519 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004520
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004521 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004522 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004523 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004524 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004525
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004526 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004527 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004528 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004529 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004530
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004531 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004532 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004533 llvm::StructType::create("struct._objc_class_extension",
4534 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004535 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004536
Chris Lattnerc1c20112011-08-12 17:43:31 +00004537 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004538
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004539 // struct _objc_class {
4540 // Class isa;
4541 // Class super_class;
4542 // char *name;
4543 // long version;
4544 // long info;
4545 // long instance_size;
4546 // struct _objc_ivar_list *ivars;
4547 // struct _objc_method_list *methods;
4548 // struct _objc_cache *cache;
4549 // struct _objc_protocol_list *protocols;
4550 // char *ivar_layout;
4551 // struct _objc_class_ext *ext;
4552 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004553 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4554 llvm::PointerType::getUnqual(ClassTy),
4555 Int8PtrTy,
4556 LongTy,
4557 LongTy,
4558 LongTy,
4559 IvarListPtrTy,
4560 MethodListPtrTy,
4561 CachePtrTy,
4562 ProtocolListPtrTy,
4563 Int8PtrTy,
4564 ClassExtensionPtrTy,
4565 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Owen Anderson96e0fc72009-07-29 22:16:19 +00004567 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004568
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004569 // struct _objc_category {
4570 // char *category_name;
4571 // char *class_name;
4572 // struct _objc_method_list *instance_method;
4573 // struct _objc_method_list *class_method;
4574 // uint32_t size; // sizeof(struct _objc_category)
4575 // struct _objc_property_list *instance_properties;// category's @property
4576 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004577 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004578 llvm::StructType::create("struct._objc_category",
4579 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4580 MethodListPtrTy, ProtocolListPtrTy,
4581 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004582
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004583 // Global metadata structures
4584
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004585 // struct _objc_symtab {
4586 // long sel_ref_cnt;
4587 // SEL *refs;
4588 // short cls_def_cnt;
4589 // short cat_def_cnt;
4590 // char *defs[cls_def_cnt + cat_def_cnt];
4591 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004592 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004593 llvm::StructType::create("struct._objc_symtab",
4594 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4595 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004596 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004597
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004598 // struct _objc_module {
4599 // long version;
4600 // long size; // sizeof(struct _objc_module)
4601 // char *name;
4602 // struct _objc_symtab* symtab;
4603 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004605 llvm::StructType::create("struct._objc_module",
4606 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004607
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004608
Mike Stumpf5408fe2009-05-16 07:57:57 +00004609 // FIXME: This is the size of the setjmp buffer and should be target
4610 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004611 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004612
Anders Carlsson124526b2008-09-09 10:10:21 +00004613 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00004614 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004615
4616 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004617 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00004618 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4619 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004620
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004621}
4622
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004623ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004624 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004625 // struct _method_list_t {
4626 // uint32_t entsize; // sizeof(struct _objc_method)
4627 // uint32_t method_count;
4628 // struct _objc_method method_list[method_count];
4629 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004630 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004631 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4632 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004633 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004634 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004635
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004636 // struct _protocol_t {
4637 // id isa; // NULL
4638 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004639 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004640 // const struct method_list_t * const instance_methods;
4641 // const struct method_list_t * const class_methods;
4642 // const struct method_list_t *optionalInstanceMethods;
4643 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004644 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004645 // const uint32_t size; // sizeof(struct _protocol_t)
4646 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004647 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004648 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004649
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004650 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004651 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004652 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004653
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004654 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004655 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4656 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4657 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4658 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004659 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4660 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004661
4662 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004663 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004664
Fariborz Jahanianda320092009-01-29 19:24:30 +00004665 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004666 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004667 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004668 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004669 ProtocolListnfABITy->setBody(LongTy,
4670 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4671 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004672
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004673 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004674 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004675
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004676 // struct _ivar_t {
4677 // unsigned long int *offset; // pointer to ivar offset location
4678 // char *name;
4679 // char *type;
4680 // uint32_t alignment;
4681 // uint32_t size;
4682 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004683 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004684 llvm::StructType::create("struct._ivar_t",
4685 llvm::PointerType::getUnqual(LongTy),
4686 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004687
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004688 // struct _ivar_list_t {
4689 // uint32 entsize; // sizeof(struct _ivar_t)
4690 // uint32 count;
4691 // struct _iver_t list[count];
4692 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004693 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004694 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4695 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004696
Owen Anderson96e0fc72009-07-29 22:16:19 +00004697 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004698
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004699 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004700 // uint32_t const flags;
4701 // uint32_t const instanceStart;
4702 // uint32_t const instanceSize;
4703 // uint32_t const reserved; // only when building for 64bit targets
4704 // const uint8_t * const ivarLayout;
4705 // const char *const name;
4706 // const struct _method_list_t * const baseMethods;
4707 // const struct _objc_protocol_list *const baseProtocols;
4708 // const struct _ivar_list_t *const ivars;
4709 // const uint8_t * const weakIvarLayout;
4710 // const struct _prop_list_t * const properties;
4711 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004712
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004713 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004714 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4715 IntTy, IntTy, IntTy, Int8PtrTy,
4716 Int8PtrTy, MethodListnfABIPtrTy,
4717 ProtocolListnfABIPtrTy,
4718 IvarListnfABIPtrTy,
4719 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004720
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004721 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004722 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004723 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4724 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004725
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004726 // struct _class_t {
4727 // struct _class_t *isa;
4728 // struct _class_t * const superclass;
4729 // void *cache;
4730 // IMP *vtable;
4731 // struct class_ro_t *ro;
4732 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004733
Chris Lattnerc1c20112011-08-12 17:43:31 +00004734 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004735 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4736 llvm::PointerType::getUnqual(ClassnfABITy),
4737 CachePtrTy,
4738 llvm::PointerType::getUnqual(ImpnfABITy),
4739 llvm::PointerType::getUnqual(ClassRonfABITy),
4740 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004741
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004742 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004743 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004744
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004745 // struct _category_t {
4746 // const char * const name;
4747 // struct _class_t *const cls;
4748 // const struct _method_list_t * const instance_methods;
4749 // const struct _method_list_t * const class_methods;
4750 // const struct _protocol_list_t * const protocols;
4751 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004752 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004753 CategorynfABITy = llvm::StructType::create("struct._category_t",
4754 Int8PtrTy, ClassnfABIPtrTy,
4755 MethodListnfABIPtrTy,
4756 MethodListnfABIPtrTy,
4757 ProtocolListnfABIPtrTy,
4758 PropertyListPtrTy,
4759 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004760
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004761 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004762 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4763 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004764
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004765 // MessageRefTy - LLVM for:
4766 // struct _message_ref_t {
4767 // IMP messenger;
4768 // SEL name;
4769 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004771 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004772 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004773 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004774 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004775 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004776 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004777 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004778 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004779 Ctx.getObjCSelType(), 0, 0, false,
4780 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004781 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004782
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004783 MessageRefCTy = Ctx.getTagDeclType(RD);
4784 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4785 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004786
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004787 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004788 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004789
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004790 // SuperMessageRefTy - LLVM for:
4791 // struct _super_message_ref_t {
4792 // SUPER_IMP messenger;
4793 // SEL name;
4794 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004795 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004796 llvm::StructType::create("struct._super_message_ref_t",
4797 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004798
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004799 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004800 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004801
Daniel Dunbare588b992009-03-01 04:46:24 +00004802
4803 // struct objc_typeinfo {
4804 // const void** vtable; // objc_ehtype_vtable + 2
4805 // const char* name; // c++ typeinfo string
4806 // Class cls;
4807 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004808 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004809 llvm::StructType::create("struct._objc_typeinfo",
4810 llvm::PointerType::getUnqual(Int8PtrTy),
4811 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004812 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004813}
4814
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004815llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004816 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004817
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004818 return NULL;
4819}
4820
Bill Wendlingbb028552012-02-07 09:25:09 +00004821void CGObjCNonFragileABIMac::
4822AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4823 const char *SymbolName,
4824 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00004825 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004826
Daniel Dunbar463b8762009-05-15 21:48:48 +00004827 if (!NumClasses)
4828 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004829
Chris Lattner0b239712012-02-06 22:16:34 +00004830 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004831 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004832 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004833 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00004834 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004835 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004836 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00004837 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004838
Daniel Dunbar463b8762009-05-15 21:48:48 +00004839 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004840 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004841 llvm::GlobalValue::InternalLinkage,
4842 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004843 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00004844 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004845 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004846 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004847}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004848
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004849void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4850 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004851
Daniel Dunbar463b8762009-05-15 21:48:48 +00004852 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004853 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004854 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004855 "\01L_OBJC_LABEL_CLASS_$",
4856 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004857
Bill Wendling13562a12012-02-07 09:06:01 +00004858 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004859 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4860 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4861 continue;
4862 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004863 }
4864
Bill Wendling13562a12012-02-07 09:06:01 +00004865 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004866 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4867 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4868 continue;
4869 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4870 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004871
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004873 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4874 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004875
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004876 // Build list of all implemented category addresses in array
4877 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004879 "\01L_OBJC_LABEL_CATEGORY_$",
4880 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004881 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004882 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4883 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004884
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004885 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004886}
4887
John McCall944c8432011-05-14 03:10:52 +00004888/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4889/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004890/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004891/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004892bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4893 // At various points we've experimented with using vtable-based
4894 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004895 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004896 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004897 return false;
John McCall944c8432011-05-14 03:10:52 +00004898 case CodeGenOptions::NonLegacy:
4899 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004900 case CodeGenOptions::Mixed:
4901 break;
4902 }
4903
4904 // If so, see whether this selector is in the white-list of things which must
4905 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004906 if (VTableDispatchMethods.empty()) {
4907 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4908 VTableDispatchMethods.insert(GetNullarySelector("class"));
4909 VTableDispatchMethods.insert(GetNullarySelector("self"));
4910 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4911 VTableDispatchMethods.insert(GetNullarySelector("length"));
4912 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004913
John McCall944c8432011-05-14 03:10:52 +00004914 // These are vtable-based if GC is disabled.
4915 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004916 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004917 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4918 VTableDispatchMethods.insert(GetNullarySelector("release"));
4919 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4920 }
4921
4922 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4923 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4924 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4925 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4926 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4927 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4928 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4929
4930 // These are vtable-based if GC is enabled.
4931 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004932 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004933 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4934 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4935
4936 // "countByEnumeratingWithState:objects:count"
4937 IdentifierInfo *KeyIdents[] = {
4938 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4939 &CGM.getContext().Idents.get("objects"),
4940 &CGM.getContext().Idents.get("count")
4941 };
4942 VTableDispatchMethods.insert(
4943 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4944 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004945 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004946
John McCall944c8432011-05-14 03:10:52 +00004947 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004948}
4949
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004950// Metadata flags
4951enum MetaDataDlags {
4952 CLS = 0x0,
4953 CLS_META = 0x1,
4954 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004955 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004956 CLS_EXCEPTION = 0x20,
4957
4958 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4959 CLS_HAS_IVAR_RELEASER = 0x40,
4960 /// class was compiled with -fobjc-arr
4961 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004962};
4963/// BuildClassRoTInitializer - generate meta-data for:
4964/// struct _class_ro_t {
4965/// uint32_t const flags;
4966/// uint32_t const instanceStart;
4967/// uint32_t const instanceSize;
4968/// uint32_t const reserved; // only when building for 64bit targets
4969/// const uint8_t * const ivarLayout;
4970/// const char *const name;
4971/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004972/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004973/// const struct _ivar_list_t *const ivars;
4974/// const uint8_t * const weakIvarLayout;
4975/// const struct _prop_list_t * const properties;
4976/// }
4977///
4978llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004979 unsigned flags,
4980 unsigned InstanceStart,
4981 unsigned InstanceSize,
4982 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004983 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004984 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004985
David Blaikie4e4d0842012-03-11 07:00:24 +00004986 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00004987 flags |= CLS_COMPILED_BY_ARC;
4988
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004989 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4990 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4991 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004992 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4994 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004995 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004996 // const struct _method_list_t * const baseMethods;
4997 std::vector<llvm::Constant*> Methods;
4998 std::string MethodListName("\01l_OBJC_$_");
4999 if (flags & CLS_META) {
5000 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005001 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005002 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005003 // Class methods should always be defined.
5004 Methods.push_back(GetMethodConstant(*i));
5005 }
5006 } else {
5007 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005009 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005010 // Instance methods should always be defined.
5011 Methods.push_back(GetMethodConstant(*i));
5012 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005013 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005014 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005015 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005016
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005017 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5018 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005019
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005020 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5021 if (llvm::Constant *C = GetMethodConstant(MD))
5022 Methods.push_back(C);
5023 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5024 if (llvm::Constant *C = GetMethodConstant(MD))
5025 Methods.push_back(C);
5026 }
5027 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005028 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005029 Values[ 5] = EmitMethodList(MethodListName,
5030 "__DATA, __objc_const", Methods);
5031
Fariborz Jahanianda320092009-01-29 19:24:30 +00005032 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5033 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005034 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005035 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005036 OID->all_referenced_protocol_begin(),
5037 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005038
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005039 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005040 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005041 else
5042 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005043 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
5044 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005045 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005046 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005047 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005048 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5049 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005050 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005051 Values);
5052 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5054 llvm::GlobalValue::InternalLinkage,
5055 Init,
5056 (flags & CLS_META) ?
5057 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5058 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005059 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005060 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005061 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005062 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005063
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005064}
5065
5066/// BuildClassMetaData - This routine defines that to-level meta-data
5067/// for the given ClassName for:
5068/// struct _class_t {
5069/// struct _class_t *isa;
5070/// struct _class_t * const superclass;
5071/// void *cache;
5072/// IMP *vtable;
5073/// struct class_ro_t *ro;
5074/// }
5075///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005076llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005077 std::string &ClassName,
5078 llvm::Constant *IsAGV,
5079 llvm::Constant *SuperClassGV,
5080 llvm::Constant *ClassRoGV,
5081 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005082 llvm::Constant *Values[] = {
5083 IsAGV,
5084 SuperClassGV,
5085 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5086 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5087 ClassRoGV // &CLASS_RO_GV
5088 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005089 if (!Values[1])
5090 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005091 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005092 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005093 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5094 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005095 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005096 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005097 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005098 if (HiddenVisibility)
5099 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005100 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005101}
5102
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005103bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005104CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005105 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005106}
5107
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005108void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005109 uint32_t &InstanceStart,
5110 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005111 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005112 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005113
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005114 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005115 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005116
5117 // If there are no fields, the start is the same as the end.
5118 if (!RL.getFieldCount())
5119 InstanceStart = InstanceSize;
5120 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005121 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005122}
5123
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005124void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5125 std::string ClassName = ID->getNameAsString();
5126 if (!ObjCEmptyCacheVar) {
5127 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005128 CGM.getModule(),
5129 ObjCTypes.CacheTy,
5130 false,
5131 llvm::GlobalValue::ExternalLinkage,
5132 0,
5133 "_objc_empty_cache");
5134
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005135 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005136 CGM.getModule(),
5137 ObjCTypes.ImpnfABITy,
5138 false,
5139 llvm::GlobalValue::ExternalLinkage,
5140 0,
5141 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005142 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005143 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005144 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005145 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005147 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005148 uint32_t InstanceSize = InstanceStart;
5149 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005150 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5151 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005152
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005153 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005154
5155 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005156 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005157 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005158 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005159 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005160 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005161 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005162 // class is root
5163 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005164 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005165 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005166 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005167 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005168 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5169 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5170 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005171 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005172 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005173 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005174 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005175 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005176 ObjCMetaClassName +
5177 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005178 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005179 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005180 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005181 }
5182 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5183 InstanceStart,
5184 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005185 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005187 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5188 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005189 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005190
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005191 // Metadata for the class
5192 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005193 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005194 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005195 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005196 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005197
Douglas Gregor68584ed2009-06-18 16:11:24 +00005198 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005199 flags |= CLS_EXCEPTION;
5200
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005201 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005202 flags |= CLS_ROOT;
5203 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005204 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005205 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005206 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005207 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005208 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005209 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005210 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005211 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005212 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005213 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005214 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005215 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005216 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005217
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005218 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005219 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005220 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5221 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005222 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005223
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005224 // Determine if this class is also "non-lazy".
5225 if (ImplementationIsNonLazy(ID))
5226 DefinedNonLazyClasses.push_back(ClassMD);
5227
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005228 // Force the definition of the EHType if necessary.
5229 if (flags & CLS_EXCEPTION)
5230 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005231 // Make sure method definition entries are all clear for next implementation.
5232 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005233}
5234
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005235/// GenerateProtocolRef - This routine is called to generate code for
5236/// a protocol reference expression; as in:
5237/// @code
5238/// @protocol(Proto1);
5239/// @endcode
5240/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5241/// which will hold address of the protocol meta-data.
5242///
5243llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005244 const ObjCProtocolDecl *PD) {
5245
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005246 // This routine is called for @protocol only. So, we must build definition
5247 // of protocol's meta-data (not a reference to it!)
5248 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005249 llvm::Constant *Init =
5250 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005251 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005252
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005253 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005254 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005255
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005256 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5257 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005258 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005259 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005260 CGM.getModule(),
5261 Init->getType(), false,
5262 llvm::GlobalValue::WeakAnyLinkage,
5263 Init,
5264 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005265 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5266 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005267 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005268 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005269}
5270
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005271/// GenerateCategory - Build metadata for a category implementation.
5272/// struct _category_t {
5273/// const char * const name;
5274/// struct _class_t *const cls;
5275/// const struct _method_list_t * const instance_methods;
5276/// const struct _method_list_t * const class_methods;
5277/// const struct _protocol_list_t * const protocols;
5278/// const struct _prop_list_t * const properties;
5279/// }
5280///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005281void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005282 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005283 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005284 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5285 "_$_" + OCD->getNameAsString());
5286 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005287 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005288
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005289 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005290 Values[0] = GetClassName(OCD->getIdentifier());
5291 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005292 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005293 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005294 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5295
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005296 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005297 std::vector<llvm::Constant*> Methods;
5298 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005300 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005301
5302 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005303 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005304 // Instance methods should always be defined.
5305 Methods.push_back(GetMethodConstant(*i));
5306 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005307
5308 Values[2] = EmitMethodList(MethodListName,
5309 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005310 Methods);
5311
5312 MethodListName = Prefix;
5313 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5314 OCD->getNameAsString();
5315 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005316 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005317 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005318 // Class methods should always be defined.
5319 Methods.push_back(GetMethodConstant(*i));
5320 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005321
5322 Values[3] = EmitMethodList(MethodListName,
5323 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005324 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005325 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005326 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005327 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005328 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005329 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5330 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005331 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005332 + Interface->getName() + "_$_"
5333 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005334 Category->protocol_begin(),
5335 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005336 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5337 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005338 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005339 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5340 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005341 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342
5343 llvm::Constant *Init =
5344 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005345 Values);
5346 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005348 false,
5349 llvm::GlobalValue::InternalLinkage,
5350 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005351 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005352 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005353 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005354 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005355 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005356 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005357
5358 // Determine if this category is also "non-lazy".
5359 if (ImplementationIsNonLazy(OCD))
5360 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005361 // method definition entries must be clear for next implementation.
5362 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005363}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005364
5365/// GetMethodConstant - Return a struct objc_method constant for the
5366/// given method if it has been defined. The result is null if the
5367/// method has not been defined. The return value has type MethodPtrTy.
5368llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005369 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005370 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005371 if (!Fn)
5372 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005373
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005374 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005375 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005376 ObjCTypes.SelectorPtrTy),
5377 GetMethodVarType(MD),
5378 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5379 };
Owen Anderson08e25242009-07-27 22:29:56 +00005380 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005381}
5382
5383/// EmitMethodList - Build meta-data for method declarations
5384/// struct _method_list_t {
5385/// uint32_t entsize; // sizeof(struct _objc_method)
5386/// uint32_t method_count;
5387/// struct _objc_method method_list[method_count];
5388/// }
5389///
Bill Wendlingbb028552012-02-07 09:25:09 +00005390llvm::Constant *
5391CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5392 const char *Section,
5393 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005394 // Return null for empty list.
5395 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005396 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005398 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005399 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005400 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005401 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005402 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005403 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005404 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005405 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005406 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005407 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005408
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005409 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005410 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005411 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005412 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005413 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005414 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005415 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005416}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005417
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005418/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5419/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005420llvm::GlobalVariable *
5421CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5422 const ObjCIvarDecl *Ivar) {
5423 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005424 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005425 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005426 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005427 CGM.getModule().getGlobalVariable(Name);
5428 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005429 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005430 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005431 false,
5432 llvm::GlobalValue::ExternalLinkage,
5433 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005434 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005435 return IvarOffsetGV;
5436}
5437
Daniel Dunbare83be122010-04-02 21:14:02 +00005438llvm::Constant *
5439CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5440 const ObjCIvarDecl *Ivar,
5441 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005442 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005444 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005445 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005446 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005447
Mike Stumpf5408fe2009-05-16 07:57:57 +00005448 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5449 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005450 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5451 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005452 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005453 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005454 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005455 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005456 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005457 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005458}
5459
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005460/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005461/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005462/// IvarListnfABIPtrTy.
5463/// struct _ivar_t {
5464/// unsigned long int *offset; // pointer to ivar offset location
5465/// char *name;
5466/// char *type;
5467/// uint32_t alignment;
5468/// uint32_t size;
5469/// }
5470/// struct _ivar_list_t {
5471/// uint32 entsize; // sizeof(struct _ivar_t)
5472/// uint32 count;
5473/// struct _iver_t list[count];
5474/// }
5475///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005476
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005477llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005478 const ObjCImplementationDecl *ID) {
5479
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005480 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005481
Jordy Rosedb8264e2011-07-22 02:08:32 +00005482 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005483 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005485 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005486
Jordy Rosedb8264e2011-07-22 02:08:32 +00005487 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005488 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005489 // Ignore unnamed bit-fields.
5490 if (!IVD->getDeclName())
5491 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005492 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005493 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005494 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005495 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5496 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005497 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005498 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00005499 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005500 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005501 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005502 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005503 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005504 // NOTE. Size of a bitfield does not match gcc's, because of the
5505 // way bitfields are treated special in each. But I am told that
5506 // 'size' for bitfield ivars is ignored by the runtime so it does
5507 // not matter. If it matters, there is enough info to get the
5508 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005509 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005510 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005511 }
5512 // Return null for empty list.
5513 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005514 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005515
5516 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00005517 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005518 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5519 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005520 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005521 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005522 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005523 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005524 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5525 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005526 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005527 llvm::GlobalValue::InternalLinkage,
5528 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005529 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005530 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005531 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005532 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005533
Chris Lattnerad64e022009-07-17 23:57:13 +00005534 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005535 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005536}
5537
5538llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005539 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005540 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005541
Fariborz Jahanianda320092009-01-29 19:24:30 +00005542 if (!Entry) {
5543 // We use the initializer as a marker of whether this is a forward
5544 // reference or not. At module finalization we add the empty
5545 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546 Entry =
5547 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5548 llvm::GlobalValue::ExternalLinkage,
5549 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005550 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005551 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005552 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005553
Fariborz Jahanianda320092009-01-29 19:24:30 +00005554 return Entry;
5555}
5556
5557/// GetOrEmitProtocol - Generate the protocol meta-data:
5558/// @code
5559/// struct _protocol_t {
5560/// id isa; // NULL
5561/// const char * const protocol_name;
5562/// const struct _protocol_list_t * protocol_list; // super protocols
5563/// const struct method_list_t * const instance_methods;
5564/// const struct method_list_t * const class_methods;
5565/// const struct method_list_t *optionalInstanceMethods;
5566/// const struct method_list_t *optionalClassMethods;
5567/// const struct _prop_list_t * properties;
5568/// const uint32_t size; // sizeof(struct _protocol_t)
5569/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005570/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005571/// }
5572/// @endcode
5573///
5574
5575llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00005577 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005578
Fariborz Jahanianda320092009-01-29 19:24:30 +00005579 // Early exit if a defining object has already been generated.
5580 if (Entry && Entry->hasInitializer())
5581 return Entry;
5582
Douglas Gregor1d784b22012-01-01 19:51:50 +00005583 // Use the protocol definition, if there is one.
5584 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5585 PD = Def;
5586
Fariborz Jahanianda320092009-01-29 19:24:30 +00005587 // Construct method lists.
5588 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5589 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005590 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005591 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005592 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005593 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005594 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005595 if (!C)
5596 return GetOrEmitProtocolRef(PD);
5597
Fariborz Jahanianda320092009-01-29 19:24:30 +00005598 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5599 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005600 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005601 } else {
5602 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005603 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005604 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005605 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005606
5607 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005608 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005609 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005610 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005611 if (!C)
5612 return GetOrEmitProtocolRef(PD);
5613
Fariborz Jahanianda320092009-01-29 19:24:30 +00005614 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5615 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005616 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005617 } else {
5618 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005619 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005620 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005621 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005622
Bob Wilsondc8dab62011-11-30 01:57:58 +00005623 MethodTypesExt.insert(MethodTypesExt.end(),
5624 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5625
5626 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005627 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005628 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005629 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005630 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5631 PD->protocol_begin(),
5632 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005633
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005634 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005635 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005636 "__DATA, __objc_const",
5637 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005638 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005639 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005640 "__DATA, __objc_const",
5641 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005642 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005643 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005644 "__DATA, __objc_const",
5645 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005646 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005647 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005648 "__DATA, __objc_const",
5649 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005650 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005651 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005652 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00005653 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005654 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005655 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005656 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5657 + PD->getName(),
5658 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005659 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005660 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005661
Fariborz Jahanianda320092009-01-29 19:24:30 +00005662 if (Entry) {
5663 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005664 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005665 Entry->setInitializer(Init);
5666 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005667 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005668 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5669 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5670 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005671 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005672 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005673 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00005674
5675 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005676 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005677 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005678 CGM.AddUsedGlobal(Entry);
5679
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005680 // Use this protocol meta-data to build protocol list table in section
5681 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005682 llvm::GlobalVariable *PTGV =
5683 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5684 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5685 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005686 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005687 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005688 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005689 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005690 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005691 return Entry;
5692}
5693
5694/// EmitProtocolList - Generate protocol list meta-data:
5695/// @code
5696/// struct _protocol_list_t {
5697/// long protocol_count; // Note, this is 32/64 bit
5698/// struct _protocol_t[protocol_count];
5699/// }
5700/// @endcode
5701///
5702llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005703CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005704 ObjCProtocolDecl::protocol_iterator begin,
5705 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00005706 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707
Fariborz Jahanianda320092009-01-29 19:24:30 +00005708 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005710 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005711
Daniel Dunbar948e2582009-02-15 07:36:20 +00005712 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005713 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005714 Name.toVector(TmpName);
5715 llvm::GlobalVariable *GV =
5716 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005717 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005718 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719
Daniel Dunbar948e2582009-02-15 07:36:20 +00005720 for (; begin != end; ++begin)
5721 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5722
Fariborz Jahanianda320092009-01-29 19:24:30 +00005723 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005724 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005725 ObjCTypes.ProtocolnfABIPtrTy));
5726
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005727 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005728 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005729 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005730 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00005731 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5732 ProtocolRefs.size()),
5733 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005734
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005735 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005736 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005737 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005738 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005739 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005740 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005741 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005742 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005743 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005744 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005745}
5746
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005747/// GetMethodDescriptionConstant - This routine build following meta-data:
5748/// struct _objc_method {
5749/// SEL _cmd;
5750/// char *method_type;
5751/// char *_imp;
5752/// }
5753
5754llvm::Constant *
5755CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005756 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005757 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005758 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5759 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005760 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005761 if (!Desc[1])
5762 return 0;
5763
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005764 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005765 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005766 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005767}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005768
5769/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5770/// This code gen. amounts to generating code for:
5771/// @code
5772/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5773/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005774///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005775LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005776 CodeGen::CodeGenFunction &CGF,
5777 QualType ObjectTy,
5778 llvm::Value *BaseValue,
5779 const ObjCIvarDecl *Ivar,
5780 unsigned CVRQualifiers) {
5781 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005782 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5783 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5784 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5785 llvm::MDNode::get(VMContext,
5786 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00005787 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005788 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005789}
5790
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005791llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005792 CodeGen::CodeGenFunction &CGF,
5793 const ObjCInterfaceDecl *Interface,
5794 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005795 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005796}
5797
John McCallb1e81442011-05-13 23:16:18 +00005798static void appendSelectorForMessageRefTable(std::string &buffer,
5799 Selector selector) {
5800 if (selector.isUnarySelector()) {
5801 buffer += selector.getNameForSlot(0);
5802 return;
5803 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005804
John McCallb1e81442011-05-13 23:16:18 +00005805 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5806 buffer += selector.getNameForSlot(i);
5807 buffer += '_';
5808 }
5809}
5810
John McCall944c8432011-05-14 03:10:52 +00005811/// Emit a "v-table" message send. We emit a weak hidden-visibility
5812/// struct, initially containing the selector pointer and a pointer to
5813/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5814/// load and call the function pointer, passing the address of the
5815/// struct as the second parameter. The runtime determines whether
5816/// the selector is currently emitted using vtable dispatch; if so, it
5817/// substitutes a stub function which simply tail-calls through the
5818/// appropriate vtable slot, and if not, it substitues a stub function
5819/// which tail-calls objc_msgSend. Both stubs adjust the selector
5820/// argument to correctly point to the selector.
5821RValue
5822CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5823 ReturnValueSlot returnSlot,
5824 QualType resultType,
5825 Selector selector,
5826 llvm::Value *arg0,
5827 QualType arg0Type,
5828 bool isSuper,
5829 const CallArgList &formalArgs,
5830 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005831 // Compute the actual arguments.
5832 CallArgList args;
5833
John McCall944c8432011-05-14 03:10:52 +00005834 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005835 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005836 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5837 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005838
John McCall944c8432011-05-14 03:10:52 +00005839 // Second argument: a pointer to the message ref structure. Leave
5840 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005841 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5842
5843 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5844
John McCallde5d3c72012-02-17 03:33:10 +00005845 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00005846
John McCallcba681a2011-05-14 21:12:11 +00005847 NullReturnState nullReturn;
5848
John McCall944c8432011-05-14 03:10:52 +00005849 // Find the function to call and the mangled name for the message
5850 // ref structure. Using a different mangled name wouldn't actually
5851 // be a problem; it would just be a waste.
5852 //
5853 // The runtime currently never uses vtable dispatch for anything
5854 // except normal, non-super message-sends.
5855 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005856 llvm::Constant *fn = 0;
5857 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00005858 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005859 if (isSuper) {
5860 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5861 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005862 } else {
John McCallcba681a2011-05-14 21:12:11 +00005863 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005864 fn = ObjCTypes.getMessageSendStretFixupFn();
5865 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005866 }
John McCallb1e81442011-05-13 23:16:18 +00005867 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5868 fn = ObjCTypes.getMessageSendFpretFixupFn();
5869 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005870 } else {
John McCallb1e81442011-05-13 23:16:18 +00005871 if (isSuper) {
5872 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5873 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005874 } else {
John McCallb1e81442011-05-13 23:16:18 +00005875 fn = ObjCTypes.getMessageSendFixupFn();
5876 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005877 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005878 }
John McCallb1e81442011-05-13 23:16:18 +00005879 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5880 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005881
John McCallb1e81442011-05-13 23:16:18 +00005882 // Append the selector name, except use underscores anywhere we
5883 // would have used colons.
5884 appendSelectorForMessageRefTable(messageRefName, selector);
5885
5886 llvm::GlobalVariable *messageRef
5887 = CGM.getModule().getGlobalVariable(messageRefName);
5888 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005889 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005890 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005891 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005892 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5893 init->getType(),
5894 /*constant*/ false,
5895 llvm::GlobalValue::WeakAnyLinkage,
5896 init,
5897 messageRefName);
5898 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5899 messageRef->setAlignment(16);
5900 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5901 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005902
5903 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00005904 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005905 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5906 e = method->param_end(); i != e; ++i) {
5907 const ParmVarDecl *ParamDecl = (*i);
5908 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5909 if (!nullReturn.NullBB)
5910 nullReturn.init(CGF, arg0);
5911 requiresnullCheck = true;
5912 break;
5913 }
5914 }
5915
John McCallb1e81442011-05-13 23:16:18 +00005916 llvm::Value *mref =
5917 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5918
John McCall944c8432011-05-14 03:10:52 +00005919 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005920 args[1].RV = RValue::get(mref);
5921
5922 // Load the function to call from the message ref table.
5923 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5924 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5925
John McCallde5d3c72012-02-17 03:33:10 +00005926 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00005927
John McCallde5d3c72012-02-17 03:33:10 +00005928 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005929 return nullReturn.complete(CGF, result, resultType, formalArgs,
5930 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005931}
5932
5933/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005934CodeGen::RValue
5935CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005936 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005937 QualType ResultType,
5938 Selector Sel,
5939 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005940 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005941 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005942 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005943 return isVTableDispatchedSelector(Sel)
5944 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005945 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005946 false, CallArgs, Method)
5947 : EmitMessageSend(CGF, Return, ResultType,
5948 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005949 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005950 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005951}
5952
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005953llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005954CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005955 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5956
Daniel Dunbardfff2302009-03-02 05:18:14 +00005957 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005958 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005959 false, llvm::GlobalValue::ExternalLinkage,
5960 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005961 }
5962
5963 return GV;
5964}
5965
John McCallf85e1932011-06-15 23:02:42 +00005966llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5967 IdentifierInfo *II) {
5968 llvm::GlobalVariable *&Entry = ClassReferences[II];
5969
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005970 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005971 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005972 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005973 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005974 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5975 false, llvm::GlobalValue::InternalLinkage,
5976 ClassGV,
5977 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005978 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005979 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005980 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005981 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005982 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005983 }
John McCallf85e1932011-06-15 23:02:42 +00005984
Benjamin Kramer578faa82011-09-27 21:06:10 +00005985 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005986}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005987
John McCallf85e1932011-06-15 23:02:42 +00005988llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5989 const ObjCInterfaceDecl *ID) {
5990 return EmitClassRefFromId(Builder, ID->getIdentifier());
5991}
5992
5993llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5994 CGBuilderTy &Builder) {
5995 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5996 return EmitClassRefFromId(Builder, II);
5997}
5998
Daniel Dunbar11394522009-04-18 08:51:00 +00005999llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006000CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00006001 const ObjCInterfaceDecl *ID) {
6002 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003
Daniel Dunbar11394522009-04-18 08:51:00 +00006004 if (!Entry) {
6005 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6006 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006007 Entry =
6008 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006009 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006010 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006011 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006012 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006013 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006014 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006015 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006016 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006017 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006018
Benjamin Kramer578faa82011-09-27 21:06:10 +00006019 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006020}
6021
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006022/// EmitMetaClassRef - Return a Value * of the address of _class_t
6023/// meta-data
6024///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006025llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6026 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006027 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6028 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006029 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006030
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006031 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006032 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006033 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006034 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006035 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006036 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006037 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006038 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006039 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006040 ObjCTypes.ClassnfABIPtrTy));
6041
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006042 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006043 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006044
Benjamin Kramer578faa82011-09-27 21:06:10 +00006045 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006046}
6047
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006048/// GetClass - Return a reference to the class for the given interface
6049/// decl.
6050llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6051 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006052 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006053 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6054 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6055 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6056 }
6057
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006058 return EmitClassRef(Builder, ID);
6059}
6060
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006061/// Generates a message send where the super is the receiver. This is
6062/// a message send to self with special delivery semantics indicating
6063/// which class's method should be called.
6064CodeGen::RValue
6065CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006066 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006067 QualType ResultType,
6068 Selector Sel,
6069 const ObjCInterfaceDecl *Class,
6070 bool isCategoryImpl,
6071 llvm::Value *Receiver,
6072 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006073 const CodeGen::CallArgList &CallArgs,
6074 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006075 // ...
6076 // Create and init a super structure; this is a (receiver, class)
6077 // pair we will pass to objc_msgSendSuper.
6078 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006079 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006080
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006081 llvm::Value *ReceiverAsObject =
6082 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6083 CGF.Builder.CreateStore(ReceiverAsObject,
6084 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006085
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006086 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006087 llvm::Value *Target;
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006088 if (IsClassMessage)
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006089 Target = EmitMetaClassRef(CGF.Builder, Class);
Fariborz Jahanianb9966bd2012-10-10 23:11:18 +00006090 else
Daniel Dunbar11394522009-04-18 08:51:00 +00006091 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006092
Mike Stumpf5408fe2009-05-16 07:57:57 +00006093 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6094 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006095 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006096 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6097 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6098 CGF.Builder.CreateStore(Target,
6099 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006100
John McCall944c8432011-05-14 03:10:52 +00006101 return (isVTableDispatchedSelector(Sel))
6102 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006103 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006104 true, CallArgs, Method)
6105 : EmitMessageSend(CGF, Return, ResultType,
6106 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006107 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006108 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006109}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006110
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006111llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006112 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006113 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006114
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006115 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006116 llvm::Constant *Casted =
6117 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6118 ObjCTypes.SelectorPtrTy);
6119 Entry =
6120 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6121 llvm::GlobalValue::InternalLinkage,
6122 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006123 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006124 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006125 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006126
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006127 if (lval)
6128 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006129 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6130
6131 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6132 llvm::MDNode::get(VMContext,
6133 ArrayRef<llvm::Value*>()));
6134 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006135}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006136/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006137/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006138///
6139void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006140 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006141 llvm::Value *dst,
6142 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006143 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006144 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006145 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006146 assert(Size <= 8 && "does not support size > 8");
6147 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6148 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006149 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6150 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006151 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6152 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006153 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6154 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006155 return;
6156}
6157
6158/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6159/// objc_assign_strongCast (id src, id *dst)
6160///
6161void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006162 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006163 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006164 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006165 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006166 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006167 assert(Size <= 8 && "does not support size > 8");
6168 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006169 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006170 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6171 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006172 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6173 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006174 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006175 src, dst, "weakassign");
6176 return;
6177}
6178
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006179void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006180 CodeGen::CodeGenFunction &CGF,
6181 llvm::Value *DestPtr,
6182 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006183 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006184 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6185 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006186 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006187 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006188 return;
6189}
6190
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006191/// EmitObjCWeakRead - Code gen for loading value of a __weak
6192/// object: objc_read_weak (id *src)
6193///
6194llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006195 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006196 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006197 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006198 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6199 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006200 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006201 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006202 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006203 return read_weak;
6204}
6205
6206/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6207/// objc_assign_weak (id src, id *dst)
6208///
6209void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006210 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006211 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006212 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006213 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006214 assert(Size <= 8 && "does not support size > 8");
6215 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6216 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006217 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6218 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006219 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6220 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006221 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006222 src, dst, "weakassign");
6223 return;
6224}
6225
6226/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6227/// objc_assign_global (id src, id *dst)
6228///
6229void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006230 llvm::Value *src, llvm::Value *dst,
6231 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006232 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006233 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006234 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006235 assert(Size <= 8 && "does not support size > 8");
6236 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6237 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006238 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6239 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006240 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6241 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006242 if (!threadlocal)
6243 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6244 src, dst, "globalassign");
6245 else
6246 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6247 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006248 return;
6249}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006250
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006251void
John McCallf1549f62010-07-06 01:34:17 +00006252CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6253 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006254 EmitAtSynchronizedStmt(CGF, S,
6255 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6256 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006257}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006258
John McCall5a180392010-07-24 00:37:23 +00006259llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006260CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006261 // There's a particular fixed type info for 'id'.
6262 if (T->isObjCIdType() ||
6263 T->isObjCQualifiedIdType()) {
6264 llvm::Constant *IDEHType =
6265 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6266 if (!IDEHType)
6267 IDEHType =
6268 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6269 false,
6270 llvm::GlobalValue::ExternalLinkage,
6271 0, "OBJC_EHTYPE_id");
6272 return IDEHType;
6273 }
6274
6275 // All other types should be Objective-C interface pointer types.
6276 const ObjCObjectPointerType *PT =
6277 T->getAs<ObjCObjectPointerType>();
6278 assert(PT && "Invalid @catch type.");
6279 const ObjCInterfaceType *IT = PT->getInterfaceType();
6280 assert(IT && "Invalid @catch type.");
6281 return GetInterfaceEHType(IT->getDecl(), false);
6282}
6283
John McCallf1549f62010-07-06 01:34:17 +00006284void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6285 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006286 EmitTryCatchStmt(CGF, S,
6287 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6288 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6289 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006290}
6291
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006292/// EmitThrowStmt - Generate code for a throw statement.
6293void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6294 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006295 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006296 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006297 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006298 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006299 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006300 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006301 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006302 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006303 }
6304
John McCall7ec404c2010-10-16 08:21:07 +00006305 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006306 CGF.Builder.ClearInsertionPoint();
6307}
Daniel Dunbare588b992009-03-01 04:46:24 +00006308
John McCall5a180392010-07-24 00:37:23 +00006309llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006310CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006311 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006312 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006313
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006314 // If we don't need a definition, return the entry if found or check
6315 // if we use an external reference.
6316 if (!ForDefinition) {
6317 if (Entry)
6318 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006319
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006320 // If this type (or a super class) has the __objc_exception__
6321 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006322 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006323 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006324 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006325 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006326 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006327 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006328 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006329 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006330
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006331 // Otherwise we need to either make a new entry or fill in the
6332 // initializer.
6333 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006334 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006335 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006336 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006337 CGM.getModule().getGlobalVariable(VTableName);
6338 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006339 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6340 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006341 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006342 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006343
Chris Lattner8b418682012-02-07 00:39:47 +00006344 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006345
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006346 llvm::Constant *Values[] = {
6347 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6348 GetClassName(ID->getIdentifier()),
6349 GetClassGlobal(ClassName)
6350 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006351 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006352 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006353
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006354 if (Entry) {
6355 Entry->setInitializer(Init);
6356 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006357 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006358 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006359 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006360 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006361 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006362 }
6363
David Blaikie4e4d0842012-03-11 07:00:24 +00006364 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006365 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006366 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006367 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006368
6369 if (ForDefinition) {
6370 Entry->setSection("__DATA,__objc_const");
6371 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6372 } else {
6373 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6374 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006375
6376 return Entry;
6377}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006378
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006379/* *** */
6380
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006381CodeGen::CGObjCRuntime *
6382CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006383 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6384 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006385 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006386
6387 case ObjCRuntime::MacOSX:
6388 case ObjCRuntime::iOS:
6389 return new CGObjCNonFragileABIMac(CGM);
6390
David Chisnall11d3f4c2012-07-03 20:49:52 +00006391 case ObjCRuntime::GNUstep:
6392 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006393 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006394 llvm_unreachable("these runtimes are not Mac runtimes");
6395 }
6396 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006397}