blob: 4e4879921a722ebdacfa0246a1a4fc9939294cc3 [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 Wendling603571a2012-10-10 07:36:56 +000066 llvm::Attributes::Builder B;
67 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 Wendling603571a2012-10-10 07:36:56 +000071 llvm::Attributes::get(B));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000072 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000073
John McCall0774cb82011-05-15 01:53:33 +000074 /// void objc_msgSend_stret (id, SEL, ...)
75 ///
76 /// The messenger used when the return value is an aggregate returned
77 /// by indirect reference in the first argument, and therefore the
78 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000079 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000080 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000081 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
82 params, true),
83 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000084
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000085 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000086
John McCall0774cb82011-05-15 01:53:33 +000087 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
88 ///
89 /// The messenger used when the return value is returned on the x87
90 /// floating-point stack; without a special entrypoint, the nil case
91 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000092 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000093 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattner8b418682012-02-07 00:39:47 +000094 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
95 params, true),
John McCall0774cb82011-05-15 01:53:33 +000096 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000097
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000098 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000099
Anders Carlssoneea64802011-10-31 16:27:11 +0000100 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
101 ///
102 /// The messenger used when the return value is returned in two values on the
103 /// x87 floating point stack; without a special entrypoint, the nil case
104 /// would be unbalanced. Only used on 64-bit X86.
105 llvm::Constant *getMessageSendFp2retFn() const {
106 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
107 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
108 llvm::Type *resultType =
109 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
110
111 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
112 params, true),
113 "objc_msgSend_fp2ret");
114 }
115
John McCall0774cb82011-05-15 01:53:33 +0000116 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
117 ///
118 /// The messenger used for super calls, which have different dispatch
119 /// semantics. The class passed is the superclass of the current
120 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000121 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000122 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000123 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000124 params, true),
125 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000126 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000127
John McCall0774cb82011-05-15 01:53:33 +0000128 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
129 ///
130 /// A slightly different messenger used for super calls. The class
131 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000132 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000133 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000134 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000135 params, true),
136 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000137 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000138
John McCall0774cb82011-05-15 01:53:33 +0000139 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
140 /// SEL op, ...)
141 ///
142 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000143 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000144 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000145 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000146 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000147 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000148 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000149
John McCall0774cb82011-05-15 01:53:33 +0000150 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
151 /// SEL op, ...)
152 ///
153 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000154 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000155 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000156 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000157 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000158 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000159 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000161 llvm::Constant *getMessageSendSuperFpretFn() const {
162 // There is no objc_msgSendSuper_fpret? How can that work?
163 return getMessageSendSuperFn();
164 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000165
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000166 llvm::Constant *getMessageSendSuperFpretFn2() const {
167 // There is no objc_msgSendSuper_fpret? How can that work?
168 return getMessageSendSuperFn2();
169 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000170
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000171protected:
172 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000173
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000174public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000175 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000176 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000177
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000178 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000179 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000180
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000181 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000182 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000183
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000184 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000185 llvm::Type *SelectorPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000186
187private:
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000188 /// ProtocolPtrTy - LLVM type for external protocol handles
189 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000190 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor4c86fdb2012-01-17 18:36:30 +0000191
192public:
193 llvm::Type *getExternalProtocolPtrTy() {
194 if (!ExternalProtocolPtrTy) {
195 // FIXME: It would be nice to unify this with the opaque type, so that the
196 // IR comes out a bit cleaner.
197 CodeGen::CodeGenTypes &Types = CGM.getTypes();
198 ASTContext &Ctx = CGM.getContext();
199 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
200 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
201 }
202
203 return ExternalProtocolPtrTy;
204 }
205
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000206 // SuperCTy - clang type for struct objc_super.
207 QualType SuperCTy;
208 // SuperPtrCTy - clang type for struct objc_super *.
209 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000210
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000211 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000212 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000213 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000214 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000215
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000216 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
217 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000218 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000219
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000220 /// PropertyListTy - LLVM type for struct objc_property_list
221 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000222 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000223 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000224 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000225
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000226 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000227 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000228
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000229 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000230 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000231 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000232 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000233
Chris Lattner72db6c32009-04-22 02:44:54 +0000234 llvm::Constant *getGetPropertyFn() {
235 CodeGen::CodeGenTypes &Types = CGM.getTypes();
236 ASTContext &Ctx = CGM.getContext();
237 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000239 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
240 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000241 Params.push_back(IdType);
242 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000243 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000244 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000245 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000246 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
247 FunctionType::ExtInfo(),
248 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000249 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
250 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000251
Chris Lattner72db6c32009-04-22 02:44:54 +0000252 llvm::Constant *getSetPropertyFn() {
253 CodeGen::CodeGenTypes &Types = CGM.getTypes();
254 ASTContext &Ctx = CGM.getContext();
255 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000256 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000257 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
258 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000259 Params.push_back(IdType);
260 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000261 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000262 Params.push_back(IdType);
263 Params.push_back(Ctx.BoolTy);
264 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000265 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000266 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
267 FunctionType::ExtInfo(),
268 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000269 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
270 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000271
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000272 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
273 CodeGen::CodeGenTypes &Types = CGM.getTypes();
274 ASTContext &Ctx = CGM.getContext();
275 // void objc_setProperty_atomic(id self, SEL _cmd,
276 // id newValue, ptrdiff_t offset);
277 // void objc_setProperty_nonatomic(id self, SEL _cmd,
278 // id newValue, ptrdiff_t offset);
279 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
280 // id newValue, ptrdiff_t offset);
281 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
282 // id newValue, ptrdiff_t offset);
283
284 SmallVector<CanQualType,4> Params;
285 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
286 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
287 Params.push_back(IdType);
288 Params.push_back(SelType);
289 Params.push_back(IdType);
290 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
291 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000292 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
293 FunctionType::ExtInfo(),
294 RequiredArgs::All));
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000295 const char *name;
296 if (atomic && copy)
297 name = "objc_setProperty_atomic_copy";
298 else if (atomic && !copy)
299 name = "objc_setProperty_atomic";
300 else if (!atomic && copy)
301 name = "objc_setProperty_nonatomic_copy";
302 else
303 name = "objc_setProperty_nonatomic";
304
305 return CGM.CreateRuntimeFunction(FTy, name);
306 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000307
308 llvm::Constant *getCopyStructFn() {
309 CodeGen::CodeGenTypes &Types = CGM.getTypes();
310 ASTContext &Ctx = CGM.getContext();
311 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000312 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000313 Params.push_back(Ctx.VoidPtrTy);
314 Params.push_back(Ctx.VoidPtrTy);
315 Params.push_back(Ctx.LongTy);
316 Params.push_back(Ctx.BoolTy);
317 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000318 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000319 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
320 FunctionType::ExtInfo(),
321 RequiredArgs::All));
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
323 }
324
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000325 /// This routine declares and returns address of:
326 /// void objc_copyCppObjectAtomic(
327 /// void *dest, const void *src,
328 /// void (*copyHelper) (void *dest, const void *source));
329 llvm::Constant *getCppAtomicObjectFunction() {
330 CodeGen::CodeGenTypes &Types = CGM.getTypes();
331 ASTContext &Ctx = CGM.getContext();
332 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
333 SmallVector<CanQualType,3> Params;
334 Params.push_back(Ctx.VoidPtrTy);
335 Params.push_back(Ctx.VoidPtrTy);
336 Params.push_back(Ctx.VoidPtrTy);
337 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000338 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
339 FunctionType::ExtInfo(),
340 RequiredArgs::All));
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
342 }
343
Chris Lattner72db6c32009-04-22 02:44:54 +0000344 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000345 CodeGen::CodeGenTypes &Types = CGM.getTypes();
346 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000347 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000348 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000349 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000350 llvm::FunctionType *FTy =
John McCall0f3d0972012-07-07 06:41:13 +0000351 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCallde5d3c72012-02-17 03:33:10 +0000352 FunctionType::ExtInfo(),
353 RequiredArgs::All));
Chris Lattner72db6c32009-04-22 02:44:54 +0000354 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
355 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000356
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000357 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000358 llvm::Constant *getGcReadWeakFn() {
359 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000360 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000361 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000362 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000363 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000364 }
365
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000366 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000367 llvm::Constant *getGcAssignWeakFn() {
368 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000369 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000370 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000371 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
373 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000374
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000375 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000376 llvm::Constant *getGcAssignGlobalFn() {
377 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000378 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000379 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000380 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000381 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
382 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000383
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000384 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
385 llvm::Constant *getGcAssignThreadLocalFn() {
386 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000387 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000388 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000389 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000390 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
391 }
392
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000393 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000394 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000395 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000396 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
397 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000398 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000399 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000400 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000402
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000403 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
404 llvm::Constant *GcMemmoveCollectableFn() {
405 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000406 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000407 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000408 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
409 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000410
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000411 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000412 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000413 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000414 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000415 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000416 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000417 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
418 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000419
420 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000421 llvm::Constant *getExceptionThrowFn() {
422 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000423 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000425 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000426 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
427 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000428
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000429 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
430 llvm::Constant *getExceptionRethrowFn() {
431 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000432 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000433 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
434 }
435
Daniel Dunbar1c566672009-02-24 01:43:46 +0000436 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000437 llvm::Constant *getSyncEnterFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000438 // int objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000439 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000440 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000441 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000442 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
443 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000444
Daniel Dunbar1c566672009-02-24 01:43:46 +0000445 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000446 llvm::Constant *getSyncExitFn() {
Aaron Ballman2d234d732012-09-06 16:44:16 +0000447 // int objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000448 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000449 llvm::FunctionType *FTy =
Aaron Ballman2d234d732012-09-06 16:44:16 +0000450 llvm::FunctionType::get(CGM.IntTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000451 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
452 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000453
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000454 llvm::Constant *getSendFn(bool IsSuper) const {
455 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
456 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000457
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000458 llvm::Constant *getSendFn2(bool IsSuper) const {
459 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
460 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000461
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000462 llvm::Constant *getSendStretFn(bool IsSuper) const {
463 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
464 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000465
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000466 llvm::Constant *getSendStretFn2(bool IsSuper) const {
467 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
468 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000469
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000470 llvm::Constant *getSendFpretFn(bool IsSuper) const {
471 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
472 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000473
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000474 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
475 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
476 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000477
Anders Carlssoneea64802011-10-31 16:27:11 +0000478 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
479 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
480 }
481
482 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
483 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
484 }
485
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000486 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
487 ~ObjCCommonTypesHelper(){}
488};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000489
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000490/// ObjCTypesHelper - Helper class that encapsulates lazy
491/// construction of varies types used during ObjC generation.
492class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000493public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000494 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000495 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000496 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000497 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000498 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000499 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000500
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000501 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000502 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000503 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000504 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000505 /// ProtocolExtensionTy - LLVM type for struct
506 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000507 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000508 /// ProtocolExtensionTy - LLVM type for struct
509 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000510 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000511 /// MethodDescriptionTy - LLVM type for struct
512 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000513 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000514 /// MethodDescriptionListTy - LLVM type for struct
515 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000516 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000517 /// MethodDescriptionListPtrTy - LLVM type for struct
518 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000519 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000520 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000521 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000522 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000523 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000524 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000525 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000526 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000527 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000528 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000529 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000530 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000531 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000532 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000533 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000535 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000536 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000538 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000539 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000540 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000541 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000542 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000543 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000544
Anders Carlsson124526b2008-09-09 10:10:21 +0000545 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000546 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000547
Anders Carlsson124526b2008-09-09 10:10:21 +0000548 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000549 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000550 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000551 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000552 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000553 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000554 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000555
556 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000557 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000558 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000559 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000560 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000561 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000562 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000563
564 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000565 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000566 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000567 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000568 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000570 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000571
Anders Carlsson124526b2008-09-09 10:10:21 +0000572 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000573 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000576 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000577 "objc_exception_match");
578
Chris Lattner34b02a12009-04-22 02:26:14 +0000579 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000580
Anders Carlsson124526b2008-09-09 10:10:21 +0000581 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000582 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000583 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000584 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
Bill Wendling603571a2012-10-10 07:36:56 +0000585 llvm::Attributes::Builder B;
586 B.addAttribute(llvm::Attributes::NonLazyBind);
John McCall0774cb82011-05-15 01:53:33 +0000587 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
588 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000589 "_setjmp",
Bill Wendling603571a2012-10-10 07:36:56 +0000590 llvm::Attributes::get(B));
Chris Lattner34b02a12009-04-22 02:26:14 +0000591 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000592
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000593public:
594 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000595 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000596};
597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000599/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000600class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000601public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000602
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000603 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000604 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000606 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000607 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000608
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000609 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000610 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Daniel Dunbar948e2582009-02-15 07:36:20 +0000612 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000613 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000614
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000615 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000616 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000617
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000618 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000619 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000621 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000622 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000623
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000624 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000625 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000627 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000628 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000629
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000630 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000631 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000632
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000633 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000634 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000635
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000636 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000637 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000639 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000640 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000641
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000642 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000643 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000644
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000645 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000647 // MessageRefTy - LLVM for:
648 // struct _message_ref_t {
649 // IMP messenger;
650 // SEL name;
651 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000652 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000653 // MessageRefCTy - clang type for struct _message_ref_t
654 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000656 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000657 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000658 // MessageRefCPtrTy - clang type for struct _message_ref_t*
659 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahanianef163782009-02-05 01:13:09 +0000661 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000662 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000663
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000664 // SuperMessageRefTy - LLVM for:
665 // struct _super_message_ref_t {
666 // SUPER_IMP messenger;
667 // SEL name;
668 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000669 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000671 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000673
Chris Lattner1c02f862009-04-22 02:53:24 +0000674 llvm::Constant *getMessageSendFixupFn() {
675 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000676 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000677 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000678 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000679 "objc_msgSend_fixup");
680 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000681
Chris Lattner1c02f862009-04-22 02:53:24 +0000682 llvm::Constant *getMessageSendFpretFixupFn() {
683 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000684 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000686 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000687 "objc_msgSend_fpret_fixup");
688 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Chris Lattner1c02f862009-04-22 02:53:24 +0000690 llvm::Constant *getMessageSendStretFixupFn() {
691 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000692 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000693 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000694 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000695 "objc_msgSend_stret_fixup");
696 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697
Chris Lattner1c02f862009-04-22 02:53:24 +0000698 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000700 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000701 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000702 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000703 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000704 "objc_msgSendSuper2_fixup");
705 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000706
Chris Lattner1c02f862009-04-22 02:53:24 +0000707 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000708 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000709 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000710 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000711 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000712 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000713 "objc_msgSendSuper2_stret_fixup");
714 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Chris Lattner8a569112009-04-22 02:15:23 +0000716 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000718 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000719
Chris Lattner8a569112009-04-22 02:15:23 +0000720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721
Chris Lattner8a569112009-04-22 02:15:23 +0000722 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000723 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000724 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000725 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000726 "objc_begin_catch");
727 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000728
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000729 llvm::StructType *EHTypeTy;
730 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000731
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000732 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
733 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000734};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000735
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000736class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000737public:
738 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000739 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000740 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000741 unsigned ivar_bytepos;
742 unsigned ivar_size;
743 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000744 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000745
746 // Allow sorting based on byte pos.
747 bool operator<(const GC_IVAR &b) const {
748 return ivar_bytepos < b.ivar_bytepos;
749 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000750 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000751
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000752 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000753 public:
754 unsigned skip;
755 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000756 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000757 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000758 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000759
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000760protected:
Owen Anderson69243822009-07-13 04:10:07 +0000761 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000763 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000765 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000766 SmallVector<GC_IVAR, 16> SkipIvars;
767 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000768
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000769 /// LazySymbols - Symbols to generate a lazy reference for. See
770 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000771 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000772
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000773 /// DefinedSymbols - External symbols which are defined by this
774 /// module. The symbols in this list and LazySymbols are used to add
775 /// special linker symbols which ensure that Objective-C modules are
776 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000777 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000778
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000779 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000780 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000782 /// MethodVarNames - uniqued method variable names.
783 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000784
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000785 /// DefinedCategoryNames - list of category names in form Class_Category.
786 llvm::SetVector<std::string> DefinedCategoryNames;
787
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000788 /// MethodVarTypes - uniqued method type signatures. We have to use
789 /// a StringMap here because have no other unique reference.
790 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000791
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000792 /// MethodDefinitions - map of methods which have been defined in
793 /// this translation unit.
794 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000795
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000796 /// PropertyNames - uniqued method variable names.
797 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000798
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000799 /// ClassReferences - uniqued class references.
800 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000802 /// SelectorReferences - uniqued selector references.
803 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000804
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000805 /// Protocols - Protocols for which an objc_protocol structure has
806 /// been emitted. Forward declarations are handled by creating an
807 /// empty structure whose initializer is filled in when/if defined.
808 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000809
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000810 /// DefinedProtocols - Protocols which have actually been
811 /// defined. We should not need this, see FIXME in GenerateProtocol.
812 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000813
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000814 /// DefinedClasses - List of defined classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000815 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000816
817 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000818 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000819
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000820 /// DefinedCategories - List of defined categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000821 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000822
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000823 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling1e01ac42012-02-07 09:40:07 +0000824 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000825
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000826 /// GetNameForMethod - Return a name for the given method.
827 /// \param[out] NameOut - The return value.
828 void GetNameForMethod(const ObjCMethodDecl *OMD,
829 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000830 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000831
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000832 /// GetMethodVarName - Return a unique constant for the given
833 /// selector's name. The return value has type char *.
834 llvm::Constant *GetMethodVarName(Selector Sel);
835 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000836
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000837 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000838 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000839
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000840 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000841 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
842 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000843 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000844
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000845 /// GetPropertyName - Return a unique constant for the given
846 /// name. The return value has type char *.
847 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000849 // FIXME: This can be dropped once string functions are unified.
850 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
851 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000852
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000853 /// GetClassName - Return a unique constant for the given selector's
854 /// name. The return value has type char *.
855 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000856
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000857 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
858
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000859 /// BuildIvarLayout - Builds ivar layout bitmap for the class
860 /// implementation for the __strong or __weak case.
861 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000862 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
863 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000864
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000865 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000867 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000868 unsigned int BytePos, bool ForStrongLayout,
869 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000870 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000871 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000872 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +0000873 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000874 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000875 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000876
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000877 /// GetIvarLayoutName - Returns a unique constant for the given
878 /// ivar layout bitmap.
879 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
880 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000882 /// EmitPropertyList - Emit the given property list. The return
883 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000884 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000885 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000886 const ObjCContainerDecl *OCD,
887 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000888
Bob Wilsondc8dab62011-11-30 01:57:58 +0000889 /// EmitProtocolMethodTypes - Generate the array of extended method type
890 /// strings. The return value has type Int8PtrPtrTy.
891 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingbb028552012-02-07 09:25:09 +0000892 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000893 const ObjCCommonTypesHelper &ObjCTypes);
894
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000895 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendling3964e622012-02-09 22:16:49 +0000896 void PushProtocolProperties(
897 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
898 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
899 const Decl *Container,
900 const ObjCProtocolDecl *PROTO,
901 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000902
Fariborz Jahanianda320092009-01-29 19:24:30 +0000903 /// GetProtocolRef - Return a reference to the internal protocol
904 /// description, creating an empty one if it has not been
905 /// defined. The return value has type ProtocolPtrTy.
906 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000907
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000908 /// CreateMetadataVar - Create a global variable with internal
909 /// linkage for use by the Objective-C runtime.
910 ///
911 /// This is a convenience wrapper which not only creates the
912 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000913 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000914 ///
915 /// \param Name - The variable name.
916 /// \param Init - The variable initializer; this is also used to
917 /// define the type of the variable.
918 /// \param Section - The section the variable should go into, or 0.
919 /// \param Align - The alignment for the variable, or 0.
920 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000921 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000922 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000923 llvm::Constant *Init,
924 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000925 unsigned Align,
926 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000927
John McCall944c8432011-05-14 03:10:52 +0000928 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
929 ReturnValueSlot Return,
930 QualType ResultType,
931 llvm::Value *Sel,
932 llvm::Value *Arg0,
933 QualType Arg0Ty,
934 bool IsSuper,
935 const CallArgList &CallArgs,
936 const ObjCMethodDecl *OMD,
937 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000938
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000939 /// EmitImageInfo - Emit the image info marker used to encode some module
940 /// level information.
941 void EmitImageInfo();
942
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000943public:
Owen Anderson69243822009-07-13 04:10:07 +0000944 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCallde5d3c72012-02-17 03:33:10 +0000945 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000946
David Chisnall0d13f6f2010-01-23 02:40:42 +0000947 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000948
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000949 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
950 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000951
Fariborz Jahanianda320092009-01-29 19:24:30 +0000952 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000953
Fariborz Jahanianda320092009-01-29 19:24:30 +0000954 /// GetOrEmitProtocol - Get the protocol object for the given
955 /// declaration, emitting it if necessary. The return value has type
956 /// ProtocolPtrTy.
957 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000958
Fariborz Jahanianda320092009-01-29 19:24:30 +0000959 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
960 /// object for the given declaration, emitting it if needed. These
961 /// forward references will be filled in with empty bodies if no
962 /// definition is seen. The return value has type ProtocolPtrTy.
963 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000964 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
965 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000966
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000967};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000968
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000969class CGObjCMac : public CGObjCCommonMac {
970private:
971 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000972
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000973 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000974 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000975 void EmitModuleInfo();
976
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000977 /// EmitModuleSymols - Emit module symbols, the list of defined
978 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000979 llvm::Constant *EmitModuleSymbols();
980
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000981 /// FinishModule - Write out global data structures at the end of
982 /// processing a translation unit.
983 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000985 /// EmitClassExtension - Generate the class extension structure used
986 /// to store the weak ivar layout and properties. The return value
987 /// has type ClassExtensionPtrTy.
988 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
989
990 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
991 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000992 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000993 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000994
John McCallf85e1932011-06-15 23:02:42 +0000995 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
996 IdentifierInfo *II);
997
998 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
999
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001000 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1001 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001002
1003 /// EmitIvarList - Emit the ivar list for the given
1004 /// implementation. If ForClass is true the list of class ivars
1005 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1006 /// interface ivars will be emitted. The return value has type
1007 /// IvarListPtrTy.
1008 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001009 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001010
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001011 /// EmitMetaClass - Emit a forward reference to the class structure
1012 /// for the metaclass of the given interface. The return value has
1013 /// type ClassPtrTy.
1014 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1015
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001016 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001017 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001018 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1019 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00001020 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001021
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001022 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001023
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001024 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001025
1026 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001027 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001028 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001029 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001030 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001031
1032 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001033 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034 /// - TypeName: The name for the type containing the methods.
Sylvestre Ledruf3477c12012-09-27 10:16:10 +00001035 /// - IsProtocol: True iff these methods are for a protocol.
1036 /// - ClassMethds: True iff these are class methods.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001037 /// - Required: When true, only "required" methods are
1038 /// listed. Similarly, when false only "optional" methods are
1039 /// listed. For classes this should always be true.
1040 /// - begin, end: The method list to output.
1041 ///
1042 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001043 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001044 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001045 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001046
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001047 /// GetOrEmitProtocol - Get the protocol object for the given
1048 /// declaration, emitting it if necessary. The return value has type
1049 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001050 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001051
1052 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1053 /// object for the given declaration, emitting it if needed. These
1054 /// forward references will be filled in with empty bodies if no
1055 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001056 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001057
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001058 /// EmitProtocolExtension - Generate the protocol extension
1059 /// structure used to store optional instance and class methods, and
1060 /// protocol properties. The return value has type
1061 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001062 llvm::Constant *
1063 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00001064 ArrayRef<llvm::Constant*> OptInstanceMethods,
1065 ArrayRef<llvm::Constant*> OptClassMethods,
1066 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001067
1068 /// EmitProtocolList - Generate the list of referenced
1069 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001070 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001071 ObjCProtocolDecl::protocol_iterator begin,
1072 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1075 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001076 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1077 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001078
1079public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001080 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001081
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001082 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001083
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001084 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001085 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001086 QualType ResultType,
1087 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001088 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001089 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001090 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001091 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001092
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001093 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001094 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001095 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001096 QualType ResultType,
1097 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001098 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001099 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001100 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001101 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001102 const CallArgList &CallArgs,
1103 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001104
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001105 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001106 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001107
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001108 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1109 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001110
1111 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1112 /// untyped one.
1113 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1114 const ObjCMethodDecl *Method);
1115
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001116 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001117
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001118 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001119
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001120 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001121
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001122 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001123
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001124 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001125 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001126
Chris Lattner74391b42009-03-22 21:03:39 +00001127 virtual llvm::Constant *GetPropertyGetFunction();
1128 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001129 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1130 bool copy);
David Chisnall8fac25d2010-12-26 22:13:16 +00001131 virtual llvm::Constant *GetGetStructFunction();
1132 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001133 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001134 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001135
John McCallf1549f62010-07-06 01:34:17 +00001136 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1137 const ObjCAtTryStmt &S);
1138 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1139 const ObjCAtSynchronizedStmt &S);
1140 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001141 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1142 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001143 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001144 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001145 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001146 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001147 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001148 llvm::Value *src, llvm::Value *dest,
1149 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001150 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001151 llvm::Value *src, llvm::Value *dest,
1152 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001153 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1154 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001155 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1156 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001157 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001158
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001159 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1160 QualType ObjectTy,
1161 llvm::Value *BaseValue,
1162 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001163 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001164 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001165 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001166 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001167
1168 /// GetClassGlobal - Return the global variable for the Objective-C
1169 /// class of the given name.
1170 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001171 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001172 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001173};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001175class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001176private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001177 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001178 llvm::GlobalVariable* ObjCEmptyCacheVar;
1179 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001180
Daniel Dunbar11394522009-04-18 08:51:00 +00001181 /// SuperClassReferences - uniqued super class references.
1182 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001183
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001184 /// MetaClassReferences - uniqued meta class references.
1185 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001186
1187 /// EHTypeReferences - uniqued class ehtype references.
1188 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001189
John McCall944c8432011-05-14 03:10:52 +00001190 /// VTableDispatchMethods - List of methods for which we generate
1191 /// vtable-based message dispatch.
1192 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001193
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001194 /// DefinedMetaClasses - List of defined meta-classes.
1195 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1196
John McCall944c8432011-05-14 03:10:52 +00001197 /// isVTableDispatchedSelector - Returns true if SEL is a
1198 /// vtable-based selector.
1199 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001200
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001201 /// FinishNonFragileABIModule - Write out global data structures at the end of
1202 /// processing a translation unit.
1203 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001204
Daniel Dunbar463b8762009-05-15 21:48:48 +00001205 /// AddModuleClassList - Add the given list of class pointers to the
1206 /// module with the provided symbol and section names.
Bill Wendlingbb028552012-02-07 09:25:09 +00001207 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00001208 const char *SymbolName,
1209 const char *SectionName);
1210
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001211 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1212 unsigned InstanceStart,
1213 unsigned InstanceSize,
1214 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001215 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001216 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001217 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001218 llvm::Constant *ClassRoGV,
1219 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001220
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001221 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001222
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001223 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001224
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001225 /// EmitMethodList - Emit the method list for the given
1226 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001227 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001228 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00001229 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001230 /// EmitIvarList - Emit the ivar list for the given
1231 /// implementation. If ForClass is true the list of class ivars
1232 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1233 /// interface ivars will be emitted. The return value has type
1234 /// IvarListnfABIPtrTy.
1235 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001236
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001237 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001238 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001239 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001240
Fariborz Jahanianda320092009-01-29 19:24:30 +00001241 /// GetOrEmitProtocol - Get the protocol object for the given
1242 /// declaration, emitting it if necessary. The return value has type
1243 /// ProtocolPtrTy.
1244 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245
Fariborz Jahanianda320092009-01-29 19:24:30 +00001246 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1247 /// object for the given declaration, emitting it if needed. These
1248 /// forward references will be filled in with empty bodies if no
1249 /// definition is seen. The return value has type ProtocolPtrTy.
1250 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001251
Fariborz Jahanianda320092009-01-29 19:24:30 +00001252 /// EmitProtocolList - Generate the list of referenced
1253 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001255 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001256 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
John McCall944c8432011-05-14 03:10:52 +00001258 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1259 ReturnValueSlot Return,
1260 QualType ResultType,
1261 Selector Sel,
1262 llvm::Value *Receiver,
1263 QualType Arg0Ty,
1264 bool IsSuper,
1265 const CallArgList &CallArgs,
1266 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001267
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001268 /// GetClassGlobal - Return the global variable for the Objective-C
1269 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001270 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001271
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001272 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001273 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001275 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001276
1277 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1278 IdentifierInfo *II);
1279
1280 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Daniel Dunbar11394522009-04-18 08:51:00 +00001282 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1283 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001284 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1285 const ObjCInterfaceDecl *ID);
1286
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001287 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1288 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001289 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001290 const ObjCInterfaceDecl *ID);
1291
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001292 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1293 /// the given ivar.
1294 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001295 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001296 const ObjCInterfaceDecl *ID,
1297 const ObjCIvarDecl *Ivar);
1298
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001299 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1300 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001301 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1302 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001303
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001304 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001305 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001306 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001307 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001308
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001309 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001310 return "OBJC_METACLASS_$_";
1311 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001313 const char *getClassSymbolPrefix() const {
1314 return "OBJC_CLASS_$_";
1315 }
1316
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001317 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001318 uint32_t &InstanceStart,
1319 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001320
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001321 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001322 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001323 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1324 return CGM.getContext().Selectors.getSelector(0, &II);
1325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001326
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001327 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001328 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1329 return CGM.getContext().Selectors.getSelector(1, &II);
1330 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001331
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001332 /// ImplementationIsNonLazy - Check whether the given category or
1333 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001334 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001335
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001336public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001337 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001338 // FIXME. All stubs for now!
1339 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001340
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001341 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001342 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001343 QualType ResultType,
1344 Selector Sel,
1345 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001346 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001347 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001348 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001349
1350 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001351 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001352 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 QualType ResultType,
1354 Selector Sel,
1355 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001356 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 llvm::Value *Receiver,
1358 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001359 const CallArgList &CallArgs,
1360 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001361
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001362 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001363 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001364
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001365 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1366 bool lvalue = false)
1367 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001368
1369 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1370 /// untyped one.
1371 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1372 const ObjCMethodDecl *Method)
1373 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001374
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001375 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001376
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001377 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +00001378
Daniel Dunbar85fdea02012-02-28 15:36:15 +00001379 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall29254f42012-01-31 18:59:20 +00001380
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001381 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001382 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001383
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001384 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001385
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001386 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001387 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001388 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001389 virtual llvm::Constant *GetPropertySetFunction() {
1390 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001391 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001392
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001393 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1394 bool copy) {
1395 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1396 }
1397
David Chisnall8fac25d2010-12-26 22:13:16 +00001398 virtual llvm::Constant *GetSetStructFunction() {
1399 return ObjCTypes.getCopyStructFn();
1400 }
1401 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001402 return ObjCTypes.getCopyStructFn();
1403 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001404 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1405 return ObjCTypes.getCppAtomicObjectFunction();
1406 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001407
Chris Lattner74391b42009-03-22 21:03:39 +00001408 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001409 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001410 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001411
John McCallf1549f62010-07-06 01:34:17 +00001412 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1413 const ObjCAtTryStmt &S);
1414 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1415 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001416 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001417 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001418 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001419 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001420 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001421 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001423 llvm::Value *src, llvm::Value *dest,
1424 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001426 llvm::Value *src, llvm::Value *dest,
1427 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001429 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001430 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1431 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001432 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001433 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1434 QualType ObjectTy,
1435 llvm::Value *BaseValue,
1436 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001437 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001438 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001439 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001440 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001441};
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001442
1443/// A helper class for performing the null-initialization of a return
1444/// value.
1445struct NullReturnState {
1446 llvm::BasicBlock *NullBB;
1447 llvm::BasicBlock *callBB;
1448 NullReturnState() : NullBB(0), callBB(0) {}
1449
1450 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1451 // Make blocks for the null-init and call edges.
1452 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1453 callBB = CGF.createBasicBlock("msgSend.call");
1454
1455 // Check for a null receiver and, if there is one, jump to the
1456 // null-init test.
1457 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1458 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1459
1460 // Otherwise, start performing the call.
1461 CGF.EmitBlock(callBB);
1462 }
1463
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001464 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1465 const CallArgList &CallArgs,
1466 const ObjCMethodDecl *Method) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001467 if (!NullBB) return result;
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001468
1469 llvm::Value *NullInitPtr = 0;
1470 if (result.isScalar() && !resultType->isVoidType()) {
1471 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1472 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1473 }
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001474
1475 // Finish the call path.
1476 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1477 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1478
1479 // Emit the null-init block and perform the null-initialization there.
1480 CGF.EmitBlock(NullBB);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001481
1482 // Release consumed arguments along the null-receiver path.
1483 if (Method) {
1484 CallArgList::const_iterator I = CallArgs.begin();
1485 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1486 e = Method->param_end(); i != e; ++i, ++I) {
1487 const ParmVarDecl *ParamDecl = (*i);
1488 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1489 RValue RV = I->RV;
1490 assert(RV.isScalar() &&
1491 "NullReturnState::complete - arg not on object");
1492 CGF.EmitARCRelease(RV.getScalarVal(), true);
1493 }
1494 }
1495 }
1496
1497 if (result.isScalar()) {
1498 if (NullInitPtr)
1499 CGF.EmitNullInitialization(NullInitPtr, resultType);
1500 // Jump to the continuation block.
1501 CGF.EmitBlock(contBB);
1502 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1503 : result;
1504 }
1505
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001506 if (!resultType->isAnyComplexType()) {
1507 assert(result.isAggregate() && "null init of non-aggregate result?");
1508 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1509 // Jump to the continuation block.
1510 CGF.EmitBlock(contBB);
1511 return result;
1512 }
1513
1514 // _Complex type
1515 // FIXME. Now easy to handle any other scalar type whose result is returned
1516 // in memory due to ABI limitations.
1517 CGF.EmitBlock(contBB);
1518 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1519 llvm::Type *MemberType = CallCV.first->getType();
1520 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1521 // Create phi instruction for scalar complex value.
1522 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1523 PHIReal->addIncoming(ZeroCV, NullBB);
1524 PHIReal->addIncoming(CallCV.first, callBB);
1525 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1526 PHIImag->addIncoming(ZeroCV, NullBB);
1527 PHIImag->addIncoming(CallCV.second, callBB);
1528 return RValue::getComplex(PHIReal, PHIImag);
1529 }
1530};
1531
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001532} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001533
1534/* *** Helper Functions *** */
1535
1536/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001537static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001538 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001539 unsigned idx0,
1540 unsigned idx1) {
1541 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001542 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1543 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001544 };
Jay Foada5c04342011-07-21 14:31:17 +00001545 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001546}
1547
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001548/// hasObjCExceptionAttribute - Return true if this class or any super
1549/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001550static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001551 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001552 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001553 return true;
1554 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001555 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001556 return false;
1557}
1558
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001559/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001560
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001561CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001562 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001563 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001564 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001565}
1566
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001567/// GetClass - Return a reference to the class for the given interface
1568/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001569llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570 const ObjCInterfaceDecl *ID) {
1571 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001572}
1573
1574/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001575llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1576 bool lval) {
1577 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001578}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001579llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001580 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001581 return EmitSelector(Builder, Method->getSelector());
1582}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001583
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001584llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001585 if (T->isObjCIdType() ||
1586 T->isObjCQualifiedIdType()) {
1587 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001588 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001589 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001590 if (T->isObjCClassType() ||
1591 T->isObjCQualifiedClassType()) {
1592 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001593 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001594 }
1595 if (T->isObjCObjectPointerType())
1596 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1597
John McCall5a180392010-07-24 00:37:23 +00001598 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall5a180392010-07-24 00:37:23 +00001599}
1600
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001601/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001602/*
1603 struct __builtin_CFString {
1604 const int *isa; // point to __CFConstantStringClassReference
1605 int flags;
1606 const char *str;
1607 long length;
1608 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001609*/
1610
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001611/// or Generate a constant NSString object.
1612/*
1613 struct __builtin_NSString {
1614 const int *isa; // point to __NSConstantStringClassReference
1615 const char *str;
1616 unsigned int length;
1617 };
1618*/
1619
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001620llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001621 const StringLiteral *SL) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001622 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001623 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001624 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001625}
1626
Ted Kremenekebcb57a2012-03-06 20:05:56 +00001627enum {
1628 kCFTaggedObjectID_Integer = (1 << 1) + 1
1629};
1630
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001631/// Generates a message send where the super is the receiver. This is
1632/// a message send to self with special delivery semantics indicating
1633/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001634CodeGen::RValue
1635CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001636 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001637 QualType ResultType,
1638 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001639 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001640 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001641 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001642 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001643 const CodeGen::CallArgList &CallArgs,
1644 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001645 // Create and init a super structure; this is a (receiver, class)
1646 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001647 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001648 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001649 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001650 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001651 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001652 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001653
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001654 // If this is a class message the metaclass is passed as the target.
1655 llvm::Value *Target;
1656 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001657 if (isCategoryImpl) {
1658 // Message sent to 'super' in a class method defined in a category
1659 // implementation requires an odd treatment.
1660 // If we are in a class method, we must retrieve the
1661 // _metaclass_ for the current class, pointed at by
1662 // the class's "isa" pointer. The following assumes that
1663 // isa" is the first ivar in a class (which it must be).
1664 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1665 Target = CGF.Builder.CreateStructGEP(Target, 0);
1666 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001667 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001668 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1669 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1670 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1671 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001672 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001673 }
1674 else if (isCategoryImpl)
1675 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1676 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001677 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1678 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1679 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001680 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001681 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1682 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001683 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001684 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001685 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001686 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001687 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001688 return EmitMessageSend(CGF, Return, ResultType,
1689 EmitSelector(CGF.Builder, Sel),
1690 ObjCSuper, ObjCTypes.SuperPtrCTy,
1691 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001692}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001693
1694/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001695CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001696 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001697 QualType ResultType,
1698 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001699 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001700 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001701 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001702 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001703 return EmitMessageSend(CGF, Return, ResultType,
1704 EmitSelector(CGF.Builder, Sel),
1705 Receiver, CGF.getContext().getObjCIdType(),
1706 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001707}
1708
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001709CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001710CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1711 ReturnValueSlot Return,
1712 QualType ResultType,
1713 llvm::Value *Sel,
1714 llvm::Value *Arg0,
1715 QualType Arg0Ty,
1716 bool IsSuper,
1717 const CallArgList &CallArgs,
1718 const ObjCMethodDecl *Method,
1719 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001720 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001721 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001722 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001723 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1724 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001725 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001726
John McCallde5d3c72012-02-17 03:33:10 +00001727 // If we're calling a method, use the formal signature.
1728 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001729
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001730 if (Method)
1731 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1732 CGM.getContext().getCanonicalType(ResultType) &&
1733 "Result type mismatch!");
1734
John McCallcba681a2011-05-14 21:12:11 +00001735 NullReturnState nullReturn;
1736
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001737 llvm::Constant *Fn = NULL;
John McCallde5d3c72012-02-17 03:33:10 +00001738 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian4e1524b2012-01-29 20:27:13 +00001739 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001740 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001741 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001742 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1743 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1744 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001745 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1746 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1747 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001748 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001749 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001750 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001751 }
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001752
1753 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00001754 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001755 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1756 e = Method->param_end(); i != e; ++i) {
1757 const ParmVarDecl *ParamDecl = (*i);
1758 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1759 if (!nullReturn.NullBB)
1760 nullReturn.init(CGF, Arg0);
1761 requiresnullCheck = true;
1762 break;
1763 }
1764 }
1765
John McCallde5d3c72012-02-17 03:33:10 +00001766 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1767 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanian3c267e62012-01-30 21:40:37 +00001768 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1769 requiresnullCheck ? Method : 0);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001770}
1771
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001772static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1773 if (FQT.isObjCGCStrong())
1774 return Qualifiers::Strong;
1775
John McCallf85e1932011-06-15 23:02:42 +00001776 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001777 return Qualifiers::Weak;
1778
Fariborz Jahanianba83c952012-02-16 00:15:02 +00001779 // check for __unsafe_unretained
1780 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1781 return Qualifiers::GCNone;
1782
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001783 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1784 return Qualifiers::Strong;
1785
1786 if (const PointerType *PT = FQT->getAs<PointerType>())
1787 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1788
1789 return Qualifiers::GCNone;
1790}
1791
John McCall6b5a61b2011-02-07 10:33:21 +00001792llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1793 const CGBlockInfo &blockInfo) {
Chris Lattner8b418682012-02-07 00:39:47 +00001794 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall6b5a61b2011-02-07 10:33:21 +00001795
David Blaikie4e4d0842012-03-11 07:00:24 +00001796 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1797 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001798 return nullPtr;
1799
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001800 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001801 SkipIvars.clear();
1802 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001803 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1804 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001805
Fariborz Jahanian81979822010-09-09 00:21:45 +00001806 // __isa is the first field in block descriptor and must assume by runtime's
1807 // convention that it is GC'able.
1808 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001809
1810 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1811
1812 // Calculate the basic layout of the block structure.
1813 const llvm::StructLayout *layout =
Micah Villmow25a6a842012-10-08 16:25:52 +00001814 CGM.getDataLayout().getStructLayout(blockInfo.StructureType);
John McCall6b5a61b2011-02-07 10:33:21 +00001815
1816 // Ignore the optional 'this' capture: C++ objects are not assumed
1817 // to be GC'ed.
1818
1819 // Walk the captured variables.
1820 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1821 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1822 const VarDecl *variable = ci->getVariable();
1823 QualType type = variable->getType();
1824
1825 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1826
1827 // Ignore constant captures.
1828 if (capture.isConstant()) continue;
1829
1830 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1831
1832 // __block variables are passed by their descriptor address.
1833 if (ci->isByRef()) {
1834 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001835 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001836 }
1837
1838 assert(!type->isArrayType() && "array variable should not be caught");
1839 if (const RecordType *record = type->getAs<RecordType>()) {
1840 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001841 continue;
1842 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001843
John McCall6b5a61b2011-02-07 10:33:21 +00001844 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1845 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1846
1847 if (GCAttr == Qualifiers::Strong)
1848 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1849 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001850 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001851 SkipIvars.push_back(GC_IVAR(fieldOffset,
1852 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001853 }
1854
1855 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001856 return nullPtr;
1857
1858 // Sort on byte position; captures might not be allocated in order,
1859 // and unions can do funny things.
1860 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1861 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001862
1863 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001864 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikie4e4d0842012-03-11 07:00:24 +00001865 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001866 printf("\n block variable layout for block: ");
Roman Divacky31ba6132012-09-06 15:59:27 +00001867 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00001868 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001869 if (!(s[i] & 0xf0))
1870 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1871 else
1872 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1873 printf("\n");
1874 }
1875
1876 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001877}
1878
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001879llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001880 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001881 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001882 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001883 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1884
Owen Anderson3c4972d2009-07-29 18:54:39 +00001885 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00001886 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001887}
1888
Fariborz Jahanianda320092009-01-29 19:24:30 +00001889void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001890 // FIXME: We shouldn't need this, the protocol decl should contain enough
1891 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001892 DefinedProtocols.insert(PD->getIdentifier());
1893
1894 // If we have generated a forward reference to this protocol, emit
1895 // it now. Otherwise do nothing, the protocol objects are lazily
1896 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001897 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001898 GetOrEmitProtocol(PD);
1899}
1900
Fariborz Jahanianda320092009-01-29 19:24:30 +00001901llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001902 if (DefinedProtocols.count(PD->getIdentifier()))
1903 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001904
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001905 return GetOrEmitProtocolRef(PD);
1906}
1907
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001908/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001909// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1910struct _objc_protocol {
1911struct _objc_protocol_extension *isa;
1912char *protocol_name;
1913struct _objc_protocol_list *protocol_list;
1914struct _objc__method_prototype_list *instance_methods;
1915struct _objc__method_prototype_list *class_methods
1916};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001917
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001918See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001920llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00001921 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001922
1923 // Early exit if a defining object has already been generated.
1924 if (Entry && Entry->hasInitializer())
1925 return Entry;
1926
Douglas Gregor1d784b22012-01-01 19:51:50 +00001927 // Use the protocol definition, if there is one.
1928 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1929 PD = Def;
1930
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001931 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001932 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001933 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1934
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001935 // Construct method lists.
1936 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1937 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001938 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001939 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001940 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001941 ObjCMethodDecl *MD = *i;
1942 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001943 if (!C)
1944 return GetOrEmitProtocolRef(PD);
1945
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001946 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1947 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001948 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001949 } else {
1950 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001951 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001952 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001953 }
1954
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001955 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001956 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001957 ObjCMethodDecl *MD = *i;
1958 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001959 if (!C)
1960 return GetOrEmitProtocolRef(PD);
1961
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001962 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1963 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001964 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001965 } else {
1966 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001967 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001969 }
1970
Bob Wilsondc8dab62011-11-30 01:57:58 +00001971 MethodTypesExt.insert(MethodTypesExt.end(),
1972 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1973
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001974 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001975 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1976 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001977 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001978 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001979 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001980 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001981 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001982 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001983 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001984 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001985 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001986 ClassMethods)
1987 };
Owen Anderson08e25242009-07-27 22:29:56 +00001988 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001989 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001990
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001991 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001992 // Already created, fix the linkage and update the initializer.
1993 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001994 Entry->setInitializer(Init);
1995 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001997 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001998 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001999 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002000 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002001 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002002 // FIXME: Is this necessary? Why only for protocol?
2003 Entry->setAlignment(4);
John McCall50651b92012-03-30 21:29:05 +00002004
2005 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002006 }
Chris Lattnerad64e022009-07-17 23:57:13 +00002007 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002008
2009 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002010}
2011
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002012llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002013 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2014
2015 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002016 // We use the initializer as a marker of whether this is a forward
2017 // reference or not. At module finalization we add the empty
2018 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002019 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00002020 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002021 llvm::GlobalValue::ExternalLinkage,
2022 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002023 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002024 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002025 // FIXME: Is this necessary? Why only for protocol?
2026 Entry->setAlignment(4);
2027 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002028
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002029 return Entry;
2030}
2031
2032/*
2033 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002034 uint32_t size;
2035 struct objc_method_description_list *optional_instance_methods;
2036 struct objc_method_description_list *optional_class_methods;
2037 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00002038 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002039 };
2040*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002041llvm::Constant *
2042CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingbb028552012-02-07 09:25:09 +00002043 ArrayRef<llvm::Constant*> OptInstanceMethods,
2044 ArrayRef<llvm::Constant*> OptClassMethods,
2045 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002046 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002047 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002048 llvm::Constant *Values[] = {
2049 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002050 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002051 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002052 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002053 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002054 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002055 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002056 OptClassMethods),
2057 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00002058 ObjCTypes),
2059 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2060 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002061 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002063 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002064 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00002065 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002066 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002067
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002069 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002070
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002071 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002072 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002073 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002074 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002075}
2076
2077/*
2078 struct objc_protocol_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002079 struct objc_protocol_list *next;
2080 long count;
2081 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002082 };
2083*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00002084llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002085CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00002086 ObjCProtocolDecl::protocol_iterator begin,
2087 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00002088 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002089
Daniel Dunbardbc93372008-08-21 21:57:41 +00002090 for (; begin != end; ++begin)
2091 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002092
2093 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002094 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002095 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002096
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002097 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002098 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002099
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002100 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002102 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002103 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002104 ProtocolRefs.size() - 1);
2105 Values[2] =
2106 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2107 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002108 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002110 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002111 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002112 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002113 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002114 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002115}
2116
Bill Wendlingbb028552012-02-07 09:25:09 +00002117void CGObjCCommonMac::
2118PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendling3964e622012-02-09 22:16:49 +00002119 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingbb028552012-02-07 09:25:09 +00002120 const Decl *Container,
2121 const ObjCProtocolDecl *PROTO,
2122 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002123 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2124 E = PROTO->protocol_end(); P != E; ++P)
2125 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2126 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2127 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002128 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002129 if (!PropertySet.insert(PD->getIdentifier()))
2130 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002131 llvm::Constant *Prop[] = {
2132 GetPropertyName(PD->getIdentifier()),
2133 GetPropertyTypeString(PD, Container)
2134 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002135 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2136 }
2137}
2138
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002139/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002140 struct _objc_property {
Bill Wendling3964e622012-02-09 22:16:49 +00002141 const char * const name;
2142 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002143 };
2144
2145 struct _objc_property_list {
Bill Wendling3964e622012-02-09 22:16:49 +00002146 uint32_t entsize; // sizeof (struct _objc_property)
2147 uint32_t prop_count;
2148 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002149 };
2150*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002151llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002152 const Decl *Container,
2153 const ObjCContainerDecl *OCD,
2154 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendling3964e622012-02-09 22:16:49 +00002155 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002156 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002157 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2158 E = OCD->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002159 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002160 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002161 llvm::Constant *Prop[] = {
2162 GetPropertyName(PD->getIdentifier()),
2163 GetPropertyTypeString(PD, Container)
2164 };
Owen Anderson08e25242009-07-27 22:29:56 +00002165 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002166 Prop));
2167 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002168 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002169 for (ObjCInterfaceDecl::all_protocol_iterator
2170 P = OID->all_referenced_protocol_begin(),
2171 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002172 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2173 ObjCTypes);
2174 }
2175 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2176 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2177 E = CD->protocol_end(); P != E; ++P)
2178 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2179 ObjCTypes);
2180 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002181
2182 // Return null for empty list.
2183 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002184 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002185
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186 unsigned PropertySize =
Micah Villmow25a6a842012-10-08 16:25:52 +00002187 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002188 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002189 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2190 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002191 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002192 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002193 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002194 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002195
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002196 llvm::GlobalVariable *GV =
2197 CreateMetadataVar(Name, Init,
2198 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002199 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002200 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002201 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002202 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002203}
2204
Bill Wendlingbb028552012-02-07 09:25:09 +00002205llvm::Constant *
2206CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2207 ArrayRef<llvm::Constant*> MethodTypes,
2208 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilsondc8dab62011-11-30 01:57:58 +00002209 // Return null for empty list.
2210 if (MethodTypes.empty())
2211 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2212
2213 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2214 MethodTypes.size());
2215 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2216
2217 llvm::GlobalVariable *GV =
2218 CreateMetadataVar(Name, Init,
2219 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2220 (ObjCABI == 2) ? 8 : 4,
2221 true);
2222 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2223}
2224
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002225/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002226 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002227 int count;
2228 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002229 };
2230*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002231llvm::Constant *
2232CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002233 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002234 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002235 ObjCTypes.SelectorPtrTy),
2236 GetMethodVarType(MD)
2237 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002238 if (!Desc[1])
2239 return 0;
2240
Owen Anderson08e25242009-07-27 22:29:56 +00002241 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002242 Desc);
2243}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002244
Bill Wendlingbb028552012-02-07 09:25:09 +00002245llvm::Constant *
2246CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2247 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002248 // Return null for empty list.
2249 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002250 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002251
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002252 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002253 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002254 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002255 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002256 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002257 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002258
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002259 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002260 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002261 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002262}
2263
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002264/*
2265 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002266 char *category_name;
2267 char *class_name;
2268 struct _objc_method_list *instance_methods;
2269 struct _objc_method_list *class_methods;
2270 struct _objc_protocol_list *protocols;
2271 uint32_t size; // <rdar://4585769>
2272 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002273 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002274*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002275void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Micah Villmow25a6a842012-10-08 16:25:52 +00002276 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002277
Mike Stumpf5408fe2009-05-16 07:57:57 +00002278 // FIXME: This is poor design, the OCD should have a pointer to the category
2279 // decl. Additionally, note that Category can be null for the @implementation
2280 // w/o an @interface case. Sema should just create one for us as it does for
2281 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002282 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002283 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002284 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002285
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002286 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002287 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2288 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002289
Bill Wendling3964e622012-02-09 22:16:49 +00002290 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002291 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002292 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002293 // Instance methods should always be defined.
2294 InstanceMethods.push_back(GetMethodConstant(*i));
2295 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002296 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002297 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002298 // Class methods should always be defined.
2299 ClassMethods.push_back(GetMethodConstant(*i));
2300 }
2301
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002302 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002303 Values[0] = GetClassName(OCD->getIdentifier());
2304 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002305 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002306 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002307 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002308 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002309 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002310 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002311 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002312 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002313 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002314 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002315 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002316 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002317 Category->protocol_begin(),
2318 Category->protocol_end());
2319 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002320 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002321 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002322 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002323
2324 // If there is no category @interface then there can be no properties.
2325 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002326 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002327 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002328 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002329 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002330 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002331
Owen Anderson08e25242009-07-27 22:29:56 +00002332 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002333 Values);
2334
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002335 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002336 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002337 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002338 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002339 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002340 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002341 // method definition entries must be clear for next implementation.
2342 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002343}
2344
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345// FIXME: Get from somewhere?
2346enum ClassFlags {
2347 eClassFlags_Factory = 0x00001,
2348 eClassFlags_Meta = 0x00002,
2349 // <rdr://5142207>
2350 eClassFlags_HasCXXStructors = 0x02000,
2351 eClassFlags_Hidden = 0x20000,
2352 eClassFlags_ABI2_Hidden = 0x00010,
2353 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2354};
2355
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002356/*
2357 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002358 Class isa;
2359 Class super_class;
2360 const char *name;
2361 long version;
2362 long info;
2363 long instance_size;
2364 struct _objc_ivar_list *ivars;
2365 struct _objc_method_list *methods;
2366 struct _objc_cache *cache;
2367 struct _objc_protocol_list *protocols;
2368 // Objective-C 1.0 extensions (<rdr://4585769>)
2369 const char *ivar_layout;
2370 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002371 };
2372
2373 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002374*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002375void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002376 DefinedSymbols.insert(ID->getIdentifier());
2377
Chris Lattner8ec03f52008-11-24 03:54:41 +00002378 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002380 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002381 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002383 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002384 Interface->all_referenced_protocol_begin(),
2385 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002386 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002387 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002388 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002389 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002390 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391
2392 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002393 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002394 Flags |= eClassFlags_Hidden;
2395
Bill Wendling3964e622012-02-09 22:16:49 +00002396 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002398 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002399 // Instance methods should always be defined.
2400 InstanceMethods.push_back(GetMethodConstant(*i));
2401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002402 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002403 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002404 // Class methods should always be defined.
2405 ClassMethods.push_back(GetMethodConstant(*i));
2406 }
2407
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002408 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002409 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00002410 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002411
2412 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2413 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2414
2415 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2416 if (llvm::Constant *C = GetMethodConstant(MD))
2417 InstanceMethods.push_back(C);
2418 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2419 if (llvm::Constant *C = GetMethodConstant(MD))
2420 InstanceMethods.push_back(C);
2421 }
2422 }
2423
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002424 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002425 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002427 // Record a reference to the super class.
2428 LazySymbols.insert(Super->getIdentifier());
2429
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002430 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002431 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432 ObjCTypes.ClassPtrTy);
2433 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002434 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002435 }
2436 Values[ 2] = GetClassName(ID->getIdentifier());
2437 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002438 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2439 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2440 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002441 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002442 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002443 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002444 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002445 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002446 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002447 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002449 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002450 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002451 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002452 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002453 std::string Name("\01L_OBJC_CLASS_");
2454 Name += ClassName;
2455 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2456 // Check for a forward reference.
2457 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2458 if (GV) {
2459 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2460 "Forward metaclass reference has incorrect type.");
2461 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2462 GV->setInitializer(Init);
2463 GV->setSection(Section);
2464 GV->setAlignment(4);
2465 CGM.AddUsedGlobal(GV);
2466 }
2467 else
2468 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002469 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002470 // method definition entries must be clear for next implementation.
2471 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002472}
2473
2474llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2475 llvm::Constant *Protocols,
Bill Wendlingbb028552012-02-07 09:25:09 +00002476 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002477 unsigned Flags = eClassFlags_Meta;
Micah Villmow25a6a842012-10-08 16:25:52 +00002478 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479
John McCall1fb0caa2010-10-22 21:05:15 +00002480 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002482
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002483 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484 // The isa for the metaclass is the root of the hierarchy.
2485 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2486 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2487 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002488 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002489 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002490 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002491 // The super class for the metaclass is emitted as the name of the
2492 // super class. The runtime fixes this up to point to the
2493 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002494 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002495 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002496 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002497 ObjCTypes.ClassPtrTy);
2498 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002499 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 }
2501 Values[ 2] = GetClassName(ID->getIdentifier());
2502 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002503 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2504 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2505 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002506 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002507 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002508 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002509 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002510 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002511 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002512 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002513 Values[ 9] = Protocols;
2514 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002515 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002516 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002517 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002518 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002519 Values);
2520
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002521 std::string Name("\01L_OBJC_METACLASS_");
Benjamin Kramer94be8ea2012-07-31 11:45:39 +00002522 Name += ID->getName();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002523
2524 // Check for a forward reference.
2525 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2526 if (GV) {
2527 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2528 "Forward metaclass reference has incorrect type.");
2529 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2530 GV->setInitializer(Init);
2531 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002532 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002533 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002534 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002535 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002536 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002537 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002538 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539
2540 return GV;
2541}
2542
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002543llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002544 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002545
Mike Stumpf5408fe2009-05-16 07:57:57 +00002546 // FIXME: Should we look these up somewhere other than the module. Its a bit
2547 // silly since we only generate these while processing an implementation, so
2548 // exactly one pointer would work if know when we entered/exitted an
2549 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002550
2551 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002552 // Previously, metaclass with internal linkage may have been defined.
2553 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002554 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2555 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002556 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2557 "Forward metaclass reference has incorrect type.");
2558 return GV;
2559 } else {
2560 // Generate as an external reference to keep a consistent
2561 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002562 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002563 llvm::GlobalValue::ExternalLinkage,
2564 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002565 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002566 }
2567}
2568
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002569llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2570 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2571
2572 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2573 true)) {
2574 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2575 "Forward class metadata reference has incorrect type.");
2576 return GV;
2577 } else {
2578 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2579 llvm::GlobalValue::ExternalLinkage,
2580 0,
2581 Name);
2582 }
2583}
2584
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002585/*
2586 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002587 uint32_t size;
2588 const char *weak_ivar_layout;
2589 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002590 };
2591*/
2592llvm::Constant *
2593CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002594 uint64_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00002595 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002596
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002597 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002598 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002599 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002600 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002601 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002602
2603 // Return null if no extension bits are used.
2604 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002605 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002606
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002607 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002608 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002609 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002610 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002611 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002612}
2613
2614/*
2615 struct objc_ivar {
Bill Wendling795b1002012-02-22 09:30:11 +00002616 char *ivar_name;
2617 char *ivar_type;
2618 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002619 };
2620
2621 struct objc_ivar_list {
Bill Wendling795b1002012-02-22 09:30:11 +00002622 int ivar_count;
2623 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002624 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002625*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002626llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002627 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002628 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002629
2630 // When emitting the root class GCC emits ivar entries for the
2631 // actual class structure. It is not clear if we need to follow this
2632 // behavior; for now lets try and get away with not doing it. If so,
2633 // the cleanest solution would be to make up an ObjCInterfaceDecl
2634 // for the class.
2635 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002636 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002637
Jordy Rosedb8264e2011-07-22 02:08:32 +00002638 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002639
Jordy Rosedb8264e2011-07-22 02:08:32 +00002640 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002641 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002642 // Ignore unnamed bit-fields.
2643 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002644 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002645 llvm::Constant *Ivar[] = {
2646 GetMethodVarName(IVD->getIdentifier()),
2647 GetMethodVarType(IVD),
2648 llvm::ConstantInt::get(ObjCTypes.IntTy,
2649 ComputeIvarBaseOffset(CGM, OID, IVD))
2650 };
Owen Anderson08e25242009-07-27 22:29:56 +00002651 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002652 }
2653
2654 // Return null for empty list.
2655 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002656 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002657
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002658 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002659 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002660 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002661 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002662 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002663 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002664
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002665 llvm::GlobalVariable *GV;
2666 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002667 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002668 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002669 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002670 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002671 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002672 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002673 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002674 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002675}
2676
2677/*
2678 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002679 SEL method_name;
2680 char *method_types;
2681 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002682 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002683
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002684 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002685 struct objc_method_list *obsolete;
2686 int count;
2687 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002688 };
2689*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002690
2691/// GetMethodConstant - Return a struct objc_method constant for the
2692/// given method if it has been defined. The result is null if the
2693/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002694llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002695 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002696 if (!Fn)
2697 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002698
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002699 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002700 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002701 ObjCTypes.SelectorPtrTy),
2702 GetMethodVarType(MD),
2703 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2704 };
Owen Anderson08e25242009-07-27 22:29:56 +00002705 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002706}
2707
Chris Lattner5f9e2722011-07-23 10:55:15 +00002708llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002709 const char *Section,
Bill Wendlingbb028552012-02-07 09:25:09 +00002710 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002711 // Return null for empty list.
2712 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002713 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002714
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002715 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002716 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002717 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002718 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002719 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002720 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002721 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002722
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002723 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002724 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002725}
2726
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002727llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002728 const ObjCContainerDecl *CD) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002729 SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002730 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002731
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002732 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002733 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00002734 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002735 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002736 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002737 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002738 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002739 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002740 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002741
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002742 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002743}
2744
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002745llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002746CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002747 llvm::Constant *Init,
2748 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002749 unsigned Align,
2750 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002751 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002752 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002753 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002754 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002755 if (Section)
2756 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002757 if (Align)
2758 GV->setAlignment(Align);
2759 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002760 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002761 return GV;
2762}
2763
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002764llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002765 // Abuse this interface function as a place to finalize.
2766 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002767 return NULL;
2768}
2769
Chris Lattner74391b42009-03-22 21:03:39 +00002770llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002771 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002772}
2773
Chris Lattner74391b42009-03-22 21:03:39 +00002774llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002775 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002776}
2777
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002778llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2779 bool copy) {
2780 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2781}
2782
David Chisnall8fac25d2010-12-26 22:13:16 +00002783llvm::Constant *CGObjCMac::GetGetStructFunction() {
2784 return ObjCTypes.getCopyStructFn();
2785}
2786llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002787 return ObjCTypes.getCopyStructFn();
2788}
2789
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002790llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2791 return ObjCTypes.getCppAtomicObjectFunction();
2792}
2793
Chris Lattner74391b42009-03-22 21:03:39 +00002794llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002795 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002796}
2797
John McCallf1549f62010-07-06 01:34:17 +00002798void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2799 return EmitTryOrSynchronizedStmt(CGF, S);
2800}
2801
2802void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2803 const ObjCAtSynchronizedStmt &S) {
2804 return EmitTryOrSynchronizedStmt(CGF, S);
2805}
2806
John McCallcc505292010-07-21 06:59:36 +00002807namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002808 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002809 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002810 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002811 llvm::Value *CallTryExitVar;
2812 llvm::Value *ExceptionData;
2813 ObjCTypesHelper &ObjCTypes;
2814 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002815 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002816 llvm::Value *CallTryExitVar,
2817 llvm::Value *ExceptionData,
2818 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002819 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002820 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2821
John McCallad346f42011-07-12 20:27:29 +00002822 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002823 // Check whether we need to call objc_exception_try_exit.
2824 // In optimized code, this branch will always be folded.
2825 llvm::BasicBlock *FinallyCallExit =
2826 CGF.createBasicBlock("finally.call_exit");
2827 llvm::BasicBlock *FinallyNoCallExit =
2828 CGF.createBasicBlock("finally.no_call_exit");
2829 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2830 FinallyCallExit, FinallyNoCallExit);
2831
2832 CGF.EmitBlock(FinallyCallExit);
2833 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2834 ->setDoesNotThrow();
2835
2836 CGF.EmitBlock(FinallyNoCallExit);
2837
2838 if (isa<ObjCAtTryStmt>(S)) {
2839 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002840 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2841 // Save the current cleanup destination in case there's
2842 // control flow inside the finally statement.
2843 llvm::Value *CurCleanupDest =
2844 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2845
John McCallcc505292010-07-21 06:59:36 +00002846 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2847
John McCalld96a8e72010-08-11 00:16:14 +00002848 if (CGF.HaveInsertPoint()) {
2849 CGF.Builder.CreateStore(CurCleanupDest,
2850 CGF.getNormalCleanupDestSlot());
2851 } else {
2852 // Currently, the end of the cleanup must always exist.
2853 CGF.EnsureInsertPoint();
2854 }
2855 }
John McCallcc505292010-07-21 06:59:36 +00002856 } else {
2857 // Emit objc_sync_exit(expr); as finally's sole statement for
2858 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002859 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002860 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2861 ->setDoesNotThrow();
2862 }
2863 }
2864 };
John McCall87bb5822010-07-31 23:20:56 +00002865
2866 class FragileHazards {
2867 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002868 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002869 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2870
2871 llvm::InlineAsm *ReadHazard;
2872 llvm::InlineAsm *WriteHazard;
2873
2874 llvm::FunctionType *GetAsmFnType();
2875
2876 void collectLocals();
2877 void emitReadHazard(CGBuilderTy &Builder);
2878
2879 public:
2880 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002881
John McCall87bb5822010-07-31 23:20:56 +00002882 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002883 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002884 };
2885}
2886
2887/// Create the fragile-ABI read and write hazards based on the current
2888/// state of the function, which is presumed to be immediately prior
2889/// to a @try block. These hazards are used to maintain correct
2890/// semantics in the face of optimization and the fragile ABI's
2891/// cavalier use of setjmp/longjmp.
2892FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2893 collectLocals();
2894
2895 if (Locals.empty()) return;
2896
2897 // Collect all the blocks in the function.
2898 for (llvm::Function::iterator
2899 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2900 BlocksBeforeTry.insert(&*I);
2901
2902 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2903
2904 // Create a read hazard for the allocas. This inhibits dead-store
2905 // optimizations and forces the values to memory. This hazard is
2906 // inserted before any 'throwing' calls in the protected scope to
2907 // reflect the possibility that the variables might be read from the
2908 // catch block if the call throws.
2909 {
2910 std::string Constraint;
2911 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2912 if (I) Constraint += ',';
2913 Constraint += "*m";
2914 }
2915
2916 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2917 }
2918
2919 // Create a write hazard for the allocas. This inhibits folding
2920 // loads across the hazard. This hazard is inserted at the
2921 // beginning of the catch path to reflect the possibility that the
2922 // variables might have been written within the protected scope.
2923 {
2924 std::string Constraint;
2925 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2926 if (I) Constraint += ',';
2927 Constraint += "=*m";
2928 }
2929
2930 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2931 }
2932}
2933
2934/// Emit a write hazard at the current location.
2935void FragileHazards::emitWriteHazard() {
2936 if (Locals.empty()) return;
2937
Jay Foad4c7d9f12011-07-15 08:37:34 +00002938 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002939}
2940
John McCall87bb5822010-07-31 23:20:56 +00002941void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2942 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002943 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002944}
2945
2946/// Emit read hazards in all the protected blocks, i.e. all the blocks
2947/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002948void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002949 if (Locals.empty()) return;
2950
2951 CGBuilderTy Builder(CGF.getLLVMContext());
2952
2953 // Iterate through all blocks, skipping those prior to the try.
2954 for (llvm::Function::iterator
2955 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2956 llvm::BasicBlock &BB = *FI;
2957 if (BlocksBeforeTry.count(&BB)) continue;
2958
2959 // Walk through all the calls in the block.
2960 for (llvm::BasicBlock::iterator
2961 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2962 llvm::Instruction &I = *BI;
2963
2964 // Ignore instructions that aren't non-intrinsic calls.
2965 // These are the only calls that can possibly call longjmp.
2966 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2967 if (isa<llvm::IntrinsicInst>(I))
2968 continue;
2969
2970 // Ignore call sites marked nounwind. This may be questionable,
2971 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2972 llvm::CallSite CS(&I);
2973 if (CS.doesNotThrow()) continue;
2974
John McCall0b251722010-08-04 05:59:32 +00002975 // Insert a read hazard before the call. This will ensure that
2976 // any writes to the locals are performed before making the
2977 // call. If the call throws, then this is sufficient to
2978 // guarantee correctness as long as it doesn't also write to any
2979 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002980 Builder.SetInsertPoint(&BB, BI);
2981 emitReadHazard(Builder);
2982 }
2983 }
2984}
2985
2986static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2987 if (V) S.insert(V);
2988}
2989
2990void FragileHazards::collectLocals() {
2991 // Compute a set of allocas to ignore.
2992 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2993 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2994 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002995
2996 // Collect all the allocas currently in the function. This is
2997 // probably way too aggressive.
2998 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2999 for (llvm::BasicBlock::iterator
3000 I = Entry.begin(), E = Entry.end(); I != E; ++I)
3001 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
3002 Locals.push_back(&*I);
3003}
3004
3005llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003006 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00003007 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3008 tys[i] = Locals[i]->getType();
3009 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00003010}
3011
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003012/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003013
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003014 Objective-C setjmp-longjmp (sjlj) Exception Handling
3015 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003016
John McCallf1549f62010-07-06 01:34:17 +00003017 A catch buffer is a setjmp buffer plus:
3018 - a pointer to the exception that was caught
3019 - a pointer to the previous exception data buffer
3020 - two pointers of reserved storage
3021 Therefore catch buffers form a stack, with a pointer to the top
3022 of the stack kept in thread-local storage.
3023
3024 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3025 objc_exception_try_exit pops the given catch buffer, which is
3026 required to be the top of the EH stack.
3027 objc_exception_throw pops the top of the EH stack, writes the
3028 thrown exception into the appropriate field, and longjmps
3029 to the setjmp buffer. It crashes the process (with a printf
3030 and an abort()) if there are no catch buffers on the stack.
3031 objc_exception_extract just reads the exception pointer out of the
3032 catch buffer.
3033
3034 There's no reason an implementation couldn't use a light-weight
3035 setjmp here --- something like __builtin_setjmp, but API-compatible
3036 with the heavyweight setjmp. This will be more important if we ever
3037 want to implement correct ObjC/C++ exception interactions for the
3038 fragile ABI.
3039
3040 Note that for this use of setjmp/longjmp to be correct, we may need
3041 to mark some local variables volatile: if a non-volatile local
3042 variable is modified between the setjmp and the longjmp, it has
3043 indeterminate value. For the purposes of LLVM IR, it may be
3044 sufficient to make loads and stores within the @try (to variables
3045 declared outside the @try) volatile. This is necessary for
3046 optimized correctness, but is not currently being done; this is
3047 being tracked as rdar://problem/8160285
3048
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003049 The basic framework for a @try-catch-finally is as follows:
3050 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003051 objc_exception_data d;
3052 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00003053 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003054
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003055 objc_exception_try_enter(&d);
3056 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003057 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003058 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003059 // exception path
3060 id _caught = objc_exception_extract(&d);
3061
3062 // enter new try scope for handlers
3063 if (!setjmp(d.jmp_buf)) {
3064 ... match exception and execute catch blocks ...
3065
3066 // fell off end, rethrow.
3067 _rethrow = _caught;
3068 ... jump-through-finally to finally_rethrow ...
3069 } else {
3070 // exception in catch block
3071 _rethrow = objc_exception_extract(&d);
3072 _call_try_exit = false;
3073 ... jump-through-finally to finally_rethrow ...
3074 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003075 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003076 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003077
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003078 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00003079 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003080 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00003081
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003082 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00003083 ... dispatch to finally destination ...
3084
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00003086 objc_exception_throw(_rethrow);
3087
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003088 finally_end:
3089 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003090
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003091 This framework differs slightly from the one gcc uses, in that gcc
3092 uses _rethrow to determine if objc_exception_try_exit should be called
3093 and if the object should be rethrown. This breaks in the face of
3094 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003095
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003097
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003098 - If there are no catch blocks, then we avoid emitting the second
3099 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003100
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003101 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3102 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003103
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003104 - FIXME: If there is no @finally block we can do a few more
3105 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003106
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003107 Rethrows and Jumps-Through-Finally
3108 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003109
John McCallf1549f62010-07-06 01:34:17 +00003110 '@throw;' is supported by pushing the currently-caught exception
3111 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003112
John McCallf1549f62010-07-06 01:34:17 +00003113 Branches through the @finally block are handled with an ordinary
3114 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3115 exceptions are not compatible with C++ exceptions, and this is
3116 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00003117
John McCallf1549f62010-07-06 01:34:17 +00003118 @synchronized(expr) { stmt; } is emitted as if it were:
3119 id synch_value = expr;
3120 objc_sync_enter(synch_value);
3121 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003122*/
3123
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003124void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3125 const Stmt &S) {
3126 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003127
3128 // A destination for the fall-through edges of the catch handlers to
3129 // jump to.
3130 CodeGenFunction::JumpDest FinallyEnd =
3131 CGF.getJumpDestInCurrentScope("finally.end");
3132
3133 // A destination for the rethrow edge of the catch handlers to jump
3134 // to.
3135 CodeGenFunction::JumpDest FinallyRethrow =
3136 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003137
Daniel Dunbar1c566672009-02-24 01:43:46 +00003138 // For @synchronized, call objc_sync_enter(sync.expr). The
3139 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003140 // @synchronized. We can't avoid a temp here because we need the
3141 // value to be preserved. If the backend ever does liveness
3142 // correctly after setjmp, this will be unnecessary.
3143 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003144 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003145 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003146 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3147 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003148 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3149 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003150
3151 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3152 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003153 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003154
John McCall0b251722010-08-04 05:59:32 +00003155 // Allocate memory for the setjmp buffer. This needs to be kept
3156 // live throughout the try and catch blocks.
3157 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3158 "exceptiondata.ptr");
3159
John McCall87bb5822010-07-31 23:20:56 +00003160 // Create the fragile hazards. Note that this will not capture any
3161 // of the allocas required for exception processing, but will
3162 // capture the current basic block (which extends all the way to the
3163 // setjmp call) as "before the @try".
3164 FragileHazards Hazards(CGF);
3165
John McCallf1549f62010-07-06 01:34:17 +00003166 // Create a flag indicating whether the cleanup needs to call
3167 // objc_exception_try_exit. This is true except when
3168 // - no catches match and we're branching through the cleanup
3169 // just to rethrow the exception, or
3170 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003171 // The setjmp-safety rule here is that we should always store to this
3172 // variable in a place that dominates the branch through the cleanup
3173 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003174 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003175 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176
John McCall9e2213d2010-10-04 23:42:51 +00003177 // A slot containing the exception to rethrow. Only needed when we
3178 // have both a @catch and a @finally.
3179 llvm::Value *PropagatingExnVar = 0;
3180
John McCallf1549f62010-07-06 01:34:17 +00003181 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003182 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003183 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003184 CallTryExitVar,
3185 ExceptionData,
3186 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003187
3188 // Enter a try block:
3189 // - Call objc_exception_try_enter to push ExceptionData on top of
3190 // the EH stack.
3191 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3192 ->setDoesNotThrow();
3193
3194 // - Call setjmp on the exception data buffer.
3195 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3196 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3197 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003198 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003199 llvm::CallInst *SetJmpResult =
3200 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3201 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003202 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003203
3204 // If setjmp returned 0, enter the protected block; otherwise,
3205 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003206 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3207 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003208 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003209 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3210 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003211
John McCallf1549f62010-07-06 01:34:17 +00003212 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003213 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003214 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003215 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003216 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003217
3218 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003219
John McCallf1549f62010-07-06 01:34:17 +00003220 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003221 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003222
John McCall87bb5822010-07-31 23:20:56 +00003223 // Don't optimize loads of the in-scope locals across this point.
3224 Hazards.emitWriteHazard();
3225
John McCallf1549f62010-07-06 01:34:17 +00003226 // For a @synchronized (or a @try with no catches), just branch
3227 // through the cleanup to the rethrow block.
3228 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3229 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003230 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003231 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003232
3233 // Otherwise, we have to match against the caught exceptions.
3234 } else {
John McCall0b251722010-08-04 05:59:32 +00003235 // Retrieve the exception object. We may emit multiple blocks but
3236 // nothing can cross this so the value is already in SSA form.
3237 llvm::CallInst *Caught =
3238 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3239 ExceptionData, "caught");
3240 Caught->setDoesNotThrow();
3241
John McCallf1549f62010-07-06 01:34:17 +00003242 // Push the exception to rethrow onto the EH value stack for the
3243 // benefit of any @throws in the handlers.
3244 CGF.ObjCEHValueStack.push_back(Caught);
3245
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003246 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003247
John McCall0b251722010-08-04 05:59:32 +00003248 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003249
John McCall0b251722010-08-04 05:59:32 +00003250 llvm::BasicBlock *CatchBlock = 0;
3251 llvm::BasicBlock *CatchHandler = 0;
3252 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003253 // Save the currently-propagating exception before
3254 // objc_exception_try_enter clears the exception slot.
3255 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3256 "propagating_exception");
3257 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3258
John McCall0b251722010-08-04 05:59:32 +00003259 // Enter a new exception try block (in case a @catch block
3260 // throws an exception).
3261 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3262 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003263
John McCall0b251722010-08-04 05:59:32 +00003264 llvm::CallInst *SetJmpResult =
3265 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3266 "setjmp.result");
3267 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003268 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003269
John McCall0b251722010-08-04 05:59:32 +00003270 llvm::Value *Threw =
3271 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3272
3273 CatchBlock = CGF.createBasicBlock("catch");
3274 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3275 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3276
3277 CGF.EmitBlock(CatchBlock);
3278 }
3279
3280 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003281
Daniel Dunbar55e40722008-09-27 07:03:52 +00003282 // Handle catch list. As a special case we check if everything is
3283 // matched and avoid generating code for falling off the end if
3284 // so.
3285 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003286 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3287 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003288
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003289 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003290 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003291
Anders Carlsson80f25672008-09-09 17:59:25 +00003292 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003293 if (!CatchParam) {
3294 AllMatched = true;
3295 } else {
John McCall183700f2009-09-21 23:43:11 +00003296 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003297
John McCallf1549f62010-07-06 01:34:17 +00003298 // catch(id e) always matches under this ABI, since only
3299 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003300 // FIXME: For the time being we also match id<X>; this should
3301 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003302 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003303 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003304 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003305
John McCallf1549f62010-07-06 01:34:17 +00003306 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003307 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003308 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3309
Anders Carlssondde0a942008-09-11 09:15:33 +00003310 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003311 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003312 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003313
3314 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003315 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003316 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003317
Anders Carlssondde0a942008-09-11 09:15:33 +00003318 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003319
3320 // The scope of the catch variable ends right here.
3321 CatchVarCleanups.ForceCleanup();
3322
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003323 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003324 break;
3325 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003326
Steve Naroff14108da2009-07-10 23:34:53 +00003327 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003328 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003329
3330 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003331 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3332 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003333
3334 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003335 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336
John McCallf1549f62010-07-06 01:34:17 +00003337 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003338 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3339 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003340 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003341
John McCallf1549f62010-07-06 01:34:17 +00003342 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3343 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003344
3345 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003346 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003347
Anders Carlsson80f25672008-09-09 17:59:25 +00003348 // Emit the @catch block.
3349 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003350
3351 // Collect any cleanups for the catch variable. The scope lasts until
3352 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003353 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003354
John McCallb6bbcc92010-10-15 04:57:14 +00003355 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003356 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003357
John McCallf1549f62010-07-06 01:34:17 +00003358 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003359 llvm::Value *Tmp =
3360 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003361 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003362 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003363
Anders Carlssondde0a942008-09-11 09:15:33 +00003364 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003365
3366 // We're done with the catch variable.
3367 CatchVarCleanups.ForceCleanup();
3368
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003369 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003370
Anders Carlsson80f25672008-09-09 17:59:25 +00003371 CGF.EmitBlock(NextCatchBlock);
3372 }
3373
John McCallf1549f62010-07-06 01:34:17 +00003374 CGF.ObjCEHValueStack.pop_back();
3375
John McCall0b251722010-08-04 05:59:32 +00003376 // If nothing wanted anything to do with the caught exception,
3377 // kill the extract call.
3378 if (Caught->use_empty())
3379 Caught->eraseFromParent();
3380
3381 if (!AllMatched)
3382 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3383
3384 if (HasFinally) {
3385 // Emit the exception handler for the @catch blocks.
3386 CGF.EmitBlock(CatchHandler);
3387
3388 // In theory we might now need a write hazard, but actually it's
3389 // unnecessary because there's no local-accessing code between
3390 // the try's write hazard and here.
3391 //Hazards.emitWriteHazard();
3392
John McCall9e2213d2010-10-04 23:42:51 +00003393 // Extract the new exception and save it to the
3394 // propagating-exception slot.
3395 assert(PropagatingExnVar);
3396 llvm::CallInst *NewCaught =
3397 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3398 ExceptionData, "caught");
3399 NewCaught->setDoesNotThrow();
3400 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3401
John McCall0b251722010-08-04 05:59:32 +00003402 // Don't pop the catch handler; the throw already did.
3403 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003404 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003405 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003406 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003407
John McCall87bb5822010-07-31 23:20:56 +00003408 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003409 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003410
John McCallf1549f62010-07-06 01:34:17 +00003411 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003412 CGF.Builder.restoreIP(TryFallthroughIP);
3413 if (CGF.HaveInsertPoint())
3414 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003415 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003416 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003417
John McCallf1549f62010-07-06 01:34:17 +00003418 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003419 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003420 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003421 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003422 // If we have a propagating-exception variable, check it.
3423 llvm::Value *PropagatingExn;
3424 if (PropagatingExnVar) {
3425 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003426
John McCall9e2213d2010-10-04 23:42:51 +00003427 // Otherwise, just look in the buffer for the exception to throw.
3428 } else {
3429 llvm::CallInst *Caught =
3430 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3431 ExceptionData);
3432 Caught->setDoesNotThrow();
3433 PropagatingExn = Caught;
3434 }
3435
3436 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003437 ->setDoesNotThrow();
3438 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003439 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003440
John McCall87bb5822010-07-31 23:20:56 +00003441 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003442}
3443
3444void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003445 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003446 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003447
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003448 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003449 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003450 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003451 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003452 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003453 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003454 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003455 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003456 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003457
John McCallf1549f62010-07-06 01:34:17 +00003458 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3459 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003460 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003461
3462 // Clear the insertion point to indicate we are in unreachable code.
3463 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003464}
3465
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003466/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003467/// object: objc_read_weak (id *src)
3468///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003469llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003470 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003471 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003472 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3473 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3474 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003475 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003476 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003477 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003478 return read_weak;
3479}
3480
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003481/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3482/// objc_assign_weak (id src, id *dst)
3483///
3484void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003485 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003486 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003487 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003488 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003489 assert(Size <= 8 && "does not support size > 8");
3490 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003491 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003492 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3493 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003494 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3495 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003496 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003497 src, dst, "weakassign");
3498 return;
3499}
3500
Fariborz Jahanian58626502008-11-19 00:59:10 +00003501/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3502/// objc_assign_global (id src, id *dst)
3503///
3504void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003505 llvm::Value *src, llvm::Value *dst,
3506 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003507 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003508 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003509 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003510 assert(Size <= 8 && "does not support size > 8");
3511 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003512 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003513 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3514 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003515 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3516 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003517 if (!threadlocal)
3518 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3519 src, dst, "globalassign");
3520 else
3521 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3522 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003523 return;
3524}
3525
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003526/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003527/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003528///
3529void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003530 llvm::Value *src, llvm::Value *dst,
3531 llvm::Value *ivarOffset) {
3532 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003533 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003534 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003535 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003536 assert(Size <= 8 && "does not support size > 8");
3537 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003538 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003539 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3540 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003541 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3542 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003543 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3544 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003545 return;
3546}
3547
Fariborz Jahanian58626502008-11-19 00:59:10 +00003548/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3549/// objc_assign_strongCast (id src, id *dst)
3550///
3551void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003552 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003553 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003554 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00003555 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003556 assert(Size <= 8 && "does not support size > 8");
3557 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003558 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003559 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3560 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003561 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3562 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003563 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003564 src, dst, "weakassign");
3565 return;
3566}
3567
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003568void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569 llvm::Value *DestPtr,
3570 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003571 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003572 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3573 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003574 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003575 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003576 return;
3577}
3578
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003579/// EmitObjCValueForIvar - Code Gen for ivar reference.
3580///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003581LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3582 QualType ObjectTy,
3583 llvm::Value *BaseValue,
3584 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003585 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003586 const ObjCInterfaceDecl *ID =
3587 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003588 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3589 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003590}
3591
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003592llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003593 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003594 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003595 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003596 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003597 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3598 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003599}
3600
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003601/* *** Private Interface *** */
3602
3603/// EmitImageInfo - Emit the image info marker used to encode some module
3604/// level information.
3605///
3606/// See: <rdr://4810609&4810587&4810587>
3607/// struct IMAGE_INFO {
3608/// unsigned version;
3609/// unsigned flags;
3610/// };
3611enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003612 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003613 eImageInfo_GarbageCollected = (1 << 1),
3614 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003615 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3616
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003617 // A flag indicating that the module has no instances of a @synthesize of a
3618 // superclass variable. <rdar://problem/6803242>
Bill Wendling09822512012-04-24 11:04:57 +00003619 eImageInfo_CorrectedSynthesize = (1 << 4),
3620 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003621};
3622
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003623void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003624 unsigned version = 0; // Version is unused?
Bill Wendling3973acc2012-02-16 01:13:30 +00003625 const char *Section = (ObjCABI == 1) ?
3626 "__OBJC, __image_info,regular" :
3627 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003628
Bill Wendling3973acc2012-02-16 01:13:30 +00003629 // Generate module-level named metadata to convey this information to the
3630 // linker and code-gen.
3631 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003632
Bill Wendling3973acc2012-02-16 01:13:30 +00003633 // Add the ObjC ABI version to the module flags.
3634 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3635 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3636 version);
3637 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3638 llvm::MDString::get(VMContext,Section));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003639
David Blaikie4e4d0842012-03-11 07:00:24 +00003640 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003641 // Non-GC overrides those files which specify GC.
3642 Mod.addModuleFlag(llvm::Module::Override,
3643 "Objective-C Garbage Collection", (uint32_t)0);
3644 } else {
3645 // Add the ObjC garbage collection value.
3646 Mod.addModuleFlag(llvm::Module::Error,
3647 "Objective-C Garbage Collection",
3648 eImageInfo_GarbageCollected);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003649
David Blaikie4e4d0842012-03-11 07:00:24 +00003650 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendling3973acc2012-02-16 01:13:30 +00003651 // Add the ObjC GC Only value.
3652 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3653 eImageInfo_GCOnly);
3654
3655 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3656 llvm::Value *Ops[2] = {
3657 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3658 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3659 eImageInfo_GarbageCollected)
3660 };
3661 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3662 llvm::MDNode::get(VMContext, Ops));
3663 }
3664 }
Bill Wendling09822512012-04-24 11:04:57 +00003665
3666 // Indicate whether we're compiling this to run on a simulator.
3667 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3668 if (Triple.getOS() == llvm::Triple::IOS &&
3669 (Triple.getArch() == llvm::Triple::x86 ||
3670 Triple.getArch() == llvm::Triple::x86_64))
3671 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3672 eImageInfo_ImageIsSimulated);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003673}
3674
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003675// struct objc_module {
3676// unsigned long version;
3677// unsigned long size;
3678// const char *name;
3679// Symtab symtab;
3680// };
3681
3682// FIXME: Get from somewhere
3683static const int ModuleVersion = 7;
3684
3685void CGObjCMac::EmitModuleInfo() {
Micah Villmow25a6a842012-10-08 16:25:52 +00003686 uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003688 llvm::Constant *Values[] = {
3689 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3690 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3691 // This used to be the filename, now it is unused. <rdr://4327263>
3692 GetClassName(&CGM.getContext().Idents.get("")),
3693 EmitModuleSymbols()
3694 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003695 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003696 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003697 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003698 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003699}
3700
3701llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003702 unsigned NumClasses = DefinedClasses.size();
3703 unsigned NumCategories = DefinedCategories.size();
3704
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003705 // Return null if no symbols were defined.
3706 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003707 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003708
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003709 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003710 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003711 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003712 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3713 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003714
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003715 // The runtime expects exactly the list of defined classes followed
3716 // by the list of defined categories, in a single array.
Chris Lattner0b239712012-02-06 22:16:34 +00003717 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003718 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003719 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003720 ObjCTypes.Int8PtrTy);
3721 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003722 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003723 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003724 ObjCTypes.Int8PtrTy);
3725
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003726 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003727 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00003728 Symbols.size()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003729 Symbols);
3730
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003731 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003732
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003733 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003734 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3735 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003736 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003737 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003738}
3739
John McCallf85e1932011-06-15 23:02:42 +00003740llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3741 IdentifierInfo *II) {
3742 LazySymbols.insert(II);
3743
3744 llvm::GlobalVariable *&Entry = ClassReferences[II];
3745
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003746 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003747 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003748 llvm::ConstantExpr::getBitCast(GetClassName(II),
3749 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003750 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003751 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3752 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3753 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003754 }
John McCallf85e1932011-06-15 23:02:42 +00003755
Benjamin Kramer578faa82011-09-27 21:06:10 +00003756 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003757}
3758
John McCallf85e1932011-06-15 23:02:42 +00003759llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3760 const ObjCInterfaceDecl *ID) {
3761 return EmitClassRefFromId(Builder, ID->getIdentifier());
3762}
3763
3764llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3765 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3766 return EmitClassRefFromId(Builder, II);
3767}
3768
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003769llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3770 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003771 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003772
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003773 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003774 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003775 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003776 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003777 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003778 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3779 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003780 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003781 }
3782
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003783 if (lvalue)
3784 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003785 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003786}
3787
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003788llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003789 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003790
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003791 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003792 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00003793 llvm::ConstantDataArray::getString(VMContext,
3794 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003795 ((ObjCABI == 2) ?
3796 "__TEXT,__objc_classname,cstring_literals" :
3797 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003798 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003799
Owen Andersona1cf15f2009-07-14 23:10:40 +00003800 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003801}
3802
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003803llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3804 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3805 I = MethodDefinitions.find(MD);
3806 if (I != MethodDefinitions.end())
3807 return I->second;
3808
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003809 return NULL;
3810}
3811
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003812/// GetIvarLayoutName - Returns a unique constant for the given
3813/// ivar layout bitmap.
3814llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003815 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003816 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003817}
3818
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003819void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003820 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003821 bool ForStrongLayout,
3822 bool &HasUnion) {
3823 const RecordDecl *RD = RT->getDecl();
3824 // FIXME - Use iterator.
David Blaikie262bc182012-04-30 02:36:29 +00003825 SmallVector<const FieldDecl*, 16> Fields;
3826 for (RecordDecl::field_iterator i = RD->field_begin(),
3827 e = RD->field_end(); i != e; ++i)
David Blaikie581deb32012-06-06 20:45:41 +00003828 Fields.push_back(*i);
Chris Lattner2acc6e32011-07-18 04:24:23 +00003829 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003830 const llvm::StructLayout *RecLayout =
Micah Villmow25a6a842012-10-08 16:25:52 +00003831 CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003832
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003833 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3834 ForStrongLayout, HasUnion);
3835}
3836
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003837void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003838 const llvm::StructLayout *Layout,
3839 const RecordDecl *RD,
Bill Wendling795b1002012-02-22 09:30:11 +00003840 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003841 unsigned int BytePos, bool ForStrongLayout,
3842 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003843 bool IsUnion = (RD && RD->isUnion());
3844 uint64_t MaxUnionIvarSize = 0;
3845 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003846 const FieldDecl *MaxField = 0;
3847 const FieldDecl *MaxSkippedField = 0;
3848 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003849 uint64_t MaxFieldOffset = 0;
3850 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003851 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003852 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003853
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003854 if (RecFields.empty())
3855 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003856 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3857 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikie4e4d0842012-03-11 07:00:24 +00003858 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003859 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003860 FirstFieldDelta =
3861 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3862 }
3863
Chris Lattnerf1690852009-03-31 08:48:01 +00003864 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003865 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003866 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003867 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003868 // Note that 'i' here is actually the field index inside RD of Field,
3869 // although this dependency is hidden.
3870 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003871 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003872 } else
John McCallf85e1932011-06-15 23:02:42 +00003873 FieldOffset =
3874 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003875
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003876 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003877 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003878 LastFieldBitfieldOrUnnamed = Field;
3879 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003880 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003881 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003882
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003883 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003884 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003885 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003886 if (FQT->isUnionType())
3887 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003888
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003889 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003890 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003891 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003892 continue;
3893 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003894
Chris Lattnerf1690852009-03-31 08:48:01 +00003895 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003896 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003897 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003898 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003899 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003900 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003901 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3902 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003903 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003904 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003905 FQT = CArray->getElementType();
3906 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003907
3908 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003909 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003910 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003911 int OldIndex = IvarsInfo.size() - 1;
3912 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003913
Ted Kremenek6217b802009-07-29 21:53:49 +00003914 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003915 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003916 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003917
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003918 // Replicate layout information for each array element. Note that
3919 // one element is already done.
3920 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003921 for (int FirstIndex = IvarsInfo.size() - 1,
3922 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003923 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003924 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3925 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3926 IvarsInfo[i].ivar_size));
3927 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3928 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3929 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003930 }
3931 continue;
3932 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003933 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003934 // At this point, we are done with Record/Union and array there of.
3935 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003936 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003937
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003938 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003939 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3940 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003941 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003942 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003943 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003944 MaxUnionIvarSize = UnionIvarSize;
3945 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003946 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003947 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003948 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003949 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003950 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003951 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003952 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003953 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3954 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003955 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003956 // FIXME: Why the asymmetry? We divide by word size in bits on other
3957 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003958 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003959 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003960 MaxSkippedUnionIvarSize = UnionIvarSize;
3961 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003962 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003963 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003964 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003965 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003966 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003967 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003968 }
3969 }
3970 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003971
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003972 if (LastFieldBitfieldOrUnnamed) {
3973 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3974 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003975 uint64_t BitFieldSize
3976 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003977 GC_IVAR skivar;
3978 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3979 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3980 + ((BitFieldSize % ByteSizeInBits) != 0);
3981 SkipIvars.push_back(skivar);
3982 } else {
3983 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3984 // Last field was unnamed. Must update skip info.
3985 unsigned FieldSize
3986 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3987 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3988 FieldSize / ByteSizeInBits));
3989 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003990 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003992 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003993 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003994 MaxUnionIvarSize));
3995 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003996 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003997 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003998}
3999
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004000/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
4001/// the computations and returning the layout bitmap (for ivar or blocks) in
4002/// the given argument BitMap string container. Routine reads
4003/// two containers, IvarsInfo and SkipIvars which are assumed to be
4004/// filled already by the caller.
Chris Lattner94010692012-02-05 02:30:40 +00004005llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004006 unsigned int WordsToScan, WordsToSkip;
Chris Lattner8b418682012-02-07 00:39:47 +00004007 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004008
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004009 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00004010 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004011 unsigned int WordSize =
Micah Villmow25a6a842012-10-08 16:25:52 +00004012 CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004013 if (IvarsInfo[0].ivar_bytepos == 0) {
4014 WordsToSkip = 0;
4015 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004016 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004017 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4018 WordsToScan = IvarsInfo[0].ivar_size;
4019 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004020 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004021 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004022 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004023 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004024 // consecutive 'scanned' object pointers.
4025 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004026 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004027 // Skip over 'gc'able object pointer which lay over each other.
4028 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4029 continue;
4030 // Must skip over 1 or more words. We save current skip/scan values
4031 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004032 SKIP_SCAN SkScan;
4033 SkScan.skip = WordsToSkip;
4034 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004035 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004036
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004037 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004038 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4039 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004040 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004041 WordsToSkip = 0;
4042 WordsToScan = IvarsInfo[i].ivar_size;
4043 }
4044 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004045 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004046 SKIP_SCAN SkScan;
4047 SkScan.skip = WordsToSkip;
4048 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004049 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004050 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004051
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004052 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004053 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004054 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004055 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004056 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004057 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004058 IvarsInfo[LastIndex].ivar_bytepos +
4059 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004060 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00004061 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004062 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00004063 SKIP_SCAN SkScan;
4064 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4065 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004066 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004067 }
4068 }
4069 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4070 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00004071 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004072 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004073 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4074 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4075 // 0xM0 followed by 0x0N detected.
4076 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4077 for (int j = i+1; j < SkipScan; j++)
4078 SkipScanIvars[j] = SkipScanIvars[j+1];
4079 --SkipScan;
4080 }
4081 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004082
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004083 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004084 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004085 unsigned char byte;
4086 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4087 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4088 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4089 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004090
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004091 // first skip big.
4092 for (unsigned int ix = 0; ix < skip_big; ix++)
4093 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004094
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004095 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004096 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004097 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004098 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004099 byte |= 0xf;
4100 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004101 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004102 byte |= scan_small;
4103 scan_small = 0;
4104 }
4105 BitMap += byte;
4106 }
4107 // next scan big
4108 for (unsigned int ix = 0; ix < scan_big; ix++)
4109 BitMap += (unsigned char)(0x0f);
4110 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00004111 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00004112 byte = scan_small;
4113 BitMap += byte;
4114 }
4115 }
4116 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00004117 unsigned char zero = 0;
4118 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004119
4120 llvm::GlobalVariable * Entry =
4121 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004122 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004123 ((ObjCABI == 2) ?
4124 "__TEXT,__objc_classname,cstring_literals" :
4125 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004126 1, true);
4127 return getConstantGEP(VMContext, Entry, 0, 0);
4128}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004129
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004130/// BuildIvarLayout - Builds ivar layout bitmap for the class
4131/// implementation for the __strong or __weak case.
4132/// The layout map displays which words in ivar list must be skipped
4133/// and which must be scanned by GC (see below). String is built of bytes.
4134/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4135/// of words to skip and right nibble is count of words to scan. So, each
4136/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4137/// represented by a 0x00 byte which also ends the string.
4138/// 1. when ForStrongLayout is true, following ivars are scanned:
4139/// - id, Class
4140/// - object *
4141/// - __strong anything
4142///
4143/// 2. When ForStrongLayout is false, following ivars are scanned:
4144/// - __weak anything
4145///
4146llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4147 const ObjCImplementationDecl *OMD,
4148 bool ForStrongLayout) {
4149 bool hasUnion = false;
4150
Chris Lattner8b418682012-02-07 00:39:47 +00004151 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikie4e4d0842012-03-11 07:00:24 +00004152 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4153 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004154 return llvm::Constant::getNullValue(PtrTy);
4155
Jordy Rosedb8264e2011-07-22 02:08:32 +00004156 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4157 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikie4e4d0842012-03-11 07:00:24 +00004158 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004159 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004160 IVD; IVD = IVD->getNextIvar())
4161 RecFields.push_back(cast<FieldDecl>(IVD));
4162 }
4163 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004164 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004165 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004166
Jordy Rosedb8264e2011-07-22 02:08:32 +00004167 // FIXME: This is not ideal; we shouldn't have to do this copy.
4168 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004169 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004170
4171 if (RecFields.empty())
4172 return llvm::Constant::getNullValue(PtrTy);
4173
4174 SkipIvars.clear();
4175 IvarsInfo.clear();
4176
4177 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4178 if (IvarsInfo.empty())
4179 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004180 // Sort on byte position in case we encounterred a union nested in
4181 // the ivar list.
4182 if (hasUnion && !IvarsInfo.empty())
4183 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4184 if (hasUnion && !SkipIvars.empty())
4185 std::sort(SkipIvars.begin(), SkipIvars.end());
4186
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004187 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004188 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004189
David Blaikie4e4d0842012-03-11 07:00:24 +00004190 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004191 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004192 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004193 OMD->getClassInterface()->getName().data());
Roman Divacky31ba6132012-09-06 15:59:27 +00004194 const unsigned char *s = (const unsigned char*)BitMap.c_str();
Bill Wendling13562a12012-02-07 09:06:01 +00004195 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004196 if (!(s[i] & 0xf0))
4197 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4198 else
4199 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4200 printf("\n");
4201 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004202 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004203}
4204
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004205llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004206 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4207
Chris Lattner0b239712012-02-06 22:16:34 +00004208 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004209 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004210 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner94010692012-02-05 02:30:40 +00004211 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004212 ((ObjCABI == 2) ?
4213 "__TEXT,__objc_methname,cstring_literals" :
4214 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004215 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004216
Owen Andersona1cf15f2009-07-14 23:10:40 +00004217 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004218}
4219
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004220// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004221llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004222 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4223}
4224
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004225llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004226 std::string TypeStr;
4227 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4228
4229 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004230
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004231 if (!Entry)
4232 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004233 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004234 ((ObjCABI == 2) ?
4235 "__TEXT,__objc_methtype,cstring_literals" :
4236 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004237 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004238
Owen Andersona1cf15f2009-07-14 23:10:40 +00004239 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004240}
4241
Bob Wilsondc8dab62011-11-30 01:57:58 +00004242llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4243 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004244 std::string TypeStr;
Bill Wendlinga4dc6932012-02-09 22:45:21 +00004245 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004246 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004247
4248 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4249
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004250 if (!Entry)
4251 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner94010692012-02-05 02:30:40 +00004252 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004253 ((ObjCABI == 2) ?
4254 "__TEXT,__objc_methtype,cstring_literals" :
4255 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004256 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004257
Owen Andersona1cf15f2009-07-14 23:10:40 +00004258 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004259}
4260
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004261// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004262llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004263 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004265 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004266 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner94010692012-02-05 02:30:40 +00004267 llvm::ConstantDataArray::getString(VMContext,
4268 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004269 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004270 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004271
Owen Andersona1cf15f2009-07-14 23:10:40 +00004272 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004273}
4274
4275// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004276// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004277llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004278CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4279 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004280 std::string TypeStr;
4281 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004282 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4283}
4284
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004285void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004286 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004287 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004288 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004289 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004290 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4291 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004292 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004293 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +00004294 OS << '(' << *CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004295 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004296}
4297
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004298void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004299 EmitModuleInfo();
4300
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004301 // Emit the dummy bodies for any protocols which were referenced but
4302 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004303 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004304 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4305 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004306 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004307
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004308 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004309 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004310 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004311 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004312 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004313 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004314 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004315 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004316 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004317 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004318 }
4319
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004320 // Add assembler directives to add lazy undefined symbol references
4321 // for classes which are referenced but not defined. This is
4322 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004323 //
4324 // FIXME: It would be nice if we had an LLVM construct for this.
4325 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004326 SmallString<256> Asm;
Daniel Dunbar33063492009-09-07 00:20:42 +00004327 Asm += CGM.getModule().getModuleInlineAsm();
4328 if (!Asm.empty() && Asm.back() != '\n')
4329 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004330
Daniel Dunbar33063492009-09-07 00:20:42 +00004331 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004332 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4333 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004334 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4335 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004336 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004337 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004338 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004339 }
4340
Bill Wendling13562a12012-02-07 09:06:01 +00004341 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004342 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4343 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4344 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004345
Daniel Dunbar33063492009-09-07 00:20:42 +00004346 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004347 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004348}
4349
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004350CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004351 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004352 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004353 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004354 ObjCABI = 2;
4355}
4356
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004357/* *** */
4358
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004359ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregor65a1e672012-01-17 23:38:32 +00004360 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4361{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004362 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4363 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004364
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004365 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004366 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004367 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004368 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattner8b418682012-02-07 00:39:47 +00004369 Int8PtrTy = CGM.Int8PtrTy;
4370 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004371
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004372 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004373 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004374 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004375
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004376 // I'm not sure I like this. The implicit coordination is a bit
4377 // gross. We should solve this in a reasonable fashion because this
4378 // is a pretty common task (match some runtime data structure with
4379 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004380
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004381 // FIXME: This is leaked.
4382 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004383
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004384 // struct _objc_super {
4385 // id self;
4386 // Class cls;
4387 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004388 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004389 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004390 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004391 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004392 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004393 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
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.getObjCClassType(), 0, 0, false,
4396 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004397 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004398
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004399 SuperCTy = Ctx.getTagDeclType(RD);
4400 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004401
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004402 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004403 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4404
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004405 // struct _prop_t {
4406 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004407 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004408 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004409 PropertyTy = llvm::StructType::create("struct._prop_t",
4410 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004412 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004413 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004414 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004415 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004416 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004417 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004418 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4419 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004420 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004421 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004422
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004423 // struct _objc_method {
4424 // SEL _cmd;
4425 // char *method_type;
4426 // char *_imp;
4427 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004428 MethodTy = llvm::StructType::create("struct._objc_method",
4429 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4430 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004431
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004432 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004433 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004434 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004435
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004436}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004437
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004439 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004440 // struct _objc_method_description {
4441 // SEL name;
4442 // char *types;
4443 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004445 llvm::StructType::create("struct._objc_method_description",
4446 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004447
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004448 // struct _objc_method_description_list {
4449 // int count;
4450 // struct _objc_method_description[1];
4451 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004452 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004453 llvm::StructType::create("struct._objc_method_description_list",
4454 IntTy,
4455 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004456
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004457 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004459 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004460
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004461 // Protocol description structures
4462
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004463 // struct _objc_protocol_extension {
4464 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4465 // struct _objc_method_description_list *optional_instance_methods;
4466 // struct _objc_method_description_list *optional_class_methods;
4467 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004468 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004469 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004470 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004471 llvm::StructType::create("struct._objc_protocol_extension",
4472 IntTy, MethodDescriptionListPtrTy,
4473 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004474 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004475
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004476 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004477 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004478
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004479 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004480
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004481 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004482 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004483
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004484 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004485 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004486 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004487 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004488 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004489 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004490
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004491 // struct _objc_protocol {
4492 // struct _objc_protocol_extension *isa;
4493 // char *protocol_name;
4494 // struct _objc_protocol **_objc_protocol_list;
4495 // struct _objc_method_description_list *instance_methods;
4496 // struct _objc_method_description_list *class_methods;
4497 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004498 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4499 llvm::PointerType::getUnqual(ProtocolListTy),
4500 MethodDescriptionListPtrTy,
4501 MethodDescriptionListPtrTy,
4502 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004503
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004504 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004505 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004506
Owen Anderson96e0fc72009-07-29 22:16:19 +00004507 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004508
4509 // Class description structures
4510
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004511 // struct _objc_ivar {
4512 // char *ivar_name;
4513 // char *ivar_type;
4514 // int ivar_offset;
4515 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004516 IvarTy = llvm::StructType::create("struct._objc_ivar",
4517 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004518
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004519 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004520 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004521 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004522 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004523
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004524 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004525 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004526 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004527 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004528
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004529 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004530 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004531 llvm::StructType::create("struct._objc_class_extension",
4532 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004533 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004534
Chris Lattnerc1c20112011-08-12 17:43:31 +00004535 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004536
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004537 // struct _objc_class {
4538 // Class isa;
4539 // Class super_class;
4540 // char *name;
4541 // long version;
4542 // long info;
4543 // long instance_size;
4544 // struct _objc_ivar_list *ivars;
4545 // struct _objc_method_list *methods;
4546 // struct _objc_cache *cache;
4547 // struct _objc_protocol_list *protocols;
4548 // char *ivar_layout;
4549 // struct _objc_class_ext *ext;
4550 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004551 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4552 llvm::PointerType::getUnqual(ClassTy),
4553 Int8PtrTy,
4554 LongTy,
4555 LongTy,
4556 LongTy,
4557 IvarListPtrTy,
4558 MethodListPtrTy,
4559 CachePtrTy,
4560 ProtocolListPtrTy,
4561 Int8PtrTy,
4562 ClassExtensionPtrTy,
4563 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004564
Owen Anderson96e0fc72009-07-29 22:16:19 +00004565 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004566
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004567 // struct _objc_category {
4568 // char *category_name;
4569 // char *class_name;
4570 // struct _objc_method_list *instance_method;
4571 // struct _objc_method_list *class_method;
4572 // uint32_t size; // sizeof(struct _objc_category)
4573 // struct _objc_property_list *instance_properties;// category's @property
4574 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004575 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004576 llvm::StructType::create("struct._objc_category",
4577 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4578 MethodListPtrTy, ProtocolListPtrTy,
4579 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004580
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004581 // Global metadata structures
4582
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004583 // struct _objc_symtab {
4584 // long sel_ref_cnt;
4585 // SEL *refs;
4586 // short cls_def_cnt;
4587 // short cat_def_cnt;
4588 // char *defs[cls_def_cnt + cat_def_cnt];
4589 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004590 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004591 llvm::StructType::create("struct._objc_symtab",
4592 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4593 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004594 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004595
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004596 // struct _objc_module {
4597 // long version;
4598 // long size; // sizeof(struct _objc_module)
4599 // char *name;
4600 // struct _objc_symtab* symtab;
4601 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004603 llvm::StructType::create("struct._objc_module",
4604 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004605
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004606
Mike Stumpf5408fe2009-05-16 07:57:57 +00004607 // FIXME: This is the size of the setjmp buffer and should be target
4608 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004609 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004610
Anders Carlsson124526b2008-09-09 10:10:21 +00004611 // Exceptions
Chris Lattner8b418682012-02-07 00:39:47 +00004612 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004613
4614 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004615 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner8b418682012-02-07 00:39:47 +00004616 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4617 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004618
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004619}
4620
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004622 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004623 // struct _method_list_t {
4624 // uint32_t entsize; // sizeof(struct _objc_method)
4625 // uint32_t method_count;
4626 // struct _objc_method method_list[method_count];
4627 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004628 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004629 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4630 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004631 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004632 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004633
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004634 // struct _protocol_t {
4635 // id isa; // NULL
4636 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004637 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004638 // const struct method_list_t * const instance_methods;
4639 // const struct method_list_t * const class_methods;
4640 // const struct method_list_t *optionalInstanceMethods;
4641 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004642 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004643 // const uint32_t size; // sizeof(struct _protocol_t)
4644 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004645 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004646 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004647
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004648 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004649 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004650 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004652 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004653 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4654 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4655 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4656 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004657 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4658 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004659
4660 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004661 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004662
Fariborz Jahanianda320092009-01-29 19:24:30 +00004663 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004664 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004665 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004666 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004667 ProtocolListnfABITy->setBody(LongTy,
4668 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4669 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004670
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004671 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004672 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004673
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004674 // struct _ivar_t {
4675 // unsigned long int *offset; // pointer to ivar offset location
4676 // char *name;
4677 // char *type;
4678 // uint32_t alignment;
4679 // uint32_t size;
4680 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004681 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004682 llvm::StructType::create("struct._ivar_t",
4683 llvm::PointerType::getUnqual(LongTy),
4684 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004685
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004686 // struct _ivar_list_t {
4687 // uint32 entsize; // sizeof(struct _ivar_t)
4688 // uint32 count;
4689 // struct _iver_t list[count];
4690 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004691 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004692 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4693 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004694
Owen Anderson96e0fc72009-07-29 22:16:19 +00004695 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004696
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004697 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004698 // uint32_t const flags;
4699 // uint32_t const instanceStart;
4700 // uint32_t const instanceSize;
4701 // uint32_t const reserved; // only when building for 64bit targets
4702 // const uint8_t * const ivarLayout;
4703 // const char *const name;
4704 // const struct _method_list_t * const baseMethods;
4705 // const struct _objc_protocol_list *const baseProtocols;
4706 // const struct _ivar_list_t *const ivars;
4707 // const uint8_t * const weakIvarLayout;
4708 // const struct _prop_list_t * const properties;
4709 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004711 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004712 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4713 IntTy, IntTy, IntTy, Int8PtrTy,
4714 Int8PtrTy, MethodListnfABIPtrTy,
4715 ProtocolListnfABIPtrTy,
4716 IvarListnfABIPtrTy,
4717 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004718
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004719 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004720 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004721 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4722 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004723
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004724 // struct _class_t {
4725 // struct _class_t *isa;
4726 // struct _class_t * const superclass;
4727 // void *cache;
4728 // IMP *vtable;
4729 // struct class_ro_t *ro;
4730 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004731
Chris Lattnerc1c20112011-08-12 17:43:31 +00004732 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004733 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4734 llvm::PointerType::getUnqual(ClassnfABITy),
4735 CachePtrTy,
4736 llvm::PointerType::getUnqual(ImpnfABITy),
4737 llvm::PointerType::getUnqual(ClassRonfABITy),
4738 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004739
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004740 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004741 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004742
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004743 // struct _category_t {
4744 // const char * const name;
4745 // struct _class_t *const cls;
4746 // const struct _method_list_t * const instance_methods;
4747 // const struct _method_list_t * const class_methods;
4748 // const struct _protocol_list_t * const protocols;
4749 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004750 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004751 CategorynfABITy = llvm::StructType::create("struct._category_t",
4752 Int8PtrTy, ClassnfABIPtrTy,
4753 MethodListnfABIPtrTy,
4754 MethodListnfABIPtrTy,
4755 ProtocolListnfABIPtrTy,
4756 PropertyListPtrTy,
4757 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004758
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004759 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004760 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4761 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004762
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004763 // MessageRefTy - LLVM for:
4764 // struct _message_ref_t {
4765 // IMP messenger;
4766 // SEL name;
4767 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004768
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004769 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004770 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004771 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004772 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004773 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004774 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smithca523302012-06-10 03:12:00 +00004775 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
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.getObjCSelType(), 0, 0, false,
4778 ICIS_NoInit));
Douglas Gregor838db382010-02-11 01:19:42 +00004779 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004780
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004781 MessageRefCTy = Ctx.getTagDeclType(RD);
4782 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4783 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004784
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004785 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004786 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004787
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004788 // SuperMessageRefTy - LLVM for:
4789 // struct _super_message_ref_t {
4790 // SUPER_IMP messenger;
4791 // SEL name;
4792 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004793 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004794 llvm::StructType::create("struct._super_message_ref_t",
4795 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004796
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004797 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004798 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004799
Daniel Dunbare588b992009-03-01 04:46:24 +00004800
4801 // struct objc_typeinfo {
4802 // const void** vtable; // objc_ehtype_vtable + 2
4803 // const char* name; // c++ typeinfo string
4804 // Class cls;
4805 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004806 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004807 llvm::StructType::create("struct._objc_typeinfo",
4808 llvm::PointerType::getUnqual(Int8PtrTy),
4809 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004810 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004811}
4812
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004813llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004814 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004815
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004816 return NULL;
4817}
4818
Bill Wendlingbb028552012-02-07 09:25:09 +00004819void CGObjCNonFragileABIMac::
4820AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4821 const char *SymbolName,
4822 const char *SectionName) {
Daniel Dunbar463b8762009-05-15 21:48:48 +00004823 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004824
Daniel Dunbar463b8762009-05-15 21:48:48 +00004825 if (!NumClasses)
4826 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004827
Chris Lattner0b239712012-02-06 22:16:34 +00004828 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004829 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004830 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004831 ObjCTypes.Int8PtrTy);
Chris Lattner0b239712012-02-06 22:16:34 +00004832 llvm::Constant *Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004833 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner0b239712012-02-06 22:16:34 +00004834 Symbols.size()),
Daniel Dunbar463b8762009-05-15 21:48:48 +00004835 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004836
Daniel Dunbar463b8762009-05-15 21:48:48 +00004837 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004838 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004839 llvm::GlobalValue::InternalLinkage,
4840 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004841 SymbolName);
Micah Villmow25a6a842012-10-08 16:25:52 +00004842 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004843 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004844 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004845}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004846
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004847void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4848 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004849
Daniel Dunbar463b8762009-05-15 21:48:48 +00004850 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004851 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004852 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004853 "\01L_OBJC_LABEL_CLASS_$",
4854 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004855
Bill Wendling13562a12012-02-07 09:06:01 +00004856 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004857 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4858 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4859 continue;
4860 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004861 }
4862
Bill Wendling13562a12012-02-07 09:06:01 +00004863 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004864 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4865 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4866 continue;
4867 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4868 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004869
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004870 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004871 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4872 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004873
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004874 // Build list of all implemented category addresses in array
4875 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004876 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004877 "\01L_OBJC_LABEL_CATEGORY_$",
4878 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004879 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004880 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4881 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004882
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004883 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004884}
4885
John McCall944c8432011-05-14 03:10:52 +00004886/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4887/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004888/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004889/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004890bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4891 // At various points we've experimented with using vtable-based
4892 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004893 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004894 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004895 return false;
John McCall944c8432011-05-14 03:10:52 +00004896 case CodeGenOptions::NonLegacy:
4897 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004898 case CodeGenOptions::Mixed:
4899 break;
4900 }
4901
4902 // If so, see whether this selector is in the white-list of things which must
4903 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004904 if (VTableDispatchMethods.empty()) {
4905 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4906 VTableDispatchMethods.insert(GetNullarySelector("class"));
4907 VTableDispatchMethods.insert(GetNullarySelector("self"));
4908 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4909 VTableDispatchMethods.insert(GetNullarySelector("length"));
4910 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004911
John McCall944c8432011-05-14 03:10:52 +00004912 // These are vtable-based if GC is disabled.
4913 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004914 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004915 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4916 VTableDispatchMethods.insert(GetNullarySelector("release"));
4917 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4918 }
4919
4920 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4921 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4922 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4923 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4924 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4925 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4926 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4927
4928 // These are vtable-based if GC is enabled.
4929 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikie4e4d0842012-03-11 07:00:24 +00004930 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004931 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4932 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4933
4934 // "countByEnumeratingWithState:objects:count"
4935 IdentifierInfo *KeyIdents[] = {
4936 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4937 &CGM.getContext().Idents.get("objects"),
4938 &CGM.getContext().Idents.get("count")
4939 };
4940 VTableDispatchMethods.insert(
4941 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4942 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004943 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004944
John McCall944c8432011-05-14 03:10:52 +00004945 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004946}
4947
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004948// Metadata flags
4949enum MetaDataDlags {
4950 CLS = 0x0,
4951 CLS_META = 0x1,
4952 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004953 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004954 CLS_EXCEPTION = 0x20,
4955
4956 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4957 CLS_HAS_IVAR_RELEASER = 0x40,
4958 /// class was compiled with -fobjc-arr
4959 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004960};
4961/// BuildClassRoTInitializer - generate meta-data for:
4962/// struct _class_ro_t {
4963/// uint32_t const flags;
4964/// uint32_t const instanceStart;
4965/// uint32_t const instanceSize;
4966/// uint32_t const reserved; // only when building for 64bit targets
4967/// const uint8_t * const ivarLayout;
4968/// const char *const name;
4969/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004970/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004971/// const struct _ivar_list_t *const ivars;
4972/// const uint8_t * const weakIvarLayout;
4973/// const struct _prop_list_t * const properties;
4974/// }
4975///
4976llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004977 unsigned flags,
4978 unsigned InstanceStart,
4979 unsigned InstanceSize,
4980 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004981 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004982 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004983
David Blaikie4e4d0842012-03-11 07:00:24 +00004984 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00004985 flags |= CLS_COMPILED_BY_ARC;
4986
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004987 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4988 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4989 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004990 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004991 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4992 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004993 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004994 // const struct _method_list_t * const baseMethods;
4995 std::vector<llvm::Constant*> Methods;
4996 std::string MethodListName("\01l_OBJC_$_");
4997 if (flags & CLS_META) {
4998 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004999 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005000 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005001 // Class methods should always be defined.
5002 Methods.push_back(GetMethodConstant(*i));
5003 }
5004 } else {
5005 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005006 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005007 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005008 // Instance methods should always be defined.
5009 Methods.push_back(GetMethodConstant(*i));
5010 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005011 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005012 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00005013 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005014
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005015 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5016 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005017
Fariborz Jahanian939abce2009-01-28 22:46:49 +00005018 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5019 if (llvm::Constant *C = GetMethodConstant(MD))
5020 Methods.push_back(C);
5021 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5022 if (llvm::Constant *C = GetMethodConstant(MD))
5023 Methods.push_back(C);
5024 }
5025 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005026 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005027 Values[ 5] = EmitMethodList(MethodListName,
5028 "__DATA, __objc_const", Methods);
5029
Fariborz Jahanianda320092009-01-29 19:24:30 +00005030 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5031 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005032 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005033 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00005034 OID->all_referenced_protocol_begin(),
5035 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005037 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005038 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005039 else
5040 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005041 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
5042 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005043 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005044 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005045 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005046 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5047 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005048 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005049 Values);
5050 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005051 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5052 llvm::GlobalValue::InternalLinkage,
5053 Init,
5054 (flags & CLS_META) ?
5055 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5056 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005057 CLASS_RO_GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005058 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005059 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005060 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005061
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005062}
5063
5064/// BuildClassMetaData - This routine defines that to-level meta-data
5065/// for the given ClassName for:
5066/// struct _class_t {
5067/// struct _class_t *isa;
5068/// struct _class_t * const superclass;
5069/// void *cache;
5070/// IMP *vtable;
5071/// struct class_ro_t *ro;
5072/// }
5073///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005074llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075 std::string &ClassName,
5076 llvm::Constant *IsAGV,
5077 llvm::Constant *SuperClassGV,
5078 llvm::Constant *ClassRoGV,
5079 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005080 llvm::Constant *Values[] = {
5081 IsAGV,
5082 SuperClassGV,
5083 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5084 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5085 ClassRoGV // &CLASS_RO_GV
5086 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005087 if (!Values[1])
5088 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005089 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005090 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005091 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5092 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00005093 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005094 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005095 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005096 if (HiddenVisibility)
5097 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005098 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005099}
5100
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005101bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00005102CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005103 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005104}
5105
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005106void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005107 uint32_t &InstanceStart,
5108 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00005110 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005111
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005112 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00005113 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00005114
5115 // If there are no fields, the start is the same as the end.
5116 if (!RL.getFieldCount())
5117 InstanceStart = InstanceSize;
5118 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00005119 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00005120}
5121
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005122void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5123 std::string ClassName = ID->getNameAsString();
5124 if (!ObjCEmptyCacheVar) {
5125 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005126 CGM.getModule(),
5127 ObjCTypes.CacheTy,
5128 false,
5129 llvm::GlobalValue::ExternalLinkage,
5130 0,
5131 "_objc_empty_cache");
5132
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005133 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005134 CGM.getModule(),
5135 ObjCTypes.ImpnfABITy,
5136 false,
5137 llvm::GlobalValue::ExternalLinkage,
5138 0,
5139 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005140 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005141 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005142 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005143 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005144 uint32_t InstanceStart =
Micah Villmow25a6a842012-10-08 16:25:52 +00005145 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005146 uint32_t InstanceSize = InstanceStart;
5147 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005148 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5149 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005151 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005152
5153 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005154 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005155 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005156 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005157 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005158 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005159 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005160 // class is root
5161 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005162 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005163 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005164 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005165 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005166 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5167 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5168 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005169 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005170 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005171 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005172 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005173 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005174 ObjCMetaClassName +
5175 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005176 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005177 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005178 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005179 }
5180 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5181 InstanceStart,
5182 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005183 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005184 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005185 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5186 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005187 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005188
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005189 // Metadata for the class
5190 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005191 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005192 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005193 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005194 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005195
Douglas Gregor68584ed2009-06-18 16:11:24 +00005196 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005197 flags |= CLS_EXCEPTION;
5198
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005199 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005200 flags |= CLS_ROOT;
5201 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005202 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005203 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005204 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005205 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005206 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005207 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005208 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005209 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005210 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005211 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005212 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005213 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005214 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005215
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005216 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005217 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005218 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5219 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005220 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005221
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005222 // Determine if this class is also "non-lazy".
5223 if (ImplementationIsNonLazy(ID))
5224 DefinedNonLazyClasses.push_back(ClassMD);
5225
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005226 // Force the definition of the EHType if necessary.
5227 if (flags & CLS_EXCEPTION)
5228 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005229 // Make sure method definition entries are all clear for next implementation.
5230 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005231}
5232
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005233/// GenerateProtocolRef - This routine is called to generate code for
5234/// a protocol reference expression; as in:
5235/// @code
5236/// @protocol(Proto1);
5237/// @endcode
5238/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5239/// which will hold address of the protocol meta-data.
5240///
5241llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005242 const ObjCProtocolDecl *PD) {
5243
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005244 // This routine is called for @protocol only. So, we must build definition
5245 // of protocol's meta-data (not a reference to it!)
5246 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005247 llvm::Constant *Init =
5248 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor4c86fdb2012-01-17 18:36:30 +00005249 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005250
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005251 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005252 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005253
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005254 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5255 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005256 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005257 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005258 CGM.getModule(),
5259 Init->getType(), false,
5260 llvm::GlobalValue::WeakAnyLinkage,
5261 Init,
5262 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005263 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5264 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005265 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005266 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005267}
5268
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005269/// GenerateCategory - Build metadata for a category implementation.
5270/// struct _category_t {
5271/// const char * const name;
5272/// struct _class_t *const cls;
5273/// const struct _method_list_t * const instance_methods;
5274/// const struct _method_list_t * const class_methods;
5275/// const struct _protocol_list_t * const protocols;
5276/// const struct _prop_list_t * const properties;
5277/// }
5278///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005279void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005280 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005281 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005282 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5283 "_$_" + OCD->getNameAsString());
5284 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005285 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005286
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005287 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005288 Values[0] = GetClassName(OCD->getIdentifier());
5289 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005290 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005291 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005292 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5293
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005294 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005295 std::vector<llvm::Constant*> Methods;
5296 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005297 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005298 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005299
5300 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005301 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005302 // Instance methods should always be defined.
5303 Methods.push_back(GetMethodConstant(*i));
5304 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005305
5306 Values[2] = EmitMethodList(MethodListName,
5307 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005308 Methods);
5309
5310 MethodListName = Prefix;
5311 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5312 OCD->getNameAsString();
5313 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005314 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005315 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005316 // Class methods should always be defined.
5317 Methods.push_back(GetMethodConstant(*i));
5318 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005319
5320 Values[3] = EmitMethodList(MethodListName,
5321 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005322 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005323 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005324 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005325 if (Category) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005326 SmallString<256> ExtName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005327 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5328 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005329 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005330 + Interface->getName() + "_$_"
5331 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005332 Category->protocol_begin(),
5333 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005334 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5335 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005336 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005337 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5338 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005339 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005340
5341 llvm::Constant *Init =
5342 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005343 Values);
5344 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005345 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005346 false,
5347 llvm::GlobalValue::InternalLinkage,
5348 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005349 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005350 GCATV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005351 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005352 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005353 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005354 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005355
5356 // Determine if this category is also "non-lazy".
5357 if (ImplementationIsNonLazy(OCD))
5358 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005359 // method definition entries must be clear for next implementation.
5360 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005361}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005362
5363/// GetMethodConstant - Return a struct objc_method constant for the
5364/// given method if it has been defined. The result is null if the
5365/// method has not been defined. The return value has type MethodPtrTy.
5366llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005367 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005368 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005369 if (!Fn)
5370 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005371
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005372 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005373 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005374 ObjCTypes.SelectorPtrTy),
5375 GetMethodVarType(MD),
5376 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5377 };
Owen Anderson08e25242009-07-27 22:29:56 +00005378 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005379}
5380
5381/// EmitMethodList - Build meta-data for method declarations
5382/// struct _method_list_t {
5383/// uint32_t entsize; // sizeof(struct _objc_method)
5384/// uint32_t method_count;
5385/// struct _objc_method method_list[method_count];
5386/// }
5387///
Bill Wendlingbb028552012-02-07 09:25:09 +00005388llvm::Constant *
5389CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5390 const char *Section,
5391 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005392 // Return null for empty list.
5393 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005394 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005395
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005396 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005397 // sizeof(struct _objc_method)
Micah Villmow25a6a842012-10-08 16:25:52 +00005398 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005399 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005400 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005401 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005402 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005403 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005404 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005405 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005406
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005407 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005408 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005409 llvm::GlobalValue::InternalLinkage, Init, Name);
Micah Villmow25a6a842012-10-08 16:25:52 +00005410 GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005411 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005412 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005413 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005414}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005415
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005416/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5417/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005418llvm::GlobalVariable *
5419CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5420 const ObjCIvarDecl *Ivar) {
5421 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005422 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005423 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005424 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005425 CGM.getModule().getGlobalVariable(Name);
5426 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005427 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005428 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005429 false,
5430 llvm::GlobalValue::ExternalLinkage,
5431 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005432 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005433 return IvarOffsetGV;
5434}
5435
Daniel Dunbare83be122010-04-02 21:14:02 +00005436llvm::Constant *
5437CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5438 const ObjCIvarDecl *Ivar,
5439 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005440 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005441 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005442 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005443 IvarOffsetGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005444 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005445
Mike Stumpf5408fe2009-05-16 07:57:57 +00005446 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5447 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005448 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5449 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005450 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005451 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005452 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005453 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005454 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005455 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005456}
5457
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005458/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005459/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005460/// IvarListnfABIPtrTy.
5461/// struct _ivar_t {
5462/// unsigned long int *offset; // pointer to ivar offset location
5463/// char *name;
5464/// char *type;
5465/// uint32_t alignment;
5466/// uint32_t size;
5467/// }
5468/// struct _ivar_list_t {
5469/// uint32 entsize; // sizeof(struct _ivar_t)
5470/// uint32 count;
5471/// struct _iver_t list[count];
5472/// }
5473///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005474
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005475llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005476 const ObjCImplementationDecl *ID) {
5477
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005478 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005479
Jordy Rosedb8264e2011-07-22 02:08:32 +00005480 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005481 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005482
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005483 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484
Jordy Rosedb8264e2011-07-22 02:08:32 +00005485 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005486 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005487 // Ignore unnamed bit-fields.
5488 if (!IVD->getDeclName())
5489 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005490 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005491 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005492 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005493 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5494 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005495 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005496 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Micah Villmow25a6a842012-10-08 16:25:52 +00005497 unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005498 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005500 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005501 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005502 // NOTE. Size of a bitfield does not match gcc's, because of the
5503 // way bitfields are treated special in each. But I am told that
5504 // 'size' for bitfield ivars is ignored by the runtime so it does
5505 // not matter. If it matters, there is enough info to get the
5506 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005507 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005508 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005509 }
5510 // Return null for empty list.
5511 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005512 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005513
5514 llvm::Constant *Values[3];
Micah Villmow25a6a842012-10-08 16:25:52 +00005515 unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005516 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5517 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005518 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005519 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005520 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005521 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005522 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5523 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005524 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005525 llvm::GlobalValue::InternalLinkage,
5526 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005527 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005528 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005529 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005530 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005531
Chris Lattnerad64e022009-07-17 23:57:13 +00005532 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005533 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005534}
5535
5536llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005537 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005539
Fariborz Jahanianda320092009-01-29 19:24:30 +00005540 if (!Entry) {
5541 // We use the initializer as a marker of whether this is a forward
5542 // reference or not. At module finalization we add the empty
5543 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005544 Entry =
5545 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5546 llvm::GlobalValue::ExternalLinkage,
5547 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005548 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005549 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005550 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005551
Fariborz Jahanianda320092009-01-29 19:24:30 +00005552 return Entry;
5553}
5554
5555/// GetOrEmitProtocol - Generate the protocol meta-data:
5556/// @code
5557/// struct _protocol_t {
5558/// id isa; // NULL
5559/// const char * const protocol_name;
5560/// const struct _protocol_list_t * protocol_list; // super protocols
5561/// const struct method_list_t * const instance_methods;
5562/// const struct method_list_t * const class_methods;
5563/// const struct method_list_t *optionalInstanceMethods;
5564/// const struct method_list_t *optionalClassMethods;
5565/// const struct _prop_list_t * properties;
5566/// const uint32_t size; // sizeof(struct _protocol_t)
5567/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005568/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005569/// }
5570/// @endcode
5571///
5572
5573llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005574 const ObjCProtocolDecl *PD) {
John McCall50651b92012-03-30 21:29:05 +00005575 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005576
Fariborz Jahanianda320092009-01-29 19:24:30 +00005577 // Early exit if a defining object has already been generated.
5578 if (Entry && Entry->hasInitializer())
5579 return Entry;
5580
Douglas Gregor1d784b22012-01-01 19:51:50 +00005581 // Use the protocol definition, if there is one.
5582 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5583 PD = Def;
5584
Fariborz Jahanianda320092009-01-29 19:24:30 +00005585 // Construct method lists.
5586 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5587 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005588 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005590 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005591 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005592 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005593 if (!C)
5594 return GetOrEmitProtocolRef(PD);
5595
Fariborz Jahanianda320092009-01-29 19:24:30 +00005596 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5597 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005598 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005599 } else {
5600 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005601 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005602 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005603 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005604
5605 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005606 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005607 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005608 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005609 if (!C)
5610 return GetOrEmitProtocolRef(PD);
5611
Fariborz Jahanianda320092009-01-29 19:24:30 +00005612 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5613 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005614 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005615 } else {
5616 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005617 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005618 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005620
Bob Wilsondc8dab62011-11-30 01:57:58 +00005621 MethodTypesExt.insert(MethodTypesExt.end(),
5622 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5623
5624 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005625 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005626 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005627 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005628 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5629 PD->protocol_begin(),
5630 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005631
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005632 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005633 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005634 "__DATA, __objc_const",
5635 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005636 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005637 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005638 "__DATA, __objc_const",
5639 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005640 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005641 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005642 "__DATA, __objc_const",
5643 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005644 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005645 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005646 "__DATA, __objc_const",
5647 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005648 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005649 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005650 uint32_t Size =
Micah Villmow25a6a842012-10-08 16:25:52 +00005651 CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005652 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005653 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005654 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5655 + PD->getName(),
5656 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005657 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005658 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005659
Fariborz Jahanianda320092009-01-29 19:24:30 +00005660 if (Entry) {
5661 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005662 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005663 Entry->setInitializer(Init);
5664 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005665 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005666 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5667 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5668 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005669 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005670 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005671 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCall50651b92012-03-30 21:29:05 +00005672
5673 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005674 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005675 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005676 CGM.AddUsedGlobal(Entry);
5677
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005678 // Use this protocol meta-data to build protocol list table in section
5679 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005680 llvm::GlobalVariable *PTGV =
5681 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5682 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5683 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005684 PTGV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005685 CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005686 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005687 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005688 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005689 return Entry;
5690}
5691
5692/// EmitProtocolList - Generate protocol list meta-data:
5693/// @code
5694/// struct _protocol_list_t {
5695/// long protocol_count; // Note, this is 32/64 bit
5696/// struct _protocol_t[protocol_count];
5697/// }
5698/// @endcode
5699///
5700llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005701CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005702 ObjCProtocolDecl::protocol_iterator begin,
5703 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendling3964e622012-02-09 22:16:49 +00005704 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005705
Fariborz Jahanianda320092009-01-29 19:24:30 +00005706 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005707 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005708 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709
Daniel Dunbar948e2582009-02-15 07:36:20 +00005710 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00005711 SmallString<256> TmpName;
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005712 Name.toVector(TmpName);
5713 llvm::GlobalVariable *GV =
5714 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005715 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005716 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005717
Daniel Dunbar948e2582009-02-15 07:36:20 +00005718 for (; begin != end; ++begin)
5719 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5720
Fariborz Jahanianda320092009-01-29 19:24:30 +00005721 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005722 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723 ObjCTypes.ProtocolnfABIPtrTy));
5724
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005725 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005726 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005727 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005728 Values[1] =
Bill Wendling3964e622012-02-09 22:16:49 +00005729 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5730 ProtocolRefs.size()),
5731 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005733 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005734 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005735 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005736 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005737 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005738 GV->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005739 CGM.getDataLayout().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005740 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005742 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005743}
5744
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005745/// GetMethodDescriptionConstant - This routine build following meta-data:
5746/// struct _objc_method {
5747/// SEL _cmd;
5748/// char *method_type;
5749/// char *_imp;
5750/// }
5751
5752llvm::Constant *
5753CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005754 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005755 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005756 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5757 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005758 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005759 if (!Desc[1])
5760 return 0;
5761
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005762 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005763 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005764 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005765}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005766
5767/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5768/// This code gen. amounts to generating code for:
5769/// @code
5770/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5771/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005772///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005773LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005774 CodeGen::CodeGenFunction &CGF,
5775 QualType ObjectTy,
5776 llvm::Value *BaseValue,
5777 const ObjCIvarDecl *Ivar,
5778 unsigned CVRQualifiers) {
5779 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005780 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5781 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5782 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5783 llvm::MDNode::get(VMContext,
5784 ArrayRef<llvm::Value*>()));
Daniel Dunbar97776872009-04-22 07:32:20 +00005785 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancd285d02012-02-20 22:42:22 +00005786 Offset);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005787}
5788
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005789llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005790 CodeGen::CodeGenFunction &CGF,
5791 const ObjCInterfaceDecl *Interface,
5792 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005793 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005794}
5795
John McCallb1e81442011-05-13 23:16:18 +00005796static void appendSelectorForMessageRefTable(std::string &buffer,
5797 Selector selector) {
5798 if (selector.isUnarySelector()) {
5799 buffer += selector.getNameForSlot(0);
5800 return;
5801 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005802
John McCallb1e81442011-05-13 23:16:18 +00005803 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5804 buffer += selector.getNameForSlot(i);
5805 buffer += '_';
5806 }
5807}
5808
John McCall944c8432011-05-14 03:10:52 +00005809/// Emit a "v-table" message send. We emit a weak hidden-visibility
5810/// struct, initially containing the selector pointer and a pointer to
5811/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5812/// load and call the function pointer, passing the address of the
5813/// struct as the second parameter. The runtime determines whether
5814/// the selector is currently emitted using vtable dispatch; if so, it
5815/// substitutes a stub function which simply tail-calls through the
5816/// appropriate vtable slot, and if not, it substitues a stub function
5817/// which tail-calls objc_msgSend. Both stubs adjust the selector
5818/// argument to correctly point to the selector.
5819RValue
5820CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5821 ReturnValueSlot returnSlot,
5822 QualType resultType,
5823 Selector selector,
5824 llvm::Value *arg0,
5825 QualType arg0Type,
5826 bool isSuper,
5827 const CallArgList &formalArgs,
5828 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005829 // Compute the actual arguments.
5830 CallArgList args;
5831
John McCall944c8432011-05-14 03:10:52 +00005832 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005833 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005834 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5835 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005836
John McCall944c8432011-05-14 03:10:52 +00005837 // Second argument: a pointer to the message ref structure. Leave
5838 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005839 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5840
5841 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5842
John McCallde5d3c72012-02-17 03:33:10 +00005843 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCallb1e81442011-05-13 23:16:18 +00005844
John McCallcba681a2011-05-14 21:12:11 +00005845 NullReturnState nullReturn;
5846
John McCall944c8432011-05-14 03:10:52 +00005847 // Find the function to call and the mangled name for the message
5848 // ref structure. Using a different mangled name wouldn't actually
5849 // be a problem; it would just be a waste.
5850 //
5851 // The runtime currently never uses vtable dispatch for anything
5852 // except normal, non-super message-sends.
5853 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005854 llvm::Constant *fn = 0;
5855 std::string messageRefName("\01l_");
John McCallde5d3c72012-02-17 03:33:10 +00005856 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005857 if (isSuper) {
5858 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5859 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005860 } else {
John McCallcba681a2011-05-14 21:12:11 +00005861 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005862 fn = ObjCTypes.getMessageSendStretFixupFn();
5863 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005864 }
John McCallb1e81442011-05-13 23:16:18 +00005865 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5866 fn = ObjCTypes.getMessageSendFpretFixupFn();
5867 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005868 } else {
John McCallb1e81442011-05-13 23:16:18 +00005869 if (isSuper) {
5870 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5871 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005872 } else {
John McCallb1e81442011-05-13 23:16:18 +00005873 fn = ObjCTypes.getMessageSendFixupFn();
5874 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005875 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005876 }
John McCallb1e81442011-05-13 23:16:18 +00005877 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5878 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879
John McCallb1e81442011-05-13 23:16:18 +00005880 // Append the selector name, except use underscores anywhere we
5881 // would have used colons.
5882 appendSelectorForMessageRefTable(messageRefName, selector);
5883
5884 llvm::GlobalVariable *messageRef
5885 = CGM.getModule().getGlobalVariable(messageRefName);
5886 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005887 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005888 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005889 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005890 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5891 init->getType(),
5892 /*constant*/ false,
5893 llvm::GlobalValue::WeakAnyLinkage,
5894 init,
5895 messageRefName);
5896 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5897 messageRef->setAlignment(16);
5898 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5899 }
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005900
5901 bool requiresnullCheck = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00005902 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005903 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5904 e = method->param_end(); i != e; ++i) {
5905 const ParmVarDecl *ParamDecl = (*i);
5906 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5907 if (!nullReturn.NullBB)
5908 nullReturn.init(CGF, arg0);
5909 requiresnullCheck = true;
5910 break;
5911 }
5912 }
5913
John McCallb1e81442011-05-13 23:16:18 +00005914 llvm::Value *mref =
5915 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5916
John McCall944c8432011-05-14 03:10:52 +00005917 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005918 args[1].RV = RValue::get(mref);
5919
5920 // Load the function to call from the message ref table.
5921 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5922 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5923
John McCallde5d3c72012-02-17 03:33:10 +00005924 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCallb1e81442011-05-13 23:16:18 +00005925
John McCallde5d3c72012-02-17 03:33:10 +00005926 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanian3c52d362012-01-30 23:39:30 +00005927 return nullReturn.complete(CGF, result, resultType, formalArgs,
5928 requiresnullCheck ? method : 0);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005929}
5930
5931/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005932CodeGen::RValue
5933CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005934 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005935 QualType ResultType,
5936 Selector Sel,
5937 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005938 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005939 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005940 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005941 return isVTableDispatchedSelector(Sel)
5942 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005943 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005944 false, CallArgs, Method)
5945 : EmitMessageSend(CGF, Return, ResultType,
5946 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005947 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005948 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005949}
5950
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005951llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005952CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005953 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5954
Daniel Dunbardfff2302009-03-02 05:18:14 +00005955 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005956 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005957 false, llvm::GlobalValue::ExternalLinkage,
5958 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005959 }
5960
5961 return GV;
5962}
5963
John McCallf85e1932011-06-15 23:02:42 +00005964llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5965 IdentifierInfo *II) {
5966 llvm::GlobalVariable *&Entry = ClassReferences[II];
5967
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005968 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005969 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005970 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005971 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005972 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5973 false, llvm::GlobalValue::InternalLinkage,
5974 ClassGV,
5975 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005976 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00005977 CGM.getDataLayout().getABITypeAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005978 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005979 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005980 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005981 }
John McCallf85e1932011-06-15 23:02:42 +00005982
Benjamin Kramer578faa82011-09-27 21:06:10 +00005983 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005984}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005985
John McCallf85e1932011-06-15 23:02:42 +00005986llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5987 const ObjCInterfaceDecl *ID) {
5988 return EmitClassRefFromId(Builder, ID->getIdentifier());
5989}
5990
5991llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5992 CGBuilderTy &Builder) {
5993 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5994 return EmitClassRefFromId(Builder, II);
5995}
5996
Daniel Dunbar11394522009-04-18 08:51:00 +00005997llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005998CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005999 const ObjCInterfaceDecl *ID) {
6000 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006001
Daniel Dunbar11394522009-04-18 08:51:00 +00006002 if (!Entry) {
6003 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6004 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006005 Entry =
6006 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00006007 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006008 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006009 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00006010 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006011 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006012 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00006013 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006014 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006015 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006016
Benjamin Kramer578faa82011-09-27 21:06:10 +00006017 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006018}
6019
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006020/// EmitMetaClassRef - Return a Value * of the address of _class_t
6021/// meta-data
6022///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006023llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6024 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006025 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6026 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00006027 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006028
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006029 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006030 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006031 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006032 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006033 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006034 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00006035 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006036 Entry->setAlignment(
Micah Villmow25a6a842012-10-08 16:25:52 +00006037 CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006038 ObjCTypes.ClassnfABIPtrTy));
6039
Daniel Dunbar33af70f2009-04-15 19:03:14 +00006040 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006041 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006042
Benjamin Kramer578faa82011-09-27 21:06:10 +00006043 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006044}
6045
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006046/// GetClass - Return a reference to the class for the given interface
6047/// decl.
6048llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6049 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006050 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00006051 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6052 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6053 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6054 }
6055
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00006056 return EmitClassRef(Builder, ID);
6057}
6058
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006059/// Generates a message send where the super is the receiver. This is
6060/// a message send to self with special delivery semantics indicating
6061/// which class's method should be called.
6062CodeGen::RValue
6063CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00006064 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006065 QualType ResultType,
6066 Selector Sel,
6067 const ObjCInterfaceDecl *Class,
6068 bool isCategoryImpl,
6069 llvm::Value *Receiver,
6070 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00006071 const CodeGen::CallArgList &CallArgs,
6072 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006073 // ...
6074 // Create and init a super structure; this is a (receiver, class)
6075 // pair we will pass to objc_msgSendSuper.
6076 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00006077 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006078
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006079 llvm::Value *ReceiverAsObject =
6080 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6081 CGF.Builder.CreateStore(ReceiverAsObject,
6082 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006083
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006084 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006085 llvm::Value *Target;
6086 if (IsClassMessage) {
6087 if (isCategoryImpl) {
6088 // Message sent to "super' in a class method defined in
6089 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00006090 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006091 Target = CGF.Builder.CreateStructGEP(Target, 0);
6092 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00006093 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00006094 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00006095 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00006096 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006097
Mike Stumpf5408fe2009-05-16 07:57:57 +00006098 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6099 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00006100 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006101 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6102 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6103 CGF.Builder.CreateStore(Target,
6104 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006105
John McCall944c8432011-05-14 03:10:52 +00006106 return (isVTableDispatchedSelector(Sel))
6107 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006108 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006109 true, CallArgs, Method)
6110 : EmitMessageSend(CGF, Return, ResultType,
6111 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006112 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00006113 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00006114}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006115
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006116llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006117 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006118 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006119
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006120 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006121 llvm::Constant *Casted =
6122 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6123 ObjCTypes.SelectorPtrTy);
6124 Entry =
6125 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6126 llvm::GlobalValue::InternalLinkage,
6127 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00006128 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00006129 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006131
Fariborz Jahanian03b29602010-06-17 19:56:20 +00006132 if (lval)
6133 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00006134 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6135
6136 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6137 llvm::MDNode::get(VMContext,
6138 ArrayRef<llvm::Value*>()));
6139 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006140}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006141/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006142/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006143///
6144void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006145 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006146 llvm::Value *dst,
6147 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006148 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006149 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006150 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006151 assert(Size <= 8 && "does not support size > 8");
6152 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6153 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006154 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6155 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006156 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6157 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006158 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6159 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006160 return;
6161}
6162
6163/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6164/// objc_assign_strongCast (id src, id *dst)
6165///
6166void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006167 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006168 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006169 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006170 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006171 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006172 assert(Size <= 8 && "does not support size > 8");
6173 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006174 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006175 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6176 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006177 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6178 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006179 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006180 src, dst, "weakassign");
6181 return;
6182}
6183
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006184void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006185 CodeGen::CodeGenFunction &CGF,
6186 llvm::Value *DestPtr,
6187 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006188 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006189 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6190 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006191 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006192 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006193 return;
6194}
6195
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006196/// EmitObjCWeakRead - Code gen for loading value of a __weak
6197/// object: objc_read_weak (id *src)
6198///
6199llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006200 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006201 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006202 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006203 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6204 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006205 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006206 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006207 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006208 return read_weak;
6209}
6210
6211/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6212/// objc_assign_weak (id src, id *dst)
6213///
6214void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006215 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006216 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006217 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006218 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006219 assert(Size <= 8 && "does not support size > 8");
6220 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6221 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006222 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6223 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006224 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6225 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006226 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006227 src, dst, "weakassign");
6228 return;
6229}
6230
6231/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6232/// objc_assign_global (id src, id *dst)
6233///
6234void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006235 llvm::Value *src, llvm::Value *dst,
6236 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006237 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006238 if (!isa<llvm::PointerType>(SrcTy)) {
Micah Villmow25a6a842012-10-08 16:25:52 +00006239 unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006240 assert(Size <= 8 && "does not support size > 8");
6241 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6242 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006243 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6244 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006245 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6246 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006247 if (!threadlocal)
6248 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6249 src, dst, "globalassign");
6250 else
6251 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6252 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006253 return;
6254}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006255
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006256void
John McCallf1549f62010-07-06 01:34:17 +00006257CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6258 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006259 EmitAtSynchronizedStmt(CGF, S,
6260 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6261 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006262}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006263
John McCall5a180392010-07-24 00:37:23 +00006264llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006265CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006266 // There's a particular fixed type info for 'id'.
6267 if (T->isObjCIdType() ||
6268 T->isObjCQualifiedIdType()) {
6269 llvm::Constant *IDEHType =
6270 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6271 if (!IDEHType)
6272 IDEHType =
6273 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6274 false,
6275 llvm::GlobalValue::ExternalLinkage,
6276 0, "OBJC_EHTYPE_id");
6277 return IDEHType;
6278 }
6279
6280 // All other types should be Objective-C interface pointer types.
6281 const ObjCObjectPointerType *PT =
6282 T->getAs<ObjCObjectPointerType>();
6283 assert(PT && "Invalid @catch type.");
6284 const ObjCInterfaceType *IT = PT->getInterfaceType();
6285 assert(IT && "Invalid @catch type.");
6286 return GetInterfaceEHType(IT->getDecl(), false);
6287}
6288
John McCallf1549f62010-07-06 01:34:17 +00006289void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6290 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006291 EmitTryCatchStmt(CGF, S,
6292 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6293 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6294 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006295}
6296
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006297/// EmitThrowStmt - Generate code for a throw statement.
6298void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6299 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006300 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006301 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006302 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006303 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006304 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006305 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006306 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006307 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006308 }
6309
John McCall7ec404c2010-10-16 08:21:07 +00006310 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006311 CGF.Builder.ClearInsertionPoint();
6312}
Daniel Dunbare588b992009-03-01 04:46:24 +00006313
John McCall5a180392010-07-24 00:37:23 +00006314llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006315CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006316 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006317 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006318
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006319 // If we don't need a definition, return the entry if found or check
6320 // if we use an external reference.
6321 if (!ForDefinition) {
6322 if (Entry)
6323 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006324
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006325 // If this type (or a super class) has the __objc_exception__
6326 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006327 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006328 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006329 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006330 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006331 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006332 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006333 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006334 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006335
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006336 // Otherwise we need to either make a new entry or fill in the
6337 // initializer.
6338 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006339 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006340 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006341 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006342 CGM.getModule().getGlobalVariable(VTableName);
6343 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006344 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6345 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006346 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006347 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006348
Chris Lattner8b418682012-02-07 00:39:47 +00006349 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006350
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006351 llvm::Constant *Values[] = {
6352 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6353 GetClassName(ID->getIdentifier()),
6354 GetClassGlobal(ClassName)
6355 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006356 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006357 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006358
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006359 if (Entry) {
6360 Entry->setInitializer(Init);
6361 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006362 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006363 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006364 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006365 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006366 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006367 }
6368
David Blaikie4e4d0842012-03-11 07:00:24 +00006369 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006370 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Micah Villmow25a6a842012-10-08 16:25:52 +00006371 Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006372 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006373
6374 if (ForDefinition) {
6375 Entry->setSection("__DATA,__objc_const");
6376 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6377 } else {
6378 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6379 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006380
6381 return Entry;
6382}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006383
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006384/* *** */
6385
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006386CodeGen::CGObjCRuntime *
6387CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00006388 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6389 case ObjCRuntime::FragileMacOSX:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006390 return new CGObjCMac(CGM);
John McCall260611a2012-06-20 06:18:46 +00006391
6392 case ObjCRuntime::MacOSX:
6393 case ObjCRuntime::iOS:
6394 return new CGObjCNonFragileABIMac(CGM);
6395
David Chisnall11d3f4c2012-07-03 20:49:52 +00006396 case ObjCRuntime::GNUstep:
6397 case ObjCRuntime::GCC:
John McCallf7226fb2012-07-12 02:07:58 +00006398 case ObjCRuntime::ObjFW:
John McCall260611a2012-06-20 06:18:46 +00006399 llvm_unreachable("these runtimes are not Mac runtimes");
6400 }
6401 llvm_unreachable("bad runtime");
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006402}